Files
archie/internal/domain/taskactions/service_test.go
T

78 lines
2.4 KiB
Go

package taskactions
import (
"context"
"errors"
"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
// failure is returned with the callees, as the store does when only their
// cancels failed.
failure error
}
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, f.failure
}
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) {
failure := errors.New("cancel callee 7: boom")
tests := []struct {
name string
status string
action taskstate.Action
failure error
// parked is a task with no live run of its own to stop.
parked bool
}{
{name: "cancel", status: taskstate.Queued, action: taskstate.ActionCancel},
{name: "stop", status: taskstate.Running, action: taskstate.ActionStop},
{name: "reject", status: taskstate.Running, action: taskstate.ActionReject},
{name: "abandon", status: taskstate.Parked, action: taskstate.ActionAbandon, parked: true},
{name: "stop with a failed callee cancel", status: taskstate.Running, action: taskstate.ActionStop, failure: failure},
{name: "cancel with a failed callee cancel", status: taskstate.Queued, action: taskstate.ActionCancel, failure: failure},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var stopped []int64
s := Service{
Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}, failure: tt.failure},
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)
}
want := []int64{7, 9}
if !tt.parked {
want = append(want, 1)
}
for _, callee := range want {
if !slices.Contains(stopped, callee) {
t.Fatalf("stopped %v, want %d among them", stopped, callee)
}
}
})
}
}