-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_event_test.go
97 lines (77 loc) · 2.01 KB
/
test_event_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
package lameduck
import (
"os"
"time"
)
// NOTE: This file contains no tests of its own.
//
// Instead it provides the definitions for testEvent and evtType. These
// are used by the mkCase function to generate test cases from a series
// of ordered events.
//
type evtType int
const (
evtNone evtType = iota
evtCancelContext
evtCloseReturn
evtLameDuckExpires
evtSendSignal
evtServeReturn
evtShutdownReturn
evtWantError
)
type testEvent struct {
etype evtType
signal os.Signal
err error
}
var (
lameDuckExpires = testEvent{etype: evtLameDuckExpires}
cancelContext = testEvent{etype: evtCancelContext}
)
func closeReturn(err error) testEvent { return testEvent{etype: evtCloseReturn, err: err} }
func sendSignal(sig os.Signal) testEvent { return testEvent{etype: evtSendSignal, signal: sig} }
func serveReturn(err error) testEvent { return testEvent{etype: evtServeReturn, err: err} }
func shutdownReturn(err error) testEvent { return testEvent{etype: evtShutdownReturn, err: err} }
func wantError(err error) testEvent { return testEvent{etype: evtWantError, err: err} }
// mkCase converts a series of ordered testEvents into a testcase.
func mkCase(evts ...testEvent) *testcase {
tc := new(testcase)
var ld time.Duration
gap := 10 * time.Millisecond
after := gap
for _, e := range evts {
switch e.etype {
case evtCancelContext:
tc.cancelAfter = after
after += gap
case evtCloseReturn:
tc.closeError = e.err
case evtLameDuckExpires:
ld = after
after += gap
case evtSendSignal:
tc.signal = e.signal
tc.signalAfter = after
after += gap
case evtServeReturn:
tc.serveError = e.err
case evtShutdownReturn:
tc.shutdownError = e.err
tc.shutdownAfter = after
after += gap
if ld != 0 {
after += gap / 2
}
case evtWantError:
tc.want = e.err
}
}
if ld < tc.signalAfter {
panic("cannot expire lame-duck before sending signal")
}
if ld > 0 {
tc.runOptions = append(tc.runOptions, Period(ld-tc.signalAfter))
}
return tc
}