Compare commits

..

2 Commits
v1.0.0 ... main

Author SHA1 Message Date
9f32c6c43f Unbuffer callbacks so they are executed in order 2024-10-23 13:34:44 -07:00
86a55d16ea Return error if adding when runner hasn't been started
Before this used to block
2024-10-23 13:34:16 -07:00
2 changed files with 49 additions and 15 deletions

11
main.go
View File

@ -45,9 +45,10 @@ func NewShellRunnerWithShell(shell string) *ShellRunner {
cmdQueue: make(chan func() *CommandResult),
results: make(chan *CommandResult, MAX_RESULTS),
stopChan: make(chan struct{}),
callbacks: make(chan func(), MAX_RESULTS),
callbacks: make(chan func()),
shell: shell,
activeCmds: make(map[*exec.Cmd]struct{}),
isStopped: true,
}
}
@ -75,16 +76,20 @@ 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.
// The callback is executed asynchronously after the command has completed.
// The order of command execution and callback invocation can be expected to be preserved.
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 {

View File

@ -1,6 +1,7 @@
package tortoise_test
import (
"sync"
"testing"
"time"
@ -49,36 +50,55 @@ func TestShellRunnerCallback(t *testing.T) {
runner.Start()
// Test command with callback
done := make(chan struct{})
outputString := ""
callbackWait := sync.WaitGroup{}
callbackReached := false
callbackWait.Add(1)
if err := runner.AddCommand("echo callback test", func(result *tortoise.CommandResult) {
callbackReached = true
if result.Output != "callback test\n" {
t.Fatalf("expected 'callback test', got '%s'", result.Output)
if err := runner.AddCommand("echo callback a", func(result *tortoise.CommandResult) {
if result.Output != "callback a\n" {
t.Fatalf("expected 'callback a', got '%s'", result.Output)
}
close(done)
outputString = outputString + "a"
callbackWait.Done()
}); err != nil {
t.Fatalf("unexpected error adding command: %v", err)
}
// Timeout waiting for callback
callbackWait.Add(1)
if err := runner.AddCommand("echo callback b", func(result *tortoise.CommandResult) {
if result.Output != "callback b\n" {
t.Fatalf("expected 'callback b', got '%s'", result.Output)
}
outputString = outputString + "b"
callbackWait.Done()
}); err != nil {
t.Fatalf("unexpected error adding command: %v", err)
}
// Timeout waiting for callbacks
done := make(chan struct{})
go func() {
callbackWait.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("callback timed out")
t.Fatal("callbacks timed out")
}
if !callbackReached {
t.Fatal("callback was not reached")
if outputString != "ab" {
t.Fatal("callbacks was not reached in order:", outputString)
}
runner.Stop()
// 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 +142,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")
}
}