-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarter_test.go
66 lines (52 loc) · 1.28 KB
/
starter_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
package starter
import (
"math/rand"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestNotBlocking(t *testing.T) {
Ready().Steady(0).Go()
}
func TestOneRunner(t *testing.T) {
p := Ready()
start := time.Now()
done := make(chan struct{})
go func() {
time.Sleep(2 * time.Second)
p.Wait()
close(done)
}()
now := p.Steady(1).Go()
require.GreaterOrEqual(t, now.Sub(start), time.Second)
<-done // this will time out if the channel has not been closed
}
func TestNRunners(t *testing.T) {
counts := []int{1, 10, 100, 1_000, 10_000, 100_000, 1_000_000}
for _, count := range counts {
count := count
t.Run(strconv.Itoa(count), func(t *testing.T) {
t.Parallel() // these tests take a long time, run them in parallel
p := Ready()
wg := &sync.WaitGroup{}
wg.Add(count)
t.Logf("Starting %d runners...", count)
for i := 0; i < count; i++ {
go func() {
defer wg.Done()
// sleep between 1 and 3 seconds
time.Sleep(time.Duration(rand.Int63n(int64(2*time.Second))) + time.Second)
p.Wait()
}()
}
t.Logf("Started %d runners...", count)
p.Steady(count)
t.Logf("%d runners ready", count)
start := p.Go()
wg.Wait()
t.Logf("Time to unlock %d runners: %s", count, time.Since(start))
})
}
}