-
Notifications
You must be signed in to change notification settings - Fork 4
/
timeout_test.go
244 lines (228 loc) · 5.97 KB
/
timeout_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package timeout
import (
"context"
"fmt"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
"syscall"
"testing"
"time"
)
var (
shellcmd = "/bin/sh"
shellflag = "-c"
stubCmd = "./testdata/stubcmd"
)
func init() {
if runtime.GOOS == "windows" {
shellcmd = "cmd"
shellflag = "/c"
stubCmd = `.\testdata\stubcmd.exe`
}
err := exec.Command("go", "build", "-o", stubCmd, "testdata/stubcmd.go").Run()
if err != nil {
panic(err)
}
}
func TestRun(t *testing.T) {
tio := &Timeout{
Duration: 10 * time.Second,
Cmd: exec.Command(shellcmd, shellflag, "echo 1"),
}
_, stdout, stderr, err := tio.Run()
if strings.TrimSpace(stdout) != "1" {
t.Errorf("something wrong")
}
if stderr != "" {
t.Errorf("something wrong")
}
if err != nil {
t.Errorf("something wrong: %v", err)
}
}
var isWin = runtime.GOOS == "windows"
func TestRunSimple(t *testing.T) {
testCases := []struct {
name string
duration time.Duration
killAfter time.Duration
cmd *exec.Cmd
signal os.Signal
preserveStatus bool
expectedExit int
skipOnWin bool
}{
{
name: "simple echo",
duration: time.Duration(0.1 * float64(time.Second)),
cmd: exec.Command(shellcmd, shellflag, "echo 1"),
expectedExit: 0,
},
{
name: "timed out",
cmd: exec.Command(shellcmd, shellflag, fmt.Sprintf("%s -sleep 3", stubCmd)),
duration: 100 * time.Millisecond,
signal: os.Interrupt,
expectedExit: 124,
},
{
name: "timed out with preserve status",
cmd: exec.Command(shellcmd, shellflag, fmt.Sprintf("%s -sleep 3", stubCmd)),
duration: time.Duration(0.1 * float64(time.Second)),
preserveStatus: true,
expectedExit: 128 + int(syscall.SIGTERM),
skipOnWin: true,
},
{
name: "preserve status (signal trapd)",
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-trap-exit", "23", "-sleep", "3"),
duration: 100 * time.Millisecond,
preserveStatus: true,
expectedExit: 23,
skipOnWin: true,
},
{
name: "kill after",
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-sleep", "3"),
duration: 100 * time.Millisecond,
killAfter: 100 * time.Microsecond,
signal: syscall.SIGTERM,
expectedExit: exitKilled,
},
{
name: "trap sigterm but exited before kill after",
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-sleep", "0.8"),
duration: 100 * time.Millisecond,
killAfter: 5 * time.Second,
signal: syscall.SIGTERM,
preserveStatus: true,
expectedExit: 0,
},
{
name: "command cannnot be invoked",
cmd: exec.Command("testdata/dummy"),
duration: 1 * time.Second,
expectedExit: 126, // TODO cmd should return 125 on win
skipOnWin: true,
},
{
name: "command cannnot be invoked (command not found)",
cmd: exec.Command("testdata/command-not-found"),
duration: 1 * time.Second,
expectedExit: 127, // TODO cmd should return 125 on win
skipOnWin: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipOnWin && isWin {
t.Skipf("%s: skip on windows", tc.name)
}
tio := &Timeout{
Duration: tc.duration,
KillAfter: tc.killAfter,
Cmd: tc.cmd,
Signal: tc.signal,
}
exit := tio.RunSimple(tc.preserveStatus)
if exit != tc.expectedExit {
t.Errorf("%s: expected exitcode: %d, but: %d", tc.name, tc.expectedExit, exit)
}
})
}
}
func TestRunContext(t *testing.T) {
expect := ExitStatus{
Code: 128 + int(syscall.SIGTERM),
Signaled: true,
typ: exitTypeCanceled,
killed: false,
}
if isWin {
expect = ExitStatus{
Code: 1,
Signaled: false,
typ: exitTypeCanceled,
killed: true,
}
}
t.Run("cancel", func(t *testing.T) {
tio := &Timeout{
Duration: 3 * time.Second,
Cmd: exec.Command(stubCmd, "-sleep", "10"),
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
st, err := tio.RunContext(ctx)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if !reflect.DeepEqual(expect, *st) {
t.Errorf("invalid exit status\n out: %v\nexpect: %v", *st, expect)
}
})
t.Run("with timeout", func(t *testing.T) {
tio := &Timeout{
Duration: 3 * time.Second,
Cmd: exec.Command(stubCmd, "-sleep", "10"),
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
st, err := tio.RunContext(ctx)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
if !reflect.DeepEqual(expect, *st) {
t.Errorf("invalid exit status\n out: %v\nexpect: %v", *st, expect)
}
})
t.Run("with timeout and signal trapped", func(t *testing.T) {
if isWin {
t.Skip("skip on windows")
}
tio := &Timeout{
Duration: 3 * time.Second,
Cmd: exec.Command(stubCmd, "-sleep", "10", "-trap", "SIGTERM"),
KillAfterCancel: time.Duration(10 * time.Millisecond),
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
st, err := tio.RunContext(ctx)
if err != nil {
t.Errorf("error should be nil but: %s", err)
}
expect := ExitStatus{
Code: exitKilled,
Signaled: true,
typ: exitTypeCanceled,
killed: true,
}
if !reflect.DeepEqual(expect, *st) {
t.Errorf("invalid exit status\n out: %v\nexpect: %v", *st, expect)
}
})
}
func TestRun_leak(t *testing.T) {
beforeGoroutine := runtime.NumGoroutine()
for range make([]struct{}, 30) {
tio := &Timeout{
Cmd: exec.Command(stubCmd, "-sleep=0.1"),
Duration: time.Second,
KillAfter: time.Second,
}
go func() {
tio.Run()
}()
}
time.Sleep(time.Second)
afterGoroutine := runtime.NumGoroutine()
if beforeGoroutine+10 < afterGoroutine {
t.Errorf("goroutine may be leaked. before: %d, after: %d", beforeGoroutine, afterGoroutine)
}
}