...
1
16
17 package async
18
19 import (
20 "fmt"
21 "sync"
22 "testing"
23 )
24
25 func TestRunner(t *testing.T) {
26 var (
27 lock sync.Mutex
28 events []string
29 funcs []func(chan struct{})
30 )
31 done := make(chan struct{}, 20)
32 for i := 0; i < 10; i++ {
33 iCopy := i
34 funcs = append(funcs, func(c chan struct{}) {
35 lock.Lock()
36 events = append(events, fmt.Sprintf("%v starting\n", iCopy))
37 lock.Unlock()
38 <-c
39 lock.Lock()
40 events = append(events, fmt.Sprintf("%v stopping\n", iCopy))
41 lock.Unlock()
42 done <- struct{}{}
43 })
44 }
45
46 r := NewRunner(funcs...)
47 r.Start()
48 r.Stop()
49 for i := 0; i < 10; i++ {
50 <-done
51 }
52 if len(events) != 20 {
53 t.Errorf("expected 20 events, but got:\n%v\n", events)
54 }
55 }
56
View as plain text