-
Notifications
You must be signed in to change notification settings - Fork 2
/
gently_test.go
69 lines (53 loc) · 1.4 KB
/
gently_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
package gently
import (
"fmt"
"log"
"os"
"testing"
"time"
)
type testStruct struct {
name string
}
func (ts *testStruct) GetName() string {
return fmt.Sprintf("testStruct '%s'", ts.name)
}
func (ts *testStruct) StopGently(signal os.Signal) {
log.Printf("gently stopping test struct '%s'...", ts.name)
}
func TestInterruptSuccess(t *testing.T) {
defer handleTestPanics(t)
queueUpArtificialSignalAfterDelay(os.Interrupt, 500)
t.Log("tested artificial interrupt signal")
}
func TestKillSuccess(t *testing.T) {
defer handleTestPanics(t)
queueUpArtificialSignalAfterDelay(os.Kill, 500)
t.Log("tested artificial kill signal")
}
func createTestStruct() *testStruct {
return &testStruct{
name: "A Test",
}
}
func handleTestPanics(t *testing.T) {
recovered := recover()
if recovered != nil {
t.Errorf("Recovered from a panic: %v", recovered)
} else {
t.Log("No panics, all good...")
}
}
func queueUpArtificialSignalAfterDelay(signal os.Signal, delay int) {
theTestStruct := createTestStruct()
goodnight := New()
goodnight.Register(theTestStruct)
go sendSignalAfterDelay(goodnight, signal, delay)
// wait for all of the registered structs to be notified
goodnight.Wait()
}
func sendSignalAfterDelay(goodnight *GoodNight, signal os.Signal, delay int) {
// send specified signal after specified delay
time.Sleep(time.Duration(delay) * time.Millisecond)
goodnight.signalListener <- signal
}