-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgoroutine_manager_test.go
More file actions
53 lines (43 loc) · 921 Bytes
/
Copy pathgoroutine_manager_test.go
File metadata and controls
53 lines (43 loc) · 921 Bytes
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
package fn
import (
"sync/atomic"
"testing"
"time"
)
func TestGoroutineManagerBasic(t *testing.T) {
gm := NewGoroutineManager()
var count atomic.Int32
for i := 0; i < 5; i++ {
ok := gm.Go(func(quit <-chan struct{}) {
count.Add(1)
<-quit
})
assertTrue(t, ok)
}
// Give goroutines time to start
time.Sleep(10 * time.Millisecond)
assertEqual(t, count.Load(), int32(5))
gm.Stop()
// After stop, Go should return false
ok := gm.Go(func(_ <-chan struct{}) {})
assertFalse(t, ok)
}
func TestGoroutineManagerDone(t *testing.T) {
gm := NewGoroutineManager()
select {
case <-gm.Done():
t.Fatal("Done should not be closed yet")
default:
}
gm.Stop()
select {
case <-gm.Done():
case <-time.After(time.Second):
t.Fatal("Done should be closed after Stop")
}
}
func TestGoroutineManagerDoubleStop(_ *testing.T) {
gm := NewGoroutineManager()
gm.Stop()
gm.Stop() // Should not panic.
}