-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex_test.go
146 lines (138 loc) · 2.83 KB
/
mutex_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gorex
import (
"context"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMutex(t *testing.T) {
t.Run("Unlock", func(t *testing.T) {
t.Run("negative", func(t *testing.T) {
t.Run("notLocked", func(t *testing.T) {
var result interface{}
func() {
defer func() {
result = recover()
}()
locker := &Mutex{}
locker.Unlock()
}()
assert.NotNil(t, result)
assert.Equal(t, -1, strings.Index(fmt.Sprint(result), "pointer dereference"), result)
})
})
})
t.Run("LockDo", func(t *testing.T) {
t.Run("positive", func(t *testing.T) {
locker := &Mutex{}
locker.LockDo(func() {
locker.LockDo(func() {
})
})
var wg sync.WaitGroup
wg.Add(1)
i := 0
locker.LockDo(func() {
go locker.LockDo(func() {
defer wg.Done()
i = 2
})
locker.LockDo(func() {
time.Sleep(time.Microsecond)
i = 1
})
})
wg.Wait()
assert.Equal(t, 2, i)
})
t.Run("negative", func(t *testing.T) {
t.Run("endOfInfinityContext", func(t *testing.T) {
debugPanicOut = io.Discard
var result interface{}
func() {
var wg0 sync.WaitGroup
defer func() {
result = recover()
wg0.Done()
}()
var cancelFn context.CancelFunc
locker := &Mutex{}
locker.InfiniteContext, cancelFn = context.WithDeadline(context.Background(), time.Now())
defer cancelFn()
var wg1 sync.WaitGroup
wg0.Add(1)
wg1.Add(1)
go locker.LockDo(func() {
wg1.Done()
wg0.Wait()
})
wg1.Wait()
locker.Lock()
}()
assert.NotNil(t, result, result)
})
})
})
t.Run("LockTryDo", func(t *testing.T) {
t.Run("true", func(t *testing.T) {
locker := &Mutex{}
i := 0
assert.True(t, locker.LockTryDo(func() {
i = 1
}))
assert.Equal(t, 1, i)
})
t.Run("false", func(t *testing.T) {
locker := &Mutex{}
i := 0
var wg0 sync.WaitGroup
var wg1 sync.WaitGroup
wg0.Add(1)
wg1.Add(1)
go locker.LockDo(func() {
wg1.Done()
wg0.Wait()
})
wg1.Wait()
assert.False(t, locker.LockTryDo(func() {
i = 1
}))
assert.Equal(t, 0, i)
wg0.Done()
})
})
t.Run("LockCtxDo", func(t *testing.T) {
t.Run("true", func(t *testing.T) {
locker := &Mutex{}
i := 0
assert.True(t, locker.LockCtxDo(context.Background(), func() {
i = 1
}))
assert.Equal(t, 1, i)
})
t.Run("false", func(t *testing.T) {
locker := &Mutex{}
i := 0
var wg0 sync.WaitGroup
var wg1 sync.WaitGroup
wg0.Add(1)
wg1.Add(1)
go locker.LockDo(func() {
wg1.Done()
wg0.Wait()
})
wg1.Wait()
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(time.Microsecond))
defer cancelFn()
assert.False(t, locker.LockCtxDo(ctx, func() {
i = 1
}))
assert.Equal(t, 0, i)
wg0.Done()
})
})
}