-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbiter_test.go
146 lines (115 loc) · 2.86 KB
/
arbiter_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package arbiter
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestArbiter(t *testing.T) {
spawnRoutines := func(a *Arbiter, n uint32, before func()) {
for idx := uint32(0); idx < n; idx++ {
a.Go(func() {
<-a.Exit()
if before != nil {
before()
}
})
}
}
t.Run("new_normal", func(t *testing.T) {
t.Parallel()
a := New()
assert.NotNil(t, a)
assert.True(t, a.ShouldRun())
})
t.Run("new_with_parent", func(t *testing.T) {
t.Parallel()
p := New()
assert.NotNil(t, p)
assert.True(t, p.ShouldRun())
a := NewWithParent(p)
assert.NotNil(t, a)
assert.True(t, a.ShouldRun())
})
t.Run("new_with_shut_parent", func(t *testing.T) {
t.Parallel()
p := New()
assert.NotNil(t, p)
assert.True(t, p.ShouldRun())
p.Shutdown()
assert.False(t, p.ShouldRun())
a := NewWithParent(p)
assert.NotNil(t, a)
assert.False(t, a.ShouldRun())
})
t.Run("test_exit_order_no_parent", func(t *testing.T) {
t.Parallel()
p := New()
exitCounter := uint32(10)
spawnRoutines(p, exitCounter, func() {
atomic.AddUint32(&exitCounter, 0xFFFFFFFF)
})
p.Shutdown()
p.Join()
assert.Equal(t, uint32(0xFFFFFFFF), atomic.AddUint32(&exitCounter, 0xFFFFFFFF))
})
t.Run("test_tree", func(t *testing.T) {
t.Parallel()
p := New()
l, r := NewWithParent(p), NewWithParent(p)
spawnRoutines(p, 10, nil)
spawnRoutines(l, 9, nil)
spawnRoutines(r, 11, nil)
assert.Equal(t, int32(9), l.NumGoroutine())
assert.Equal(t, int32(11), r.NumGoroutine())
assert.Equal(t, int32(30), p.NumGoroutine())
assert.True(t, l.ShouldRun())
assert.True(t, r.ShouldRun())
p.Shutdown()
assert.False(t, l.ShouldRun())
assert.False(t, r.ShouldRun())
p.Join()
l.Join()
r.Join()
})
t.Run("test_tick_go_surge", func(t *testing.T) {
t.Parallel()
p := New()
concurrency := int32(0)
p.TickGo(func(cancel func(), deadline time.Time) {
assert.LessOrEqual(t, atomic.AddInt32(&concurrency, 1), int32(3))
time.Sleep(time.Millisecond * 600)
atomic.AddInt32(&concurrency, -1)
}, time.Millisecond*100, 3)
time.Sleep(time.Second * 3)
p.Shutdown()
p.Join()
})
t.Run("test_tick_go_period", func(t *testing.T) {
t.Parallel()
p := New()
var last time.Time
period, first := time.Millisecond*100, true
p.TickGo(func(cancel func(), deadline time.Time) {
if !first {
assert.WithinDuration(t, last.Add(period), deadline, time.Millisecond*5)
}
last, first = deadline, false
}, time.Millisecond*100, 1)
time.Sleep(time.Second)
p.Shutdown()
p.Join()
})
t.Run("hooks", func(t *testing.T) {
p := New()
prestopped, stopped := false, false
prestop := func() { prestopped = true }
stop := func() { stopped = true }
p.HookPreStop(prestop).HookStopped(stop)
spawnRoutines(p, 10, nil)
p.Shutdown()
p.Join()
assert.True(t, prestopped)
assert.True(t, stopped)
})
}