76 lines
3.0 KiB
Go
76 lines
3.0 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func TestSessionExportGateCheckpointAndSuccessorAreAtomic(t *testing.T) {
|
|
s, id, _ := exportJobFixture(t)
|
|
ctx := context.Background()
|
|
step := protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Target: "two", Output: map[string]any{"markdown": "actual result"}, SessionExport: &protocol.SessionExport{Status: "failed", SessionID: "ses_original", Backend: "v2", Diagnostic: "offline"}}
|
|
if err := s.FinishStepExportBlocked(ctx, id, step); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.BeginStep(ctx, id, 2, "two", nil); err == nil {
|
|
t.Fatal("successor started while export was incomplete")
|
|
}
|
|
paused, err := s.GetRun(ctx, id)
|
|
if err != nil || paused.Status != "waiting" || paused.ExportPendingSequence != 1 || paused.FinishedAt != "" {
|
|
t.Fatalf("gate became terminal: %+v %v", paused, err)
|
|
}
|
|
if _, _, err := s.ResolveDecision(ctx, id, "one", "success", ""); err == nil {
|
|
t.Fatal("model export wait accepted human decision")
|
|
}
|
|
job, _ := s.SessionExportJob(ctx, id, 1)
|
|
claimed, err := s.ClaimSessionExportJob(ctx, time.UnixMilli(job.NextAt), time.Minute)
|
|
if err != nil || claimed == nil {
|
|
t.Fatalf("missing export-only retry: %v", err)
|
|
}
|
|
if err := s.RecordSessionExportRepair(ctx, id, 1, &protocol.SessionExport{Status: "complete", SessionID: claimed.SessionID, Backend: claimed.Backend, Directory: claimed.Directory, Messages: 2, HTML: "session-repair-1-test.html", HTMLBytes: 100}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.FinishSessionExportJob(ctx, claimed, time.Now(), ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.ResumeSessionExportGate(ctx, id, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.ResumeSessionExportGate(ctx, id, 1); err == nil {
|
|
t.Fatal("duplicate continuation admitted")
|
|
}
|
|
// A crash after reserving the continuation but before starting its next
|
|
// executor must retain the checkpoint for startup's interrupted run.
|
|
for _, prior := range []string{"failed", "interrupted", "running", "queued"} {
|
|
if _, err := s.db.Exec(`UPDATE pipeline_runs SET status=?,finished_at=CURRENT_TIMESTAMP WHERE id=?`, prior, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.RecoverRuns(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recovered, err := s.GetRun(ctx, id)
|
|
if err != nil || recovered.Status != "waiting" || recovered.ExportPendingSequence != 1 || recovered.FinishedAt != "" {
|
|
t.Fatalf("restart exposed a terminal export wait from %s: %+v %v", prior, recovered, err)
|
|
}
|
|
}
|
|
ready, err := s.ReadySessionExportGates(ctx)
|
|
if err != nil || ready[id] != 1 {
|
|
t.Fatalf("lost restart checkpoint: %v %v", ready, err)
|
|
}
|
|
if err := s.ResumeSessionExportGate(ctx, id, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.BeginStep(ctx, id, 2, "two", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if blocked, err := s.SessionExportBlocked(ctx, id); err != nil || blocked {
|
|
t.Fatalf("successor did not consume checkpoint: %v %v", blocked, err)
|
|
}
|
|
if err := s.BeginStep(ctx, id, 2, "two", nil); err == nil {
|
|
t.Fatal("successor started twice")
|
|
}
|
|
}
|