-
Notifications
You must be signed in to change notification settings - Fork 4
/
mdc_test.go
115 lines (93 loc) · 2.29 KB
/
mdc_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
package over
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestMdcAdapter_Set(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().Set("key1", "value1")
MDC().Set("key2", "value2")
assert.Equal(t, 2, MDC().Count())
}
func TestMdcAdapter_SetMap(t *testing.T) {
ResetGlobalMdcAdapter()
var items = map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
MDC().SetMap(items)
assert.Equal(t, 3, MDC().Count())
}
func TestMdcAdapter_SetIfAbsent(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().SetIfAbsent("key1", "value1")
ok := MDC().SetIfAbsent("key1", "value2")
assert.False(t, ok)
}
func TestMdcAdapter_Get(t *testing.T) {
t.Run("get missing key", func(t *testing.T) {
ResetGlobalMdcAdapter()
val, ok := MDC().Get("key1")
assert.False(t, ok)
assert.Nil(t, val)
})
t.Run("get", func(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().Set("key1", "value1")
val, ok := MDC().Get("key1")
assert.True(t, ok)
assert.NotNil(t, val)
assert.Equal(t, "value1", val)
})
}
func TestMdcAdapter_GetString(t *testing.T) {
t.Run("getString missing key", func(t *testing.T) {
ResetGlobalMdcAdapter()
val := MDC().GetString("key1")
assert.Equal(t, "", val)
})
t.Run("getString", func(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().Set("key1", "value1")
val := MDC().GetString("key1")
assert.Equal(t, "value1", val)
})
}
func TestMdcAdapter_Count(t *testing.T) {
ResetGlobalMdcAdapter()
count := MDC().Count()
assert.Equal(t, 0, count)
var items = map[string]interface{}{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
MDC().SetMap(items)
count = MDC().Count()
assert.Equal(t, 3, count)
}
func TestMdcAdapter_Has(t *testing.T) {
ResetGlobalMdcAdapter()
has := MDC().Has("key1")
assert.False(t, has)
}
func TestMdcAdapter_Remove(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().Set("key1", "value1")
assert.Equal(t, 1, MDC().Count())
MDC().Remove("key1")
val, ok := MDC().Get("key1")
assert.Equal(t, 0, MDC().Count())
assert.False(t, ok)
assert.Nil(t, val)
}
func TestMdcAdapter_IsEmpty(t *testing.T) {
ResetGlobalMdcAdapter()
assert.True(t, MDC().IsEmpty())
}
func TestMdcAdapter_Keys(t *testing.T) {
ResetGlobalMdcAdapter()
MDC().Set("key1", "value1")
assert.Equal(t, MDC().getUniqueKey("key1"), MDC().Keys()[0])
}