158 lines
4.6 KiB
Go
158 lines
4.6 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/runs/internal/store"
|
|
)
|
|
|
|
// fakeWork is an in-memory worker store with lease-fenced writes, used to
|
|
// drive the engine through a lost lease and a conflicting step write.
|
|
type fakeWork struct {
|
|
mu sync.Mutex
|
|
// leaseLost makes every fenced write fail as if another worker held the run.
|
|
leaseLost bool
|
|
// recordedElsewhere is a step another lease holder already recorded; it
|
|
// becomes visible once this worker tries to write the same position.
|
|
recordedElsewhere *store.Step
|
|
steps []store.Step
|
|
finished []string
|
|
results []json.RawMessage
|
|
}
|
|
|
|
func (f *fakeWork) Claim(context.Context, string, uuid.UUID, time.Duration) (store.Run, bool, error) {
|
|
return store.Run{}, false, nil
|
|
}
|
|
|
|
func (f *fakeWork) RenewLease(context.Context, uuid.UUID, uuid.UUID, time.Duration) (bool, error) {
|
|
return true, nil
|
|
}
|
|
|
|
func (f *fakeWork) Steps(context.Context, uuid.UUID) ([]store.Step, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]store.Step(nil), f.steps...), nil
|
|
}
|
|
|
|
func (f *fakeWork) RecordStep(_ context.Context, _ uuid.UUID, step store.Step) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.leaseLost {
|
|
return store.ErrLeaseLost
|
|
}
|
|
if f.recordedElsewhere != nil && f.recordedElsewhere.Seq == step.Seq {
|
|
f.steps = append(f.steps, *f.recordedElsewhere)
|
|
return store.ErrStepExists
|
|
}
|
|
f.steps = append(f.steps, step)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWork) Suspend(context.Context, uuid.UUID, uuid.UUID, *time.Time, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWork) Finish(_ context.Context, _, _ uuid.UUID, state string, result json.RawMessage, _ string) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.leaseLost {
|
|
return store.ErrLeaseLost
|
|
}
|
|
f.finished = append(f.finished, state)
|
|
f.results = append(f.results, result)
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeWork) Signalled(context.Context, uuid.UUID, string) (json.RawMessage, bool, error) {
|
|
return nil, false, nil
|
|
}
|
|
|
|
func (f *fakeWork) CancelRequested(context.Context, uuid.UUID) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
// TestLeaseFencesStepWrites guards 2.2's guarantee that correctness rests on
|
|
// fenced writes: a worker that lost its lease stops before running another
|
|
// step (which could repeat an effect), and a step another holder already
|
|
// recorded wins over this worker's own result.
|
|
func TestLeaseFencesStepWrites(t *testing.T) {
|
|
theirs := json.RawMessage(`"theirs"`)
|
|
tests := []struct {
|
|
name string
|
|
work *fakeWork
|
|
wantSteps int
|
|
wantEffects int
|
|
wantFinished []string
|
|
wantResult string
|
|
}{
|
|
{
|
|
name: "lost lease stops the run before its next step",
|
|
work: &fakeWork{leaseLost: true},
|
|
wantSteps: 0,
|
|
wantEffects: 0,
|
|
wantFinished: nil,
|
|
},
|
|
{
|
|
name: "a step recorded by another holder replaces this worker's result",
|
|
work: &fakeWork{recordedElsewhere: &store.Step{
|
|
Seq: 0, Name: "probe", Outcome: store.OutcomeDone, Output: theirs,
|
|
}},
|
|
wantSteps: 2,
|
|
wantEffects: 1,
|
|
wantFinished: []string{store.StateSucceeded},
|
|
wantResult: `"theirs"`,
|
|
},
|
|
{
|
|
name: "a held lease records the steps and finishes the run",
|
|
work: &fakeWork{},
|
|
wantSteps: 2,
|
|
wantEffects: 1,
|
|
wantFinished: []string{store.StateSucceeded},
|
|
wantResult: `"mine"`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
engine := newEngine(nil, tt.work, slog.New(slog.DiscardHandler), Options{})
|
|
effects := 0
|
|
engine.Register("probe", func(rc *Context, _ json.RawMessage) (json.RawMessage, error) {
|
|
out, err := rc.Step("probe", func(context.Context, string) (string, error) {
|
|
return "mine", nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := rc.Step("effect", func(context.Context, string) (bool, error) {
|
|
effects++
|
|
return true, nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(out)
|
|
})
|
|
run := store.Run{ID: uuid.New(), OrgID: "org", Workflow: "probe", Input: json.RawMessage(`null`)}
|
|
engine.execute(t.Context(), run, uuid.New())
|
|
|
|
if got := len(tt.work.steps); got != tt.wantSteps {
|
|
t.Fatalf("steps recorded = %d, want %d", got, tt.wantSteps)
|
|
}
|
|
if effects != tt.wantEffects {
|
|
t.Fatalf("effect step ran %d times, want %d", effects, tt.wantEffects)
|
|
}
|
|
if len(tt.work.finished) != len(tt.wantFinished) {
|
|
t.Fatalf("finished = %v, want %v", tt.work.finished, tt.wantFinished)
|
|
}
|
|
if len(tt.wantFinished) > 0 && string(tt.work.results[0]) != tt.wantResult {
|
|
t.Fatalf("result = %s, want %s", tt.work.results[0], tt.wantResult)
|
|
}
|
|
})
|
|
}
|
|
}
|