diff options
author | Shuhei Takahashi <nya@chromium.org> | 2021-03-22 00:22:23 +0900 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2021-03-23 22:36:59 +0000 |
commit | 4e27aa6cd2c3f579328e3b490780664ade34053d (patch) | |
tree | 49d83ca29d4600a0b50da333a1d975d17661cbab /src/os/exec/exec_test.go | |
parent | dc289d3dcb59f80b9e23c7e8f237628359d21d92 (diff) | |
download | go-4e27aa6cd2c3f579328e3b490780664ade34053d.tar.gz go-4e27aa6cd2c3f579328e3b490780664ade34053d.zip |
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 <emmanuel@orijtech.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r-- | src/os/exec/exec_test.go | 38 |
1 files changed, 5 insertions, 33 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 05cf807583..c1d2a8f49c 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -1052,41 +1052,18 @@ func TestContextCancel(t *testing.T) { defer cancel() c := helperCommandContext(t, ctx, "cat") - r, w, err := os.Pipe() - if err != nil { - t.Fatal(err) - } - c.Stdin = r - - stdout, err := c.StdoutPipe() + stdin, err := c.StdinPipe() if err != nil { t.Fatal(err) } - readDone := make(chan struct{}) - go func() { - defer close(readDone) - var a [1024]byte - for { - n, err := stdout.Read(a[:]) - if err != nil { - if err != io.EOF { - t.Errorf("unexpected read error: %v", err) - } - return - } - t.Logf("%s", a[:n]) - } - }() + defer stdin.Close() if err := c.Start(); err != nil { t.Fatal(err) } - if err := r.Close(); err != nil { - t.Fatal(err) - } - - if _, err := io.WriteString(w, "echo"); err != nil { + // At this point the process is alive. Ensure it by sending data to stdin. + if _, err := io.WriteString(stdin, "echo"); err != nil { t.Fatal(err) } @@ -1096,7 +1073,7 @@ func TestContextCancel(t *testing.T) { // should now fail. Give the process a little while to die. start := time.Now() for { - if _, err := io.WriteString(w, "echo"); err != nil { + if _, err := io.WriteString(stdin, "echo"); err != nil { break } if time.Since(start) > time.Minute { @@ -1105,11 +1082,6 @@ func TestContextCancel(t *testing.T) { time.Sleep(time.Millisecond) } - if err := w.Close(); err != nil { - t.Errorf("error closing write end of pipe: %v", err) - } - <-readDone - if err := c.Wait(); err == nil { t.Error("program unexpectedly exited successfully") } else { |