Skip to content

Commit 4e27aa6

Browse files
nya3jpianlancetaylor
authored andcommitted
os/exec: simplify TestContextCancel
TestContextCancel is a test that ensures a process is killed soon after canceling the context, even if Wait is not called (#16222). The test checks whether the process exited without calling Wait by writing some data to its stdin. Currently the test involves two goroutines writing to stdin and reading from stdout. However the reading goroutine is not very necessary to detect the process exit. This patch simplifies the test by connecting the process stdout to /dev/null. For #42061 Change-Id: I0447a1c024ee5abb050c627ec3766b731b02181a Reviewed-on: https://go-review.googlesource.com/c/go/+/303352 Trust: Emmanuel Odeke <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent dc289d3 commit 4e27aa6

File tree

1 file changed

+5
-33
lines changed

1 file changed

+5
-33
lines changed

src/os/exec/exec_test.go

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,41 +1052,18 @@ func TestContextCancel(t *testing.T) {
10521052
defer cancel()
10531053
c := helperCommandContext(t, ctx, "cat")
10541054

1055-
r, w, err := os.Pipe()
1056-
if err != nil {
1057-
t.Fatal(err)
1058-
}
1059-
c.Stdin = r
1060-
1061-
stdout, err := c.StdoutPipe()
1055+
stdin, err := c.StdinPipe()
10621056
if err != nil {
10631057
t.Fatal(err)
10641058
}
1065-
readDone := make(chan struct{})
1066-
go func() {
1067-
defer close(readDone)
1068-
var a [1024]byte
1069-
for {
1070-
n, err := stdout.Read(a[:])
1071-
if err != nil {
1072-
if err != io.EOF {
1073-
t.Errorf("unexpected read error: %v", err)
1074-
}
1075-
return
1076-
}
1077-
t.Logf("%s", a[:n])
1078-
}
1079-
}()
1059+
defer stdin.Close()
10801060

10811061
if err := c.Start(); err != nil {
10821062
t.Fatal(err)
10831063
}
10841064

1085-
if err := r.Close(); err != nil {
1086-
t.Fatal(err)
1087-
}
1088-
1089-
if _, err := io.WriteString(w, "echo"); err != nil {
1065+
// At this point the process is alive. Ensure it by sending data to stdin.
1066+
if _, err := io.WriteString(stdin, "echo"); err != nil {
10901067
t.Fatal(err)
10911068
}
10921069

@@ -1096,7 +1073,7 @@ func TestContextCancel(t *testing.T) {
10961073
// should now fail. Give the process a little while to die.
10971074
start := time.Now()
10981075
for {
1099-
if _, err := io.WriteString(w, "echo"); err != nil {
1076+
if _, err := io.WriteString(stdin, "echo"); err != nil {
11001077
break
11011078
}
11021079
if time.Since(start) > time.Minute {
@@ -1105,11 +1082,6 @@ func TestContextCancel(t *testing.T) {
11051082
time.Sleep(time.Millisecond)
11061083
}
11071084

1108-
if err := w.Close(); err != nil {
1109-
t.Errorf("error closing write end of pipe: %v", err)
1110-
}
1111-
<-readDone
1112-
11131085
if err := c.Wait(); err == nil {
11141086
t.Error("program unexpectedly exited successfully")
11151087
} else {

0 commit comments

Comments
 (0)