-
Notifications
You must be signed in to change notification settings - Fork 1
/
race_test.go
113 lines (92 loc) · 1.79 KB
/
race_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
package goclock_test
import (
"context"
"testing"
"time"
goclock "github.com/msales/go-clock/v2"
)
func TestRace_After(t *testing.T) {
testMockInRace(func() {
goclock.After(1 * time.Second)
})
}
func TestRace_AfterFunc(t *testing.T) {
testMockInRace(func() {
goclock.AfterFunc(1*time.Second, func() {
})
})
}
func TestRace_Now(t *testing.T) {
testMockInRace(func() {
goclock.Now()
})
}
func TestRace_Since(t *testing.T) {
testMockInRace(func() {
goclock.Since(time.Now())
})
}
func TestRace_Sleep(t *testing.T) {
testInRace(func() {
goclock.Sleep(1 * time.Nanosecond)
})
}
func TestRace_Tick(t *testing.T) {
testMockInRace(func() {
goclock.Tick(1 * time.Nanosecond)
})
}
func TestRace_Timer(t *testing.T) {
testMockInRace(func() {
goclock.Timer(1 * time.Nanosecond)
})
}
func TestRace_Until(t *testing.T) {
testMockInRace(func() {
now := time.Now().Add(5 * time.Second)
goclock.Until(now)
})
}
func TestRace_WithDeadline(t *testing.T) {
testMockInRace(func() {
now := time.Now().Add(5 * time.Second)
goclock.WithDeadline(context.Background(), now)
})
}
func TestRace_WithTimeout(t *testing.T) {
testMockInRace(func() {
goclock.WithTimeout(context.Background(), 5*time.Second)
})
}
func testMockInRace(runFunc func()) {
now := time.Date(2019, time.September, 30, 14, 30, 00, 00, time.UTC)
wait1 := make(chan struct{}, 1)
wait2 := make(chan struct{}, 1)
goclock.Mock(now)
go func() {
runFunc()
wait1 <- struct{}{}
<-wait2
}()
go func() {
runFunc()
wait2 <- struct{}{}
<-wait1
}()
goclock.Restore()
}
func testInRace(runFunc func()) {
goclock.Restore()
wait1 := make(chan struct{}, 1)
wait2 := make(chan struct{}, 1)
go func() {
runFunc()
wait1 <- struct{}{}
<-wait2
}()
go func() {
runFunc()
wait2 <- struct{}{}
<-wait1
}()
}