context_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package gls
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. )
  7. func TestContexts(t *testing.T) {
  8. mgr1 := NewContextManager()
  9. mgr2 := NewContextManager()
  10. CheckVal := func(mgr *ContextManager, key, exp_val string) {
  11. val, ok := mgr.GetValue(key)
  12. if len(exp_val) == 0 {
  13. if ok {
  14. t.Fatalf("expected no value for key %s, got %s", key, val)
  15. }
  16. return
  17. }
  18. if !ok {
  19. t.Fatalf("expected value %s for key %s, got no value",
  20. exp_val, key)
  21. }
  22. if exp_val != val {
  23. t.Fatalf("expected value %s for key %s, got %s", exp_val, key,
  24. val)
  25. }
  26. }
  27. Check := func(exp_m1v1, exp_m1v2, exp_m2v1, exp_m2v2 string) {
  28. CheckVal(mgr1, "key1", exp_m1v1)
  29. CheckVal(mgr1, "key2", exp_m1v2)
  30. CheckVal(mgr2, "key1", exp_m2v1)
  31. CheckVal(mgr2, "key2", exp_m2v2)
  32. }
  33. Check("", "", "", "")
  34. mgr2.SetValues(Values{"key1": "val1c"}, func() {
  35. Check("", "", "val1c", "")
  36. mgr1.SetValues(Values{"key1": "val1a"}, func() {
  37. Check("val1a", "", "val1c", "")
  38. mgr1.SetValues(Values{"key2": "val1b"}, func() {
  39. Check("val1a", "val1b", "val1c", "")
  40. var wg sync.WaitGroup
  41. wg.Add(2)
  42. go func() {
  43. defer wg.Done()
  44. Check("", "", "", "")
  45. }()
  46. Go(func() {
  47. defer wg.Done()
  48. Check("val1a", "val1b", "val1c", "")
  49. })
  50. wg.Wait()
  51. })
  52. })
  53. })
  54. }
  55. func ExampleContextManager_SetValues() {
  56. var (
  57. mgr = NewContextManager()
  58. request_id_key = GenSym()
  59. )
  60. MyLog := func() {
  61. if request_id, ok := mgr.GetValue(request_id_key); ok {
  62. fmt.Println("My request id is:", request_id)
  63. } else {
  64. fmt.Println("No request id found")
  65. }
  66. }
  67. mgr.SetValues(Values{request_id_key: "12345"}, func() {
  68. MyLog()
  69. })
  70. MyLog()
  71. // Output: My request id is: 12345
  72. // No request id found
  73. }
  74. func ExampleGo() {
  75. var (
  76. mgr = NewContextManager()
  77. request_id_key = GenSym()
  78. )
  79. MyLog := func() {
  80. if request_id, ok := mgr.GetValue(request_id_key); ok {
  81. fmt.Println("My request id is:", request_id)
  82. } else {
  83. fmt.Println("No request id found")
  84. }
  85. }
  86. mgr.SetValues(Values{request_id_key: "12345"}, func() {
  87. var wg sync.WaitGroup
  88. wg.Add(1)
  89. go func() {
  90. defer wg.Done()
  91. MyLog()
  92. }()
  93. wg.Wait()
  94. wg.Add(1)
  95. Go(func() {
  96. defer wg.Done()
  97. MyLog()
  98. })
  99. wg.Wait()
  100. })
  101. // Output: No request id found
  102. // My request id is: 12345
  103. }
  104. func BenchmarkGetValue(b *testing.B) {
  105. mgr := NewContextManager()
  106. mgr.SetValues(Values{"test_key": "test_val"}, func() {
  107. b.ResetTimer()
  108. for i := 0; i < b.N; i++ {
  109. val, ok := mgr.GetValue("test_key")
  110. if !ok || val != "test_val" {
  111. b.FailNow()
  112. }
  113. }
  114. })
  115. }
  116. func BenchmarkSetValues(b *testing.B) {
  117. mgr := NewContextManager()
  118. for i := 0; i < b.N/2; i++ {
  119. mgr.SetValues(Values{"test_key": "test_val"}, func() {
  120. mgr.SetValues(Values{"test_key2": "test_val2"}, func() {})
  121. })
  122. }
  123. }