-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_signaller_test.go
62 lines (51 loc) · 1.32 KB
/
test_signaller_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
package lameduck
import (
"os"
)
// NOTE: This file contains no tests of its own.
//
// Instead, it contains the testSignaller implemenation for emitting fake
// signals into the main code base without the need for actually using
// real (OS level) signaling.
//
type testSignaller struct {
orig signaler
sigs map[os.Signal]bool
ch chan<- os.Signal
}
// injectSignaller injects and returns a new testSignaller into the main codebase.
func injectSignaller() *testSignaller {
ts := &testSignaller{orig: sig}
sig = ts
return ts
}
// emit sends a Signal through the testSignaller.
func (ts *testSignaller) emit(s os.Signal) {
if ts == nil || ts.ch == nil || ts.sigs == nil || !ts.sigs[s] {
return
}
go func() { ts.ch <- s }()
}
// revert replaces an injected testSignaller with the signaler in place at the
// time of injection.
func (ts *testSignaller) revert() {
if ts != nil && ts.orig != nil {
sig = ts.orig
}
}
// notify contributes to the signaler interface
func (ts *testSignaller) notify(c chan<- os.Signal, sig ...os.Signal) {
ts.ch = c
if ts.sigs == nil {
ts.sigs = make(map[os.Signal]bool)
}
for _, s := range sig {
ts.sigs[s] = true
}
}
// stop contributes to the signaler interface
func (ts *testSignaller) stop(c chan<- os.Signal) {
if ts != nil && ts.ch == c {
ts.sigs = nil
}
}