package server import ( "context" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) func scheduleNotices(t *testing.T, s *pipeline.Store) []protocol.AppNotification { t.Helper() active, _, err := s.ActiveAppNotifications(context.Background(), "", time.Now()) if err != nil { t.Fatal(err) } var notices []protocol.AppNotification for _, n := range active { if n.Kind == protocol.NotificationSchedule { notices = append(notices, n) } } return notices } func waitOccurrence(t *testing.T, s *pipeline.Store, id int64, due time.Time, want string) (string, int64) { t.Helper() deadline := time.Now().Add(10 * time.Second) for { status, diagnostic, run, err := s.SavedScheduleOccurrence(context.Background(), id, due) if err == nil && status == want { return diagnostic, run } if time.Now().After(deadline) { t.Fatalf("occurrence status %q (%q, %v), want %q", status, diagnostic, err, want) } time.Sleep(5 * time.Millisecond) } } // A scheduled run whose workspace another run holds is delayed, not skipped: // it is recorded as delayed, the operator is told at low priority, and it // starts once the workspace is free. func TestScheduledRunWaitsForABusyWorkspace(t *testing.T) { seamScheduleBusyRetry(t, 20*time.Millisecond, time.Hour) ctx, s, r, pid := scheduleFixture(t) r.notes = newAppNotifier(s) workspace := t.TempDir() busy := saveTestSchedule(t, ctx, s, pid, "busy", workspace, "1h", time.Now()) brief := saveTestSchedule(t, ctx, s, pid, "Daily brief", workspace, "1h", time.Now()) release := make(chan struct{}) entered := make(chan struct{}, 2) r.execute = func(ctx context.Context, _ stage.Definition, input stage.Values) (stage.Result, error) { entered <- struct{}{} if prompt, _ := input["prompt"].(string); prompt == "busy" { <-release } return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil } if err := r.scheduleTick(dueOf(t, busy), false); err != nil { t.Fatal(err) } <-entered due := dueOf(t, brief) if err := r.scheduleTick(due, false); err != nil { t.Fatal(err) } diagnostic, _ := waitOccurrence(t, s, brief.ID, due, "delayed") if !strings.Contains(diagnostic, "busy workspace") || !strings.Contains(diagnostic, "already active") { t.Fatalf("delay diagnostic %q", diagnostic) } notices := scheduleNotices(t, s) if len(notices) != 1 || notices[0].Priority != 2 || !strings.Contains(notices[0].Title, "delayed") || !strings.Contains(notices[0].Title, "Daily brief") { t.Fatalf("delay notices %+v", notices) } close(release) diagnostic, run := waitOccurrence(t, s, brief.ID, due, "started") r.scheduleAdmissions.Wait() r.wg.Wait() if run == 0 || !strings.Contains(diagnostic, "started after waiting") { t.Fatalf("delayed run not admitted: run=%d %q", run, diagnostic) } runs, err := s.ListRuns(ctx) if err != nil || len(runs) != 2 { t.Fatalf("runs %+v %v", runs, err) } } // Waiting is bounded: past the limit the occurrence is skipped, saying why, // and the operator is told. func TestScheduledRunGivesUpOnABusyWorkspace(t *testing.T) { seamScheduleBusyRetry(t, 20*time.Millisecond, 150*time.Millisecond) ctx, s, r, pid := scheduleFixture(t) r.notes = newAppNotifier(s) workspace := t.TempDir() busy := saveTestSchedule(t, ctx, s, pid, "busy", workspace, "1h", time.Now()) brief := saveTestSchedule(t, ctx, s, pid, "Daily brief", workspace, "1h", time.Now()) release := make(chan struct{}) defer close(release) entered := make(chan struct{}, 2) r.execute = func(ctx context.Context, _ stage.Definition, _ stage.Values) (stage.Result, error) { entered <- struct{}{} select { case <-release: case <-ctx.Done(): } return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Done"}}, nil } if err := r.scheduleTick(dueOf(t, busy), false); err != nil { t.Fatal(err) } <-entered due := dueOf(t, brief) if claimed, err := s.ClaimSavedSchedule(ctx, *brief, due, due.Add(time.Hour), "claimed"); err != nil || !claimed { t.Fatal(claimed, err) } r.admitScheduled(*brief, due) diagnostic, run := waitOccurrence(t, s, brief.ID, due, "skipped") if run != 0 || !strings.Contains(diagnostic, "gave up after waiting") { t.Fatalf("skip diagnostic %q run=%d", diagnostic, run) } notices := scheduleNotices(t, s) var titles []string for _, n := range notices { titles = append(titles, n.Title) if n.Priority != 2 { t.Fatalf("schedule notice is not low priority: %+v", n) } } if len(notices) != 2 || !strings.Contains(strings.Join(titles, "|"), "skipped") { t.Fatalf("notices %v", titles) } }