forked from nauto/xsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevery_test.go
57 lines (42 loc) · 1.03 KB
/
every_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
package xsync
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAtMostRunsAtLeastOnce(t *testing.T) {
assert := assert.New(t)
atMostOncePerHour := AtMost{Every: time.Hour}
numRuns := int32(0)
logExecution := func() {
atomic.AddInt32(&numRuns, 1)
}
// Run the first time.
atMostOncePerHour.Run(logExecution)
// Try running several more times.
go func() {
for i := 0; i != 100; i++ {
atMostOncePerHour.Run(logExecution)
}
}()
// Wait a bit to see if anything else has run.
time.Sleep(time.Second)
assert.EqualValues(atomic.LoadInt32(&numRuns), 1)
}
func TestAtMostRunsMoreThanOnce(t *testing.T) {
assert := assert.New(t)
atMostOncePerNs := AtMost{Every: time.Nanosecond}
numRuns := int32(0)
logExecution := func() {
atomic.AddInt32(&numRuns, 1)
}
// Run twice.
go func() {
atMostOncePerNs.Run(logExecution)
atMostOncePerNs.Run(logExecution)
}()
// Wait a bit to see if both runs happened.
time.Sleep(time.Second)
assert.EqualValues(atomic.LoadInt32(&numRuns), 2)
}