-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhitman_test.go
145 lines (123 loc) · 2.62 KB
/
hitman_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package hitman
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
"fmt"
)
func TestName(t *testing.T) {
Convey("Should kill them one.", t, func() {
hits := NewTargets()
hits.Add(NewWorker())
hits.Add(NewWorker())
hits.Add(NewWorker())
hits.Add(NewWorker())
So(len(hits), ShouldEqual, 4)
err := hits.Close()
So(err, ShouldBeNil)
})
Convey("New kill channels should have a length of 0.", t, func() {
k := NewKillChannel()
So(len(k), ShouldEqual, 0)
})
Convey("Should kill them one.", t, func() {
hits := NewTargets()
t := &Worker{}
hits.Add(t)
So(len(hits), ShouldEqual, 1)
err := hits.Close()
So(err, ShouldBeNil)
})
Convey("New Targets collection should have len of zero", t, func() {
t := NewTargets()
w := NewWorker()
t.Add(w)
v,ok := t[w.Name()]
So(len(t), ShouldEqual, 1)
So(v, ShouldNotBeNil)
So(ok, ShouldBeTrue)
})
Convey("New Targets collection should have len of zero", t, func() {
t := NewTargets()
So(len(t), ShouldEqual, 0)
})
Convey("New Target via literals", t, func() {
t := NewTargets()
t.AddTarget(&Unnamed{})
So(len(t), ShouldEqual, 1)
t.Close()
})
Convey("New Target or panic should be fine if no error", t, func() {
t := NewTargets()
t.AddOrPanic(NewService())
So(len(t), ShouldEqual, 1)
t.Close()
})
Convey("Should panic when adding a service that failed to instantiate without error", t, func() {
defer func() {
panicky := recover()
err, ok := panicky.(error)
So(ok, ShouldBeTrue)
So(err, ShouldNotBeNil)
}()
NewTargets().AddOrPanic(NewServiceWithError())
})
}
type Service struct {}
func NewServiceWithError() (*Service, error) {
s := &Service{}
return s, fmt.Errorf("Had error while creating new service")
}
func NewService() (*Service, error) {
s := &Service{}
return s, nil
}
func (w *Service) Start() KillChannel {
kill := NewKillChannel()
go func(done KillChannel) {
select {
case cleaner := <-done:
cleaner.WaitGroup.Done()
return
}
}(kill)
return kill
}
type Unnamed struct {}
func (w *Unnamed) Start() KillChannel {
kill := NewKillChannel()
go func(done KillChannel) {
select {
case cleaner := <-done:
cleaner.WaitGroup.Done()
return
}
}(kill)
return kill
}
var id = 0
func newId() int {
id++
return id
}
type Worker struct {
id int
}
func NewWorker() *Worker {
return &Worker{
id: newId(),
}
}
func (w *Worker) Name() string {
return fmt.Sprintf("Work IT!!, %d", w.id)
}
func (w *Worker) Start() KillChannel {
kill := NewKillChannel()
go func(done KillChannel) {
select {
case cleaner := <-done:
cleaner.WaitGroup.Done()
return
}
}(kill)
return kill
}