-
Notifications
You must be signed in to change notification settings - Fork 2
/
varrw_test.go
62 lines (53 loc) · 974 Bytes
/
varrw_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
package multilock
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVarRW_Get(t *testing.T) {
s := []int{1}
v := NewVarRW(10)
m := v.Get(s)
m.Lock()
defer m.Unlock()
assert.Equal(t, addr(v.Get(s)), addr(m))
}
func TestVarRW_Get_One(t *testing.T) {
s := []int{1}
s2 := []int{1, 2}
v := NewVarRW(1)
m := v.Get(s)
m.Lock()
defer m.Unlock()
assert.Equal(t, addr(v.Get(s2)), addr(m))
}
func TestVarRW_Get_CustomDistribution(t *testing.T) {
s := []int{1}
s2 := []int{1, 2}
v := NewVarRW(100, WithCustomDistribution(func(_ interface{}, _ int) int {
return 0
}))
m := v.Get(s)
m.Lock()
defer m.Unlock()
assert.Equal(t, addr(v.Get(s2)), addr(m))
}
func TestVarRW_Get_Race(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
v := NewVarRW(10)
go func() {
m := v.Get("")
m.Lock()
defer m.Unlock()
wg.Done()
v.Resize(20)
}()
go func() {
m := v.Get("")
m.Lock()
defer m.Unlock()
wg.Done()
}()
wg.Wait()
}