-
Notifications
You must be signed in to change notification settings - Fork 20
/
death_unix_test.go
221 lines (185 loc) · 4.88 KB
/
death_unix_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// +build linux bsd darwin
package death
import (
"fmt"
"os"
"syscall"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
type Unhashable map[string]interface{}
func (u Unhashable) Close() error {
return nil
}
func TestDeath(t *testing.T) {
Convey("Validate death handles unhashable types", t, func() {
u := make(Unhashable)
death := NewDeath(syscall.SIGTERM)
syscall.Kill(os.Getpid(), syscall.SIGTERM)
err := death.WaitForDeath(u)
So(err, ShouldBeNil)
})
Convey("Validate death happens cleanly", t, func() {
death := NewDeath(syscall.SIGTERM)
syscall.Kill(os.Getpid(), syscall.SIGTERM)
err := death.WaitForDeath()
So(err, ShouldBeNil)
})
Convey("Validate death happens with other signals", t, func() {
death := NewDeath(syscall.SIGHUP)
closeMe := &CloseMe{}
syscall.Kill(os.Getpid(), syscall.SIGHUP)
err := death.WaitForDeath(closeMe)
So(err, ShouldBeNil)
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Validate death happens with a manual call", t, func() {
death := NewDeath(syscall.SIGHUP)
closeMe := &CloseMe{}
death.FallOnSword()
err := death.WaitForDeath(closeMe)
So(err, ShouldBeNil)
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Validate multiple sword falls do not block", t, func() {
death := NewDeath(syscall.SIGHUP)
closeMe := &CloseMe{}
death.FallOnSword()
death.FallOnSword()
err := death.WaitForDeath(closeMe)
So(err, ShouldBeNil)
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Validate multiple sword falls do not block even after we have exited waitForDeath", t, func() {
death := NewDeath(syscall.SIGHUP)
closeMe := &CloseMe{}
death.FallOnSword()
death.FallOnSword()
err := death.WaitForDeath(closeMe)
So(err, ShouldBeNil)
death.FallOnSword()
death.FallOnSword()
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Validate death gives up after timeout", t, func() {
death := NewDeath(syscall.SIGHUP)
death.SetTimeout(10 * time.Millisecond)
neverClose := &neverClose{}
syscall.Kill(os.Getpid(), syscall.SIGHUP)
err := death.WaitForDeath(neverClose)
So(err, ShouldNotBeNil)
})
Convey("Validate death uses new logger", t, func() {
death := NewDeath(syscall.SIGHUP)
closeMe := &CloseMe{}
logger := &MockLogger{}
death.SetLogger(logger)
syscall.Kill(os.Getpid(), syscall.SIGHUP)
err := death.WaitForDeath(closeMe)
So(err, ShouldBeNil)
So(closeMe.Closed, ShouldEqual, 1)
So(logger.Logs, ShouldNotBeEmpty)
})
Convey("Close multiple things with one that fails the timer", t, func() {
death := NewDeath(syscall.SIGHUP)
death.SetTimeout(10 * time.Millisecond)
neverClose := &neverClose{}
closeMe := &CloseMe{}
syscall.Kill(os.Getpid(), syscall.SIGHUP)
err := death.WaitForDeath(neverClose, closeMe)
So(err, ShouldNotBeNil)
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Close with anonymous function", t, func() {
death := NewDeath(syscall.SIGHUP)
death.SetTimeout(5 * time.Millisecond)
closeMe := &CloseMe{}
syscall.Kill(os.Getpid(), syscall.SIGHUP)
death.WaitForDeathWithFunc(func() {
closeMe.Close()
So(true, ShouldBeTrue)
})
So(closeMe.Closed, ShouldEqual, 1)
})
Convey("Validate death errors when closer returns error", t, func() {
death := NewDeath(syscall.SIGHUP)
killMe := &KillMe{}
death.FallOnSword()
err := death.WaitForDeath(killMe)
So(err, ShouldNotBeNil)
})
}
func TestGenerateErrString(t *testing.T) {
Convey("Generate for multiple errors", t, func() {
closers := []closer{
closer{
Err: fmt.Errorf("error 1"),
Name: "foo",
PKGPath: "my/pkg",
},
closer{
Err: fmt.Errorf("error 2"),
Name: "bar",
PKGPath: "my/otherpkg",
},
}
expected := "my/pkg/foo: error 1, my/otherpkg/bar: error 2"
actual := generateErrString(closers)
So(actual, ShouldEqual, expected)
})
Convey("Generate for single error", t, func() {
closers := []closer{
closer{
Err: fmt.Errorf("error 1"),
Name: "foo",
PKGPath: "my/pkg",
},
}
expected := "my/pkg/foo: error 1"
actual := generateErrString(closers)
So(actual, ShouldEqual, expected)
})
}
type MockLogger struct {
Logs []interface{}
}
func (l *MockLogger) Info(v ...interface{}) {
for _, log := range v {
l.Logs = append(l.Logs, log)
}
}
func (l *MockLogger) Debug(v ...interface{}) {
for _, log := range v {
l.Logs = append(l.Logs, log)
}
}
func (l *MockLogger) Error(v ...interface{}) {
for _, log := range v {
l.Logs = append(l.Logs, log)
}
}
func (l *MockLogger) Warn(v ...interface{}) {
for _, log := range v {
l.Logs = append(l.Logs, log)
}
}
type neverClose struct {
}
func (n *neverClose) Close() error {
time.Sleep(2 * time.Minute)
return nil
}
// CloseMe returns nil from close
type CloseMe struct {
Closed int
}
func (c *CloseMe) Close() error {
c.Closed++
return nil
}
// KillMe returns an error from close
type KillMe struct{}
func (c *KillMe) Close() error {
return fmt.Errorf("error from closer")
}