-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonce_test.go
56 lines (43 loc) · 824 Bytes
/
once_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
package sabotage_test
import (
"sync"
"testing"
"github.com/cristaloleg/sabotage"
)
func TestResetSyncOnce(t *testing.T) {
once := &sync.Once{}
counter := 0
work := func(param int) {
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
once.Do(func() {
counter += param
})
}()
}
wg.Wait()
}
work(42)
if want := 42; counter != want {
t.Fatalf("expected %v, got %v", want, counter)
}
sabotage.ResetSyncOnce(once)
counter = 0
work(78)
if want := 78; counter != want {
t.Fatalf("expected %v, got %v", want, counter)
}
}
func TestIsOnceDone(t *testing.T) {
once := &sync.Once{}
if sabotage.IsOnceDone(once) {
t.Error("should be false")
}
once.Do(func() {})
if !sabotage.IsOnceDone(once) {
t.Error("should be true")
}
}