forked from stackrox/stackrox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_maputil_test.go
More file actions
90 lines (74 loc) · 1.68 KB
/
Copy pathstring_maputil_test.go
File metadata and controls
90 lines (74 loc) · 1.68 KB
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
package maputil
import (
"strconv"
"testing"
"github.com/stackrox/rox/pkg/buildinfo"
"github.com/stackrox/rox/pkg/sync"
"github.com/stretchr/testify/assert"
)
func TestStringClone(t *testing.T) {
a := assert.New(t)
m := map[string]string{
"a": "1",
"b": "2",
}
cloned := CloneStringStringMap(m)
cloned["c"] = "3"
delete(cloned, "a")
a.Equal(map[string]string{
"a": "1",
"b": "2",
}, m)
a.Equal(map[string]string{
"b": "2",
"c": "3",
}, cloned)
}
func TestStringFastRMap(t *testing.T) {
a := assert.New(t)
m := NewStringStringFastRMap()
m.Set("a", "1")
a.Equal(map[string]string{"a": "1"}, m.GetMap())
got, exists := m.Get("a")
a.True(exists)
a.Equal(got, "1")
got, exists = m.Get("b")
a.False(exists)
a.Equal(got, "")
m.Delete("a")
a.Equal(map[string]string{}, m.GetMap())
}
func TestStringFastRMapThreadSafe(t *testing.T) {
a := assert.New(t)
var wg sync.WaitGroup
m := NewStringStringFastRMap()
numThreads := 1000
if buildinfo.RaceEnabled {
numThreads = 100 // race detector chokes on this if we have too many goroutines
}
for i := 0; i < numThreads; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
m.Set("a", "1")
m.Set(strconv.Itoa(index), "2")
_ = m.GetMap()
m.Delete("a")
got, exists := m.Get(strconv.Itoa(index))
a.True(exists)
a.Equal(got, "2")
}(i)
}
wg.Wait()
currentMap := m.GetMap()
a.Len(currentMap, numThreads)
expectedKeys := make([]string, 0, numThreads)
for i := 0; i < numThreads; i++ {
expectedKeys = append(expectedKeys, strconv.Itoa(i))
}
mapKeys := make([]string, 0, numThreads)
for k := range currentMap {
mapKeys = append(mapKeys, k)
}
assert.ElementsMatch(t, mapKeys, expectedKeys)
}