summaryrefslogtreecommitdiff
path: root/internal/executor/executor_test.go
diff options
context:
space:
mode:
authorPeter Stone <thepeterstone@gmail.com>2026-03-06 00:09:34 +0000
committerPeter Stone <thepeterstone@gmail.com>2026-03-06 00:09:34 +0000
commit3672981c4d225bb5b7d965a8945bc7bc6e8b4e9d (patch)
tree0e40902052405b9cf0063f859a017ff3ab62ef3c /internal/executor/executor_test.go
parent7466b1751c4126735769a3304e1db80dab166a9e (diff)
fix: implement cancel endpoint and pool cancel mechanism
POST /api/tasks/{id}/cancel now works. Pool tracks a cancel func per running task ID; Cancel(taskID) calls it and returns false if the task isn't running. The execute goroutine registers/deregisters the cancel func around the runner call. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/executor/executor_test.go')
-rw-r--r--internal/executor/executor_test.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/internal/executor/executor_test.go b/internal/executor/executor_test.go
index b3e6dae..6d13873 100644
--- a/internal/executor/executor_test.go
+++ b/internal/executor/executor_test.go
@@ -185,6 +185,38 @@ func TestPool_Submit_Cancellation(t *testing.T) {
}
}
+func TestPool_Cancel_StopsRunningTask(t *testing.T) {
+ store := testStore(t)
+ runner := &mockRunner{delay: 5 * time.Second}
+ logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
+ pool := NewPool(2, runner, store, logger)
+
+ tk := makeTask("cancel-1")
+ store.CreateTask(tk)
+ pool.Submit(context.Background(), tk)
+ time.Sleep(20 * time.Millisecond) // let goroutine start
+
+ if ok := pool.Cancel("cancel-1"); !ok {
+ t.Fatal("Cancel returned false for a running task")
+ }
+
+ result := <-pool.Results()
+ if result.Execution.Status != "CANCELLED" {
+ t.Errorf("status: want CANCELLED, got %q", result.Execution.Status)
+ }
+}
+
+func TestPool_Cancel_UnknownTask_ReturnsFalse(t *testing.T) {
+ store := testStore(t)
+ runner := &mockRunner{}
+ logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
+ pool := NewPool(2, runner, store, logger)
+
+ if ok := pool.Cancel("nonexistent"); ok {
+ t.Error("Cancel returned true for unknown task")
+ }
+}
+
func TestPool_AtCapacity(t *testing.T) {
store := testStore(t)
runner := &mockRunner{delay: time.Second}