mirror of
https://github.com/golang/go
synced 2024-11-23 06:20:07 -07:00
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>
This commit is contained in:
parent
dc289d3dcb
commit
4e27aa6cd2
@ -1052,41 +1052,18 @@ func TestContextCancel(t *testing.T) {
|
||||
defer cancel()
|
||||
c := helperCommandContext(t, ctx, "cat")
|
||||
|
||||
r, w, err := os.Pipe()
|
||||
stdin, err := c.StdinPipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.Stdin = r
|
||||
|
||||
stdout, err := c.StdoutPipe()
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user