132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunNotificationPersistenceLeasesAndBound(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "outbox.db")
|
|
s, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := RunNotification{RunID: 7, Sequence: 2, Event: "failed", Channel: "system", Title: "Failed", Body: "Run #7", Click: "pipelines://run/7"}
|
|
if err := s.EnqueueRunNotification(ctx, n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.EnqueueRunNotification(ctx, n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first, err := s.ClaimRunNotification(ctx, 100)
|
|
if err != nil || first == nil || first.Attempts != 1 {
|
|
t.Fatalf("first claim: %+v %v", first, err)
|
|
}
|
|
if other, err := s.ClaimRunNotification(ctx, 100); err != nil || other != nil {
|
|
t.Fatalf("lease double claim: %+v %v", other, err)
|
|
}
|
|
// Reopen an already-existing schema, preserving an in-flight lease.
|
|
if err := s.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
second, err := s.ClaimRunNotification(ctx, 161)
|
|
if err != nil || second == nil || second.Attempts != 2 || second.Body != n.Body {
|
|
t.Fatalf("recovery: %+v %v", second, err)
|
|
}
|
|
if err := s.FinishRunNotification(ctx, *second, 161, "", errors.New("publisher unavailable")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if other, err := s.ClaimRunNotification(ctx, 170); err != nil || other != nil {
|
|
t.Fatalf("backoff ignored: %+v %v", other, err)
|
|
}
|
|
third, err := s.ClaimRunNotification(ctx, 171)
|
|
if err != nil || third == nil || third.Attempts != 3 {
|
|
t.Fatalf("third claim: %+v %v", third, err)
|
|
}
|
|
// Final attempt crashes: its expired lease must become failed, not retry forever.
|
|
if other, err := s.ClaimRunNotification(ctx, 232); err != nil || other != nil {
|
|
t.Fatalf("retry bound ignored: %+v %v", other, err)
|
|
}
|
|
got, err := s.RunNotifications(ctx, 7)
|
|
if err != nil || len(got) != 1 || got[0].Status != "failed" || got[0].Attempts != 3 {
|
|
t.Fatalf("audit: %+v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestRunNotificationStageCoverageSurvivesReopen(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "coverage.db")
|
|
s, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.CoverRunNotification(ctx, 7, 1, "succeeded"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
if err := s.EnqueueRunNotification(ctx, RunNotification{RunID: 7, Sequence: 1, Event: "succeeded", Channel: "system"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := s.ClaimRunNotification(ctx, 100); err != nil || got != nil {
|
|
t.Fatalf("covered stage repeated: %+v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestRunNotificationMigrationPreservesExistingRun(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "existing.db")
|
|
s, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Submit(ctx, "existing", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := s.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d, err := s.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, _, err := s.CreateRun(ctx, d, "existing-key", "workspace", false, nil, map[string]any{"prompt": "preserve me"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Simulate the immediately preceding schema, which has no run outbox.
|
|
if _, err := s.db.ExecContext(ctx, "DROP TABLE run_notifications"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
got, err := s.GetRun(ctx, id)
|
|
if err != nil || got.Input["prompt"] != "preserve me" || got.Status != "queued" || got.NotifyMode != "auto" || len(got.Notifications) != 0 {
|
|
t.Fatalf("migration changed run: %+v %v", got, err)
|
|
}
|
|
if err := s.EnqueueRunNotification(ctx, RunNotification{RunID: id, Sequence: 1, Event: "failed", Channel: "system"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|