aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec_test.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2016-04-28 11:53:58 -0500
committerBrad Fitzpatrick <bradfitz@golang.org>2016-04-28 19:06:41 +0000
commit2cc27a7de9e7d14cb6702153688d02746c6a49ea (patch)
treea83e4b682477cef9ee6edbf440e5005bf4a47bb2 /src/os/exec/exec_test.go
parentaf125a5193c75dd59307fcf1b26d885010ce8bfd (diff)
downloadgo-2cc27a7de9e7d14cb6702153688d02746c6a49ea.tar.gz
go-2cc27a7de9e7d14cb6702153688d02746c6a49ea.zip
os/exec: add Cmd.RunContext and Cmd.WaitContext
Updates #14660 Change-Id: Ifa5c97ba327ad7ceea0a9a252e3dbd9d079dae54 Reviewed-on: https://go-review.googlesource.com/22529 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r--src/os/exec/exec_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index ed2721bb5e..1151ca7d0f 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -10,6 +10,7 @@ package exec_test
import (
"bufio"
"bytes"
+ "context"
"fmt"
"internal/testenv"
"io"
@@ -835,3 +836,41 @@ func TestOutputStderrCapture(t *testing.T) {
t.Errorf("ExitError.Stderr = %q; want %q", got, want)
}
}
+
+func TestContext(t *testing.T) {
+ c := helperCommand(t, "pipetest")
+ stdin, err := c.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ stdout, err := c.StdoutPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ if err := c.Start(); err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := stdin.Write([]byte("O:hi\n")); err != nil {
+ t.Fatal(err)
+ }
+ buf := make([]byte, 5)
+ n, err := io.ReadFull(stdout, buf)
+ if n != len(buf) || err != nil || string(buf) != "O:hi\n" {
+ t.Fatalf("ReadFull = %d, %v, %q", n, err, buf[:n])
+ }
+ waitErr := make(chan error, 1)
+ go func() {
+ waitErr <- c.WaitContext(ctx)
+ }()
+ cancel()
+ select {
+ case err := <-waitErr:
+ if err == nil {
+ t.Fatal("expected Wait failure")
+ }
+ case <-time.After(3 * time.Second):
+ t.Fatal("timeout waiting for child process death")
+ }
+}