Skip to content

Commit 7c01a6c

Browse files
committed
add testdata/stubcmd and use it in testing
1 parent 48bea23 commit 7c01a6c

7 files changed

+69
-45
lines changed

testdata/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stubcmd

testdata/countup.pl

-19
This file was deleted.

testdata/exit_with_23.pl

-9
This file was deleted.

testdata/ignore_sigterm.pl

-7
This file was deleted.

testdata/stubcmd.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"os/signal"
8+
"strings"
9+
"syscall"
10+
"time"
11+
)
12+
13+
var sigmap = map[string]os.Signal{
14+
"INT": os.Interrupt,
15+
"TERM": syscall.SIGTERM,
16+
}
17+
18+
func main() {
19+
var (
20+
trap = flag.String("trap", "", "signals")
21+
exit = flag.Int("exit", 0, "exit status")
22+
trapExit = flag.Int("trap-exit", 0, "exit status when trapping signal")
23+
sleep = flag.Float64("sleep", 0, "sleep seconds")
24+
)
25+
flag.Parse()
26+
27+
if *trap != "" {
28+
var sigs []os.Signal
29+
for _, sigStr := range strings.Split(*trap, ",") {
30+
sig, ok := sigmap[strings.TrimPrefix(strings.ToUpper(sigStr), "SIG")]
31+
if !ok {
32+
log.Printf("unknown signal name: %s\n", sigStr)
33+
os.Exit(1)
34+
}
35+
sigs = append(sigs, sig)
36+
}
37+
c := make(chan os.Signal, 1)
38+
signal.Notify(c, sigs...)
39+
go func() {
40+
for _ = range c {
41+
if *trapExit > 0 {
42+
os.Exit(*trapExit)
43+
}
44+
}
45+
}()
46+
}
47+
if *sleep > 0 {
48+
time.Sleep(time.Duration(float64(time.Second) * *sleep))
49+
}
50+
os.Exit(*exit)
51+
}

timeout_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package timeout
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"runtime"
@@ -15,11 +16,17 @@ var (
1516
shellflag = "-c"
1617
)
1718

19+
const stubCmd = "testdata/stubcmd"
20+
1821
func init() {
1922
if runtime.GOOS == "windows" {
2023
shellcmd = "cmd"
2124
shellflag = "/c"
2225
}
26+
err := exec.Command("go", "build", "-o", stubCmd, "testdata/stubcmd.go").Run()
27+
if err != nil {
28+
panic(err)
29+
}
2330
}
2431

2532
func TestRun(t *testing.T) {
@@ -63,38 +70,38 @@ func TestRunSimple(t *testing.T) {
6370
},
6471
{
6572
name: "timed out",
66-
cmd: exec.Command(shellcmd, shellflag, "sleep 3"),
73+
cmd: exec.Command(shellcmd, shellflag, fmt.Sprintf("%s -sleep 3", stubCmd)),
6774
duration: 1 * time.Second,
6875
signal: os.Interrupt,
6976
expectedExit: 124,
7077
},
7178
{
7279
name: "timed out with preserve status",
73-
cmd: exec.Command(shellcmd, shellflag, "sleep 3"),
80+
cmd: exec.Command(shellcmd, shellflag, fmt.Sprintf("%s -sleep 3", stubCmd)),
7481
duration: time.Duration(0.1 * float64(time.Second)),
7582
preserveStatus: true,
7683
expectedExit: 128 + 15,
7784
skipOnWin: true,
7885
},
7986
{
80-
name: "preserve status (signal handled)",
81-
cmd: exec.Command("perl", "testdata/exit_with_23.pl"),
87+
name: "preserve status (signal trapd)",
88+
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-trap-exit", "23", "-sleep", "3"),
8289
duration: 1 * time.Second,
8390
preserveStatus: true,
8491
expectedExit: 23,
8592
skipOnWin: true,
8693
},
8794
{
8895
name: "kill after",
89-
cmd: exec.Command("perl", "testdata/ignore_sigterm.pl"),
96+
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-sleep", "3"),
9097
duration: 1 * time.Second,
9198
killAfter: 1 * time.Second,
9299
signal: syscall.SIGTERM,
93100
expectedExit: exitKilled,
94101
},
95102
{
96-
name: "ignore sigterm but exited before kill after",
97-
cmd: exec.Command("perl", "testdata/ignore_sigterm.pl"),
103+
name: "trap sigterm but exited before kill after",
104+
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-sleep", "3"),
98105
duration: 1 * time.Second,
99106
killAfter: 5 * time.Second,
100107
signal: syscall.SIGTERM,
@@ -109,8 +116,8 @@ func TestRunSimple(t *testing.T) {
109116
skipOnWin: true,
110117
},
111118
{
112-
name: "command cannnot be invoked",
113-
cmd: exec.Command("testdata/ignore_sigterm.pl-xxxxxxxxxxxxxxxxxxxxx"),
119+
name: "command cannnot be invoked (command not found)",
120+
cmd: exec.Command("testdata/command-not-found"),
114121
duration: 1 * time.Second,
115122
expectedExit: 127, // TODO cmd should return 125 on win
116123
skipOnWin: true,

timeout_unix_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestRunCommand_signaled(t *testing.T) {
3737
}{
3838
{
3939
name: "signal handled",
40-
cmd: exec.Command("perl", "testdata/exit_with_23.pl"),
40+
cmd: exec.Command(stubCmd, "-trap", "SIGTERM", "-trap-exit", "23", "-sleep", "3"),
4141
exit: 23,
4242
signaled: false,
4343
},

0 commit comments

Comments
 (0)