This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxsignal_test.go
131 lines (98 loc) · 2.63 KB
/
ctxsignal_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
package ctxsignal_test
import (
"context"
"fmt"
"os"
"syscall"
"testing"
"time"
"github.com/henvic/ctxsignal"
)
func ExampleWithSignals() {
ctx, cancel := ctxsignal.WithSignals(context.Background(), syscall.SIGUSR2)
defer cancel()
// Remove this Go routine to test manually with kill -USR2 PID.
go func() {
p, _ := os.FindProcess(os.Getpid()) // always check your errors!
_ = p.Signal(syscall.SIGUSR2) // don't ignore errors!
}()
<-ctx.Done()
fmt.Println("Signaled!")
// Output: Signaled!
}
func ExampleWithTermination() {
ctx, cancel := ctxsignal.WithTermination(context.Background())
defer cancel()
// Remove this Go routine to test manually.
go func() {
p, _ := os.FindProcess(os.Getpid()) // always check your errors!
_ = p.Signal(syscall.SIGINT) // don't ignore errors!
}()
<-ctx.Done()
fmt.Println("Interrupted!")
// Output: Interrupted!
}
func ExampleClosed() {
ctx, cancel := ctxsignal.WithSignals(context.Background(), syscall.SIGHUP)
defer cancel()
// Remove this Go routine to test manually with kill -HUP PID.
go func() {
p, _ := os.FindProcess(os.Getpid()) // always check your errors!
_ = p.Signal(syscall.SIGHUP) // don't ignore errors!
}()
<-ctx.Done()
sig, err := ctxsignal.Closed(ctx)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(sig)
// Output: hangup
}
func TestWithTermination(t *testing.T) {
testWithTermination(t, syscall.SIGINT)
testWithTermination(t, syscall.SIGTERM)
}
func testWithTermination(t *testing.T, sig os.Signal) {
ctx, _ := ctxsignal.WithTermination(context.Background())
go selfSignal(sig)
<-ctx.Done()
}
func selfSignal(s os.Signal) {
p, err := os.FindProcess(os.Getpid())
if err != nil {
panic(err)
}
time.Sleep(5 * time.Millisecond)
if err := p.Signal(s); err != nil {
panic(err)
}
}
func TestWithSignals(t *testing.T) {
ctx, _ := ctxsignal.WithSignals(context.Background(), syscall.SIGUSR1)
go selfSignal(syscall.SIGUSR1)
<-ctx.Done()
sig, err := ctxsignal.Closed(ctx)
if err != nil {
t.Error("Expected context to be closed by signal")
}
if sig != syscall.SIGUSR1 {
t.Errorf("Expected context to be closed by %v, got %v instead", syscall.SIGUSR1, sig)
}
}
func TestWithSignalsCanceled(t *testing.T) {
ctx, cancel := ctxsignal.WithSignals(context.Background(), syscall.SIGUSR1)
go func() {
cancel()
}()
<-ctx.Done()
if _, err := ctxsignal.Closed(ctx); err == nil {
t.Error("Expected error, got nil instead")
}
}
func TestNoClosed(t *testing.T) {
ctx := context.Background()
if _, err := ctxsignal.Closed(ctx); err == nil {
t.Error("Expected error, got nil instead")
}
}