-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunction_test.go
More file actions
102 lines (81 loc) · 1.68 KB
/
junction_test.go
File metadata and controls
102 lines (81 loc) · 1.68 KB
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
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package sigmon
import (
"os"
"os/signal"
"syscall"
"testing"
)
func TestJunction(t *testing.T) {
t.Run("ConnectDisconnectSignals", tJunctionConnectDisconnectSignals)
}
func tJunctionConnectDisconnectSignals(t *testing.T) {
j := newJunction()
j.connect()
j.connect() // panics if incorrectly designed
for _, s := range osSignals {
if err := callOSSignal(s); err != nil {
t.Errorf("unexpected error when calling %s: %s", s, err)
}
}
if !isPickingUp(j.signals(), len(osSignals)) {
t.Fatalf("should not block")
}
j.disconnect()
j.disconnect() // panics if incorrectly designed
s := syscall.SIGHUP
c := make(chan os.Signal, 1)
defer close(c)
signal.Notify(c, s)
defer signal.Stop(c)
if err := callOSSignal(s); err != nil {
t.Errorf("unexpected error when calling %s: %s", s, err)
}
if isPickingUp(j.signals(), 1) {
t.Fatalf("should block")
}
select {
case <-c:
signal.Stop(c)
default:
t.Fatalf("should not block")
}
}
var (
osSignals = []syscall.Signal{
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGUSR1,
syscall.SIGUSR2,
}
)
func callOSSignal(s syscall.Signal) error {
if err := syscall.Kill(syscall.Getpid(), s); err != nil {
return err
}
// delay for signal propagation
for i := 1 << 23; i > 0; i-- {
}
return nil
}
func isPickingUp(c chan Signal, ct int) bool {
delayedChan := func() chan struct{} {
dc := make(chan struct{}, 1)
go func() {
for i := 1 << 23; i > 0; i-- {
}
dc <- struct{}{}
defer close(dc)
}()
return dc
}
for i := 0; i < ct; i++ {
select {
case <-c:
case <-delayedChan():
return false
}
}
return true
}