Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Remove crufty monitoredCmd.ctx & add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Apr 17, 2017
1 parent 598fa11 commit eaa693e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 1 addition & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
type monitoredCmd struct {
cmd *exec.Cmd
timeout time.Duration
ctx context.Context
stdout *activityBuffer
stderr *activityBuffer
}
Expand Down Expand Up @@ -68,7 +67,7 @@ func (c *monitoredCmd) run(ctx context.Context) error {
return &killCmdError{err}
}
}
return c.ctx.Err()
return ctx.Err()
case err := <-done:
return err
}
Expand Down
21 changes: 20 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func mkTestCmd(iterations int) *monitoredCmd {
}

func TestMonitoredCmd(t *testing.T) {
// Sleeps make this a bit slow
// Sleeps and compile make this a bit slow
if testing.Short() {
t.Skip("skipping test with sleeps on short")
}
Expand Down Expand Up @@ -54,4 +54,23 @@ func TestMonitoredCmd(t *testing.T) {
if cmd2.stdout.buf.String() != expectedOutput {
t.Errorf("Unexpected output:\n\t(GOT): %s\n\t(WNT): %s", cmd2.stdout.buf.String(), expectedOutput)
}

ctx, cancel := context.WithCancel(context.Background())
sync1, errchan := make(chan struct{}), make(chan error)
cmd3 := mkTestCmd(2)
go func() {
close(sync1)
errchan <- cmd3.run(ctx)
}()

// Make sure goroutine is at least started before we cancel the context.
<-sync1
// Give it a bit to get the process started.
<-time.After(5 * time.Millisecond)
cancel()

err = <-errchan
if err != context.Canceled {
t.Errorf("should have gotten canceled error, got %s", err)
}
}

0 comments on commit eaa693e

Please sign in to comment.