diff --git a/main.go b/main.go index 212b64c..70a105b 100644 --- a/main.go +++ b/main.go @@ -89,12 +89,16 @@ func (sr *ShellRunner) Start() { // AddCommand adds a shell command to be executed with an optional callback. // No commands can be added if the runner has been stopped or not yet started. // The callback is executed asynchronously after the command has completed. -// The order of command execution and callback invocation can be expected to be preserved. +// The order of command execution and callback invocation can be expected to be preserved (maybe?). func (sr *ShellRunner) AddCommand(command string, callback func(*CommandResult)) error { cmd, cancel := sr.newShellCommand(command) return sr.AddCmd(cmd, callback, cancel) } +// AddCmd adds a pre-configured exec.Cmd to be executed with an optional callback and cancel function. +// No commands can be added if the runner has been stopped or not yet started. +// The callback is executed asynchronously after the command has completed. +// The order of command execution and callback invocation can be expected to be preserved (maybe?). func (sr *ShellRunner) AddCmd(cmd *exec.Cmd, callback func(*CommandResult), cancel context.CancelFunc) error { sr.mu.Lock() defer sr.mu.Unlock() diff --git a/main_test.go b/main_test.go index 694710b..6ed375e 100644 --- a/main_test.go +++ b/main_test.go @@ -78,9 +78,6 @@ func TestShellRunnerCallback(t *testing.T) { t.Fatalf("unexpected error adding command: %v", err) } - // Wait a sec for the worker to pick up the task - time.Sleep(TaskStartWait) - callbackWait.Add(1) if err := runner.AddCommand("echo callback b", func(result *tortoise.CommandResult) { @@ -93,6 +90,9 @@ func TestShellRunnerCallback(t *testing.T) { t.Fatalf("unexpected error adding command: %v", err) } + // Wait a sec for the worker to pick up the task + time.Sleep(TaskStartWait) + // Timeout waiting for callbacks done := make(chan struct{}) go func() {