forked from oklog/run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
59 lines (55 loc) · 1.3 KB
/
group_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
package run
import (
"context"
"errors"
"testing"
"time"
)
func TestZero(t *testing.T) {
var g group
res := make(chan error)
go func() { res <- g.run() }()
select {
case err := <-res:
if err != nil {
t.Errorf("%v", err)
}
case <-time.After(100 * time.Millisecond):
t.Error("timeout")
}
}
func TestOne(t *testing.T) {
myError := errors.New("foobar")
var g group
g.add(func(context.Context) error { return myError }, func(context.Context) error { return nil })
res := make(chan error)
go func() { res <- g.run() }()
select {
case err := <-res:
if want, have := myError, err; want != have {
t.Errorf("want %v, have %v", want, have)
}
case <-time.After(100 * time.Millisecond):
t.Error("timeout")
}
}
func TestMany(t *testing.T) {
interrupt := errors.New("interrupt")
var g group
g.add(func(context.Context) error { return interrupt }, func(context.Context) error { return nil })
cancel := make(chan struct{})
g.add(func(context.Context) error { <-cancel; return nil }, func(context.Context) error {
close(cancel)
return nil
})
res := make(chan error)
go func() { res <- g.run() }()
select {
case err := <-res:
if want, have := interrupt, err; want != have {
t.Errorf("want %v, have %v", want, have)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("timeout")
}
}