-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
85 lines (72 loc) · 2.16 KB
/
manager_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package sup
import (
"fmt"
"sort"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestManager(t *testing.T) {
Convey("Given a Manager", t, func() {
rootWrit := NewTask()
rootWrit.Run(func(super Supervisor) {
mgr := NewManager(super)
Convey("And some serially executing tasks", func() {
ch := make(chan string, 2)
mgr.NewTask("1").Run(ChanWriterAgent("1", ch))
mgr.NewTask("2").Run(ChanWriterAgent("2", ch))
Convey("Tasks run and we see their sideeffects", func() {
So(<-ch, ShouldEqual, "1")
So(<-ch, ShouldEqual, "2")
})
Convey("Manager.Work should gather all the things", func() {
mgr.Work()
// maybe not the most useful test
So(mgr.(*manager).doneFuse.IsBlown(), ShouldBeTrue)
So(mgr.(*manager).wards, ShouldHaveLength, 0)
})
})
Convey("And some parallel executing tasks", func() {
ch := make(chan string, 2)
go mgr.NewTask("1").Run(ChanWriterAgent("1", ch))
go mgr.NewTask("2").Run(ChanWriterAgent("2", ch))
Convey("Tasks run and we see their sideeffects", func() {
results := []string{<-ch, <-ch}
sort.Strings(results)
So(results[0], ShouldEqual, "1")
So(results[1], ShouldEqual, "2")
})
Convey("Manager.Work should gather all the things", func() {
mgr.Work()
// maybe not the most useful test
So(mgr.(*manager).doneFuse.IsBlown(), ShouldBeTrue)
So(mgr.(*manager).wards, ShouldHaveLength, 0)
})
})
Convey("And some exploding tasks!", func() {
ch := make(chan string, 0)
explo := fmt.Errorf("bang!")
go mgr.NewTask("1").Run(ChanWriterAgent("1", ch))
go mgr.NewTask("2").Run(ChanWriterAgent("2", ch))
go mgr.NewTask("e").Run(ExplosiveAgent(explo))
Convey("Manager.Work should raise the panic", func() {
So(mgr.Work, ShouldPanic)
So(mgr.(*manager).doneFuse.IsBlown(), ShouldBeTrue)
So(mgr.(*manager).wards, ShouldHaveLength, 0)
})
})
})
})
}
func ChanWriterAgent(msg string, ch chan<- string) Agent {
return func(supvr Supervisor) {
select {
case ch <- msg:
case <-supvr.QuitCh():
}
}
}
func ExplosiveAgent(err error) Agent {
return func(Supervisor) {
panic(err)
}
}