summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/github.com/jtolds/gls/context_test.go
blob: ae5bde4aede051cb7dbc1ea44735a0399bff5244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gls

import (
	"fmt"
	"sync"
	"testing"
)

func TestContexts(t *testing.T) {
	mgr1 := NewContextManager()
	mgr2 := NewContextManager()

	CheckVal := func(mgr *ContextManager, key, exp_val string) {
		val, ok := mgr.GetValue(key)
		if len(exp_val) == 0 {
			if ok {
				t.Fatalf("expected no value for key %s, got %s", key, val)
			}
			return
		}
		if !ok {
			t.Fatalf("expected value %s for key %s, got no value",
				exp_val, key)
		}
		if exp_val != val {
			t.Fatalf("expected value %s for key %s, got %s", exp_val, key,
				val)
		}

	}

	Check := func(exp_m1v1, exp_m1v2, exp_m2v1, exp_m2v2 string) {
		CheckVal(mgr1, "key1", exp_m1v1)
		CheckVal(mgr1, "key2", exp_m1v2)
		CheckVal(mgr2, "key1", exp_m2v1)
		CheckVal(mgr2, "key2", exp_m2v2)
	}

	Check("", "", "", "")
	mgr2.SetValues(Values{"key1": "val1c"}, func() {
		Check("", "", "val1c", "")
		mgr1.SetValues(Values{"key1": "val1a"}, func() {
			Check("val1a", "", "val1c", "")
			mgr1.SetValues(Values{"key2": "val1b"}, func() {
				Check("val1a", "val1b", "val1c", "")
				var wg sync.WaitGroup
				wg.Add(2)
				go func() {
					defer wg.Done()
					Check("", "", "", "")
				}()
				Go(func() {
					defer wg.Done()
					Check("val1a", "val1b", "val1c", "")
				})
				wg.Wait()
			})
		})
	})
}

func ExampleContextManager_SetValues() {
	var (
		mgr            = NewContextManager()
		request_id_key = GenSym()
	)

	MyLog := func() {
		if request_id, ok := mgr.GetValue(request_id_key); ok {
			fmt.Println("My request id is:", request_id)
		} else {
			fmt.Println("No request id found")
		}
	}

	mgr.SetValues(Values{request_id_key: "12345"}, func() {
		MyLog()
	})
	MyLog()

	// Output: My request id is: 12345
	// No request id found
}

func ExampleGo() {
	var (
		mgr            = NewContextManager()
		request_id_key = GenSym()
	)

	MyLog := func() {
		if request_id, ok := mgr.GetValue(request_id_key); ok {
			fmt.Println("My request id is:", request_id)
		} else {
			fmt.Println("No request id found")
		}
	}

	mgr.SetValues(Values{request_id_key: "12345"}, func() {
		var wg sync.WaitGroup
		wg.Add(1)
		go func() {
			defer wg.Done()
			MyLog()
		}()
		wg.Wait()
		wg.Add(1)
		Go(func() {
			defer wg.Done()
			MyLog()
		})
		wg.Wait()
	})

	// Output: No request id found
	// My request id is: 12345
}

func BenchmarkGetValue(b *testing.B) {
	mgr := NewContextManager()
	mgr.SetValues(Values{"test_key": "test_val"}, func() {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			val, ok := mgr.GetValue("test_key")
			if !ok || val != "test_val" {
				b.FailNow()
			}
		}
	})
}

func BenchmarkSetValues(b *testing.B) {
	mgr := NewContextManager()
	for i := 0; i < b.N/2; i++ {
		mgr.SetValues(Values{"test_key": "test_val"}, func() {
			mgr.SetValues(Values{"test_key2": "test_val2"}, func() {})
		})
	}
}