diff --git a/main.go b/main.go index 1d64e0e..22297ff 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func NewShellRunnerWithShell(shell string) *ShellRunner { callbacks: make(chan func(), MAX_RESULTS), shell: shell, activeCmds: make(map[*exec.Cmd]struct{}), + isStopped: true, } } @@ -75,16 +76,18 @@ func (sr *ShellRunner) Start() { } } }() + + sr.isStopped = false } // AddCommand adds a shell command to be executed with an optional callback. -// No commands can be added if the runner has been stopped. +// No commands can be added if the runner has been stopped or not yet started. func (sr *ShellRunner) AddCommand(command string, callback func(*CommandResult)) error { sr.mu.Lock() defer sr.mu.Unlock() if sr.isStopped { - return errors.New("runner has been stopped, cannot add new commands") + return errors.New("runner is stopped, cannot add new commands") } sr.cmdQueue <- func() *CommandResult { diff --git a/main_test.go b/main_test.go index d1848b6..8d1052d 100644 --- a/main_test.go +++ b/main_test.go @@ -78,7 +78,7 @@ func TestShellRunnerCallback(t *testing.T) { // Make sure stop and kill both exit gracefully after the runner is stopped runner.Stop() - runner.Stop() + runner.Kill() } func TestShellRunnerKillWithTimeout(t *testing.T) { @@ -122,3 +122,12 @@ func TestStopPreventsNewCommands(t *testing.T) { t.Fatal("expected error when adding command after stop, but got none") } } + +func TestAddingPriorToStart(t *testing.T) { + runner := tortoise.NewShellRunner() + + err := runner.AddCommand("echo should not run", nil) + if err == nil { + t.Fatal("Should have failed to add prior to starting runner") + } +}