Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
type CmdRunner interface {
Run(cmd string) error
RunContext(ctx context.Context, cmd string) error
Start(cmd string) error
Wait() error

StdinPipe() (io.WriteCloser, error)
StdoutPipe() (io.ReadCloser, error)
Expand Down
2 changes: 0 additions & 2 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type fakeCmdRunner struct {

func (r *fakeCmdRunner) Run(string) error { return r.err }
func (r *fakeCmdRunner) RunContext(context.Context, string) error { return r.err }
func (r *fakeCmdRunner) Start(string) error { return r.err }
func (r *fakeCmdRunner) Wait() error { return r.err }
func (r *fakeCmdRunner) StdinPipe() (io.WriteCloser, error) { return nil, errors.New("not supported") }
func (r *fakeCmdRunner) StdoutPipe() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader(r.output)), nil
Expand Down
21 changes: 21 additions & 0 deletions iago_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/relab/iago"
"github.com/relab/iago/iagotest"
Expand Down Expand Up @@ -78,6 +79,26 @@ func TestIago(t *testing.T) {
}
}

// TestStartHonorsContextTimeout verifies that a task using the "start now,
// wait later" pattern via RunContext respects the group's timeout: a
// long-running remote command must be aborted promptly when the context
// deadline expires, instead of blocking until the remote process exits.
func TestStartHonorsContextTimeout(t *testing.T) {
g := iagotest.CreateSSHGroup(t, 1, false)
g.Timeout = 500 * time.Millisecond

start := time.Now()
g.ErrorHandler = iago.Ignore
g.Run("Sleep beyond timeout", func(ctx context.Context, host iago.Host) error {
return iago.Shell{Command: "sleep 60"}.Apply(ctx, host)
})
elapsed := time.Since(start)

if elapsed >= 60*time.Second {
t.Fatalf("Wait did not honor context timeout: took %s", elapsed)
}
}

func TestIagoDownloadExample(t *testing.T) {
dir := t.TempDir()

Expand Down
8 changes: 4 additions & 4 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func (c sshCmd) Run(cmd string) (err error) {
}

func (c sshCmd) RunContext(ctx context.Context, cmd string) (err error) {
if err = c.session.Start(cmd); err != nil {
if err = c.start(cmd); err != nil {
return err
}

Expand All @@ -657,14 +657,14 @@ func (c sshCmd) RunContext(ctx context.Context, cmd string) (err error) {
errChan <- c.session.Close()
}()

return c.session.Wait()
return c.wait()
}

func (c sshCmd) Start(cmd string) error {
func (c sshCmd) start(cmd string) error {
return c.session.Start(cmd)
}

func (c sshCmd) Wait() (err error) {
func (c sshCmd) wait() (err error) {
defer safeClose(c.session, &err, io.EOF)
return c.session.Wait()
}
Expand Down