-
Notifications
You must be signed in to change notification settings - Fork 82
/
group_test.go
57 lines (52 loc) · 1.2 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
package run_test
import (
"errors"
"testing"
"time"
"github.com/oklog/run"
)
func TestZero(t *testing.T) {
var g run.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 run.Group
g.Add(func() error { return myError }, func(error) {})
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 run.Group
g.Add(func() error { return interrupt }, func(error) {})
cancel := make(chan struct{})
g.Add(func() error { <-cancel; return nil }, func(error) { close(cancel) })
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")
}
}