File: context_test.go

package info (click to toggle)
golang-github-jtolds-gls 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92 kB
  • sloc: makefile: 2
file content (145 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
package gls_test

import (
	"fmt"
	"sync"
	"testing"

	"github.com/jtolds/gls"
)

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

	CheckVal := func(mgr *gls.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(gls.Values{"key1": "val1c"}, func() {
		Check("", "", "val1c", "")
		mgr1.SetValues(gls.Values{"key1": "val1a"}, func() {
			Check("val1a", "", "val1c", "")
			mgr1.SetValues(gls.Values{"key2": "val1b"}, func() {
				Check("val1a", "val1b", "val1c", "")
				var wg sync.WaitGroup
				wg.Add(2)
				go func() {
					defer wg.Done()
					Check("", "", "", "")
				}()
				gls.Go(func() {
					defer wg.Done()
					Check("val1a", "val1b", "val1c", "")
				})
				wg.Wait()
				Check("val1a", "val1b", "val1c", "")
			})
			Check("val1a", "", "val1c", "")
		})
		Check("", "", "val1c", "")
	})
	Check("", "", "", "")
}

func ExampleContextManager_SetValues() {
	var (
		mgr            = gls.NewContextManager()
		request_id_key = gls.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(gls.Values{request_id_key: "12345"}, func() {
		MyLog()
	})
	MyLog()

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

func ExampleGo() {
	var (
		mgr            = gls.NewContextManager()
		request_id_key = gls.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(gls.Values{request_id_key: "12345"}, func() {
		var wg sync.WaitGroup
		wg.Add(1)
		go func() {
			defer wg.Done()
			MyLog()
		}()
		wg.Wait()
		wg.Add(1)
		gls.Go(func() {
			defer wg.Done()
			MyLog()
		})
		wg.Wait()
	})

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

func BenchmarkGetValue(b *testing.B) {
	mgr := gls.NewContextManager()
	mgr.SetValues(gls.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 := gls.NewContextManager()
	for i := 0; i < b.N/2; i++ {
		mgr.SetValues(gls.Values{"test_key": "test_val"}, func() {
			mgr.SetValues(gls.Values{"test_key2": "test_val2"}, func() {})
		})
	}
}