forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_test.go
104 lines (88 loc) · 1.65 KB
/
state_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
package lib
import (
"context"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTagMapSet(t *testing.T) {
t.Parallel()
t.Run("Sync", func(t *testing.T) {
t.Parallel()
tm := NewTagMap(nil)
tm.Set("mytag", "42")
v, found := tm.Get("mytag")
assert.True(t, found)
assert.Equal(t, "42", v)
})
t.Run("Safe-Concurrent", func(t *testing.T) {
t.Parallel()
tm := NewTagMap(nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
count := 0
for {
select {
case <-time.Tick(1 * time.Millisecond):
count++
tm.Set("mytag", strconv.Itoa(count))
case <-ctx.Done():
return
}
}
}()
go func() {
for {
select {
case <-time.Tick(1 * time.Millisecond):
tm.Get("mytag")
case <-ctx.Done():
return
}
}
}()
time.Sleep(100 * time.Millisecond)
})
}
func TestTagMapGet(t *testing.T) {
t.Parallel()
tm := NewTagMap(map[string]string{
"key1": "value1",
})
v, ok := tm.Get("key1")
assert.True(t, ok)
assert.Equal(t, "value1", v)
}
func TestTagMapLen(t *testing.T) {
t.Parallel()
tm := NewTagMap(map[string]string{
"key1": "value1",
"key2": "value2",
})
assert.Equal(t, 2, tm.Len())
}
func TestTagMapDelete(t *testing.T) {
t.Parallel()
m := map[string]string{
"key1": "value1",
"key2": "value2",
}
tm := NewTagMap(m)
tm.Delete("key1")
_, ok := m["key1"]
assert.False(t, ok)
}
func TestTagMapClone(t *testing.T) {
t.Parallel()
tm := NewTagMap(map[string]string{
"key1": "value1",
"key2": "value2",
})
m := tm.Clone()
assert.Equal(t, map[string]string{
"key1": "value1",
"key2": "value2",
}, m)
}