package taskactions import ( "context" "slices" "testing" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/taskstate" ) // cancelStore is a task store whose cancel also cancels one waited-on callee. type cancelStore struct { Store task Task callees []int64 } func (f *cancelStore) TaskByID(context.Context, int64) (*Task, error) { task := f.task return &task, nil } func (f *cancelStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) { return f.callees, nil } func (f *cancelStore) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil } // Every operator action that cancels a run must also stop the runs of the // callees the store cancelled with it, or they run on to completion. func TestCancellingActionsStopCallees(t *testing.T) { tests := []struct { status string action taskstate.Action }{ {status: taskstate.Queued, action: taskstate.ActionCancel}, {status: taskstate.Running, action: taskstate.ActionStop}, {status: taskstate.Running, action: taskstate.ActionReject}, {status: taskstate.Parked, action: taskstate.ActionAbandon}, } for _, tt := range tests { t.Run(string(tt.action), func(t *testing.T) { var stopped []int64 s := Service{ Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}}, CancelTask: func(id int64) bool { stopped = append(stopped, id) return true }, } if err := s.Apply(t.Context(), nil, Actor{}, 1, tt.action, ActionPayload{}); err != nil { t.Fatal(err) } for _, callee := range []int64{7, 9} { if !slices.Contains(stopped, callee) { t.Fatalf("stopped %v, want callee %d among them", stopped, callee) } } }) } }