-
Notifications
You must be signed in to change notification settings - Fork 0
/
onces_test.go
85 lines (73 loc) · 1.41 KB
/
onces_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
package xsync_test
import (
"testing"
"time"
"golang.org/x/sync/errgroup"
"github.com/dushaoshuai/xsync"
)
func FuzzDo(f *testing.F) {
defaultI := 1_00_000
f.Add(time.Nanosecond.Nanoseconds(), defaultI)
f.Add(time.Microsecond.Nanoseconds(), defaultI)
f.Add(time.Millisecond.Nanoseconds(), defaultI)
f.Add(time.Second.Nanoseconds(), defaultI)
f.Add(3*time.Second.Nanoseconds(), defaultI)
f.Fuzz(func(t *testing.T, dNano int64, i int) {
if dNano > 5*time.Second.Nanoseconds() {
t.SkipNow()
}
if dNano <= 0 {
t.SkipNow()
}
if i > 1_00_000 {
t.SkipNow()
}
if i <= 0 {
t.SkipNow()
}
var (
interval = time.Duration(dNano)
onces = xsync.OnceInterval(interval)
c = make(chan time.Time)
prev time.Time
fail = make(chan struct{})
succ = make(chan struct{})
eg errgroup.Group
)
go func() {
L:
for j := 0; j < i; j++ {
select {
case <-fail:
break L
default:
}
eg.Go(func() error {
return onces.Do(func() error {
c <- time.Now()
return nil
})
})
}
eg.Wait()
close(c)
}()
go func() {
select {
case <-fail:
for range c {
}
case <-succ:
}
}()
for curr := range c {
if !prev.IsZero() && prev.Add(interval).After(curr) {
close(fail)
eg.Wait()
t.Fatalf("(%v).Add(%v).After(%v) = %v, want %v", prev, interval, curr, true, false)
}
prev = curr
}
close(succ)
})
}