-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
116 lines (103 loc) · 3.05 KB
/
context_test.go
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
package ctxslog
import (
"context"
"testing"
)
type ComplexData struct {
IntField int
StrField string
BoolField bool
SliceField []string
}
func TestWithValue(t *testing.T) {
t.Run("Will Store Single Value On Context", func(t *testing.T) {
ctx := context.Background()
defer ctx.Done()
ctx = WithValue(ctx, "key1", "value1")
m, ok := ctx.Value(fieldsKey).(*safeMap)
if !ok {
t.Errorf("Expected safeMap to be initialized in context")
}
//we expect value1 to end up in the contexts map
m.mu.Lock()
if m.fields["key1"] != "value1" {
t.Errorf("Expected value1, got %v", m.fields["key1"])
}
m.mu.Unlock()
})
t.Run("Will Store Multiple Values On Context", func(t *testing.T) {
ctx := context.Background()
defer ctx.Done()
ctx = WithValue(ctx, "key2", "value2")
ctx = WithValue(ctx, "key3", "value3")
m, ok := ctx.Value(fieldsKey).(*safeMap)
if !ok {
t.Errorf("Expected safeMap to be initialized in context")
}
//we expect value2 and value3 to end up in the contexts map
m.mu.Lock()
if m.fields["key2"] != "value2" {
t.Errorf("Expected value2, got %v", m.fields["key2"])
}
if m.fields["key3"] != "value3" {
t.Errorf("Expected value3, got %v", m.fields["key3"])
}
m.mu.Unlock()
})
t.Run("Will panic with nil parent", func(t *testing.T) {
defer func() { _ = recover() }()
_ = WithValue(nil, "", "")
t.Errorf("did not panic")
})
}
func TestWithValues(t *testing.T) {
t.Run("Will Store Multiple Values On Context", func(t *testing.T) {
ctx := context.Background()
defer ctx.Done()
cd := ComplexData{
IntField: 123,
StrField: "DEADBEEF",
BoolField: true,
SliceField: []string{"one", "two", "three"},
}
ctx = WithValues(ctx, map[string]interface{}{
"AccountID": 987654321,
"email": "[email protected]",
"complexData": cd,
})
m, ok := ctx.Value(fieldsKey).(*safeMap)
if !ok {
t.Errorf("Expected safeMap to be initialized in context")
}
//we expect value1 to end up in the contexts map
m.mu.Lock()
if m.fields["AccountID"] != 987654321 {
t.Errorf("Expected 987654321, got %v", m.fields["AccountID"])
}
if m.fields["email"] != "[email protected]" {
t.Errorf("Expected [email protected], got %v", m.fields["email"])
}
complexVal, ok := m.fields["complexData"].(ComplexData)
if !ok {
t.Errorf("mistatch type when retrieving ComplexData from context")
}
if complexVal.IntField != 123 {
t.Errorf("Expected 123, got %v", complexVal.IntField)
}
if complexVal.StrField != "DEADBEEF" {
t.Errorf("Expected 123, got %v", complexVal.StrField)
}
if complexVal.BoolField != true {
t.Errorf("Expected 123, got %v", complexVal.BoolField)
}
if complexVal.SliceField[0] != "one" || complexVal.SliceField[1] != "two" || complexVal.SliceField[2] != "three" {
t.Errorf("Expected []string{\"one\", \"two\", \"three\"}, got %v", complexVal.SliceField)
}
m.mu.Unlock()
})
t.Run("Will panic with nil parent", func(t *testing.T) {
defer func() { _ = recover() }()
_ = WithValues(nil, map[string]interface{}{})
t.Errorf("did not panic")
})
}