]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: simplify TestContextCancel
authorShuhei Takahashi <nya@chromium.org>
Sun, 21 Mar 2021 15:22:23 +0000 (00:22 +0900)
committerIan Lance Taylor <iant@golang.org>
Tue, 23 Mar 2021 22:36:59 +0000 (22:36 +0000)
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>
src/os/exec/exec_test.go

index 05cf807583f25e68be1f36360ceac22a2d87e3f9..c1d2a8f49ca3c7e1238265295482d89971b0c528 100644 (file)
@@ -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 {