mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00: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
1 changed files with 5 additions and 33 deletions
|
|
@ -1052,41 +1052,18 @@ func TestContextCancel(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
c := helperCommandContext(t, ctx, "cat")
|
c := helperCommandContext(t, ctx, "cat")
|
||||||
|
|
||||||
r, w, err := os.Pipe()
|
stdin, err := c.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
c.Stdin = r
|
defer stdin.Close()
|
||||||
|
|
||||||
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])
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := c.Start(); err != nil {
|
if err := c.Start(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.Close(); err != nil {
|
// At this point the process is alive. Ensure it by sending data to stdin.
|
||||||
t.Fatal(err)
|
if _, err := io.WriteString(stdin, "echo"); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := io.WriteString(w, "echo"); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1096,7 +1073,7 @@ func TestContextCancel(t *testing.T) {
|
||||||
// should now fail. Give the process a little while to die.
|
// should now fail. Give the process a little while to die.
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
for {
|
for {
|
||||||
if _, err := io.WriteString(w, "echo"); err != nil {
|
if _, err := io.WriteString(stdin, "echo"); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if time.Since(start) > time.Minute {
|
if time.Since(start) > time.Minute {
|
||||||
|
|
@ -1105,11 +1082,6 @@ func TestContextCancel(t *testing.T) {
|
||||||
time.Sleep(time.Millisecond)
|
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 {
|
if err := c.Wait(); err == nil {
|
||||||
t.Error("program unexpectedly exited successfully")
|
t.Error("program unexpectedly exited successfully")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue