context_test.go 3.0 KB

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