forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rungroup_test.go
61 lines (52 loc) · 1.13 KB
/
rungroup_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
package kafka
import (
"context"
"testing"
"time"
)
func TestRunGroup(t *testing.T) {
t.Run("Wait returns on empty group", func(t *testing.T) {
rg := &runGroup{}
rg.Wait()
})
t.Run("Stop returns on empty group", func(t *testing.T) {
rg := &runGroup{}
rg.Stop()
})
t.Run("Stop cancels running tasks", func(t *testing.T) {
rg := &runGroup{}
rg.Go(func(stop <-chan struct{}) {
<-stop
})
rg.Stop()
})
t.Run("Honors parent context", func(t *testing.T) {
now := time.Now()
timeout := time.Millisecond * 100
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
rg := &runGroup{}
rg = rg.WithContext(ctx)
rg.Go(func(stop <-chan struct{}) {
<-stop
})
rg.Wait()
elapsed := time.Now().Sub(now)
if elapsed < timeout {
t.Errorf("expected elapsed > %v; got %v", timeout, elapsed)
}
})
t.Run("Any death kills all; one for all and all for one", func(t *testing.T) {
rg := &runGroup{}
rg.Go(func(stop <-chan struct{}) {
<-stop
})
rg.Go(func(stop <-chan struct{}) {
<-stop
})
rg.Go(func(stop <-chan struct{}) {
// return immediately
})
rg.Wait()
})
}