feat(scheduler): 30s polling scheduler with pure NextRun/dueTaskIDs

This commit is contained in:
2026-07-03 13:07:05 +07:00
parent 8f93dcd97b
commit b9421d388c
3 changed files with 116 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package scheduler
import (
"testing"
"time"
"github.com/vasyansk/imap-copier/internal/store"
)
func TestNextRun(t *testing.T) {
anchor := time.Date(2026, 7, 3, 10, 0, 0, 0, time.UTC)
fin := time.Date(2026, 7, 3, 11, 0, 0, 0, time.UTC)
// No finished run yet → anchor + interval.
if got := NextRun(time.Hour, anchor, nil); !got.Equal(anchor.Add(time.Hour)) {
t.Fatalf("first run: got %v", got)
}
// Finished run exists → last finished + interval (from completion, not start).
if got := NextRun(time.Hour, anchor, &fin); !got.Equal(fin.Add(time.Hour)) {
t.Fatalf("recurring: got %v", got)
}
}
func TestDueTaskIDs(t *testing.T) {
now := time.Date(2026, 7, 3, 12, 0, 0, 0, time.UTC)
anchorOld := now.Add(-2 * time.Hour) // enabled 2h ago
finRecent := now.Add(-30 * time.Minute)
tasks := []store.SchedulableTask{
{ID: 1, IntervalSeconds: 3600, Anchor: anchorOld, LastFinished: nil}, // due: anchor+1h < now
{ID: 2, IntervalSeconds: 3600, Anchor: anchorOld, LastFinished: &finRecent}, // not due: fin+1h > now
{ID: 3, IntervalSeconds: 21600, Anchor: anchorOld, LastFinished: nil}, // not due: anchor+6h > now
}
ids := dueTaskIDs(tasks, now)
if len(ids) != 1 || ids[0] != 1 {
t.Fatalf("due IDs = %v, want [1]", ids)
}
}