-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_map_test.go
72 lines (60 loc) · 1.18 KB
/
string_map_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
package main
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStringMap(t *testing.T) {
m := NewStringStringMap()
m.Set("foo", "bar")
v, found := m.Get("foo")
if !found {
t.Errorf("foo not found")
}
if v != "bar" {
t.Errorf("foo does not correspond to bar")
}
m.Remove("foo")
if _, found = m.Get("foo"); found {
t.Errorf("foo removed but found")
}
m.Set("1", "a")
m.Set("2", "b")
m.Set("3", "c")
keys := []string{}
values := []string{}
m.Each(func(k, v string) bool {
keys = append(keys, k)
values = append(values, v)
return true
})
assert.Equal(t, []string{"1", "2", "3"}, keys)
assert.Equal(t, []string{"a", "b", "c"}, values)
}
func TestStringMapConcurrency(t *testing.T) {
m := NewStringStringMapSyncronized()
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("-%d", i)
m.Set("k-"+k, "v-"+k)
}
wg := sync.WaitGroup{}
wg.Add(1)
n := 100
done := make(chan bool, n)
for i := 0; i < n; i++ {
go func() {
wg.Wait()
m.Each(func(string, string) bool { return true })
done <- true
}()
go func(j int) {
wg.Wait()
m.Remove(fmt.Sprintf("%d", j))
}(i)
}
wg.Done()
for i := 0; i < n; i++ {
<-done
}
}