feat(store): task schedule columns, run trigger, scheduling queries
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTaskScheduleAndBroken(t *testing.T) {
|
||||
s := testStore(t)
|
||||
ctx := context.Background()
|
||||
epSrc, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "a", Port: 993, TLSMode: "ssl"})
|
||||
epDst, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "dst", Host: "b", Port: 993, TLSMode: "ssl"})
|
||||
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: epSrc, DstEndpointID: epDst})
|
||||
|
||||
// Defaults: no schedule, not broken.
|
||||
tk, _ := s.GetTask(ctx, taskID)
|
||||
if tk.ScheduleIntervalSeconds != 0 || tk.Broken || tk.ScheduleAnchor != nil {
|
||||
t.Fatalf("defaults: interval=%d broken=%v anchor=%v", tk.ScheduleIntervalSeconds, tk.Broken, tk.ScheduleAnchor)
|
||||
}
|
||||
|
||||
// Enable → interval set, anchor stamped, appears in schedulable list.
|
||||
if err := s.SetTaskSchedule(ctx, taskID, 3600); err != nil {
|
||||
t.Fatalf("set schedule: %v", err)
|
||||
}
|
||||
tk, _ = s.GetTask(ctx, taskID)
|
||||
if tk.ScheduleIntervalSeconds != 3600 || tk.ScheduleAnchor == nil {
|
||||
t.Fatalf("after enable: interval=%d anchor=%v", tk.ScheduleIntervalSeconds, tk.ScheduleAnchor)
|
||||
}
|
||||
sch, _ := s.ListSchedulableTasks(ctx)
|
||||
if len(sch) != 1 || sch[0].ID != taskID || sch[0].IntervalSeconds != 3600 || sch[0].LastFinished != nil {
|
||||
t.Fatalf("schedulable: %+v", sch)
|
||||
}
|
||||
|
||||
// Breaker → broken true, interval 0, drops out of schedulable list.
|
||||
if err := s.SetTaskBroken(ctx, taskID); err != nil {
|
||||
t.Fatalf("set broken: %v", err)
|
||||
}
|
||||
tk, _ = s.GetTask(ctx, taskID)
|
||||
if !tk.Broken || tk.ScheduleIntervalSeconds != 0 {
|
||||
t.Fatalf("after break: broken=%v interval=%d", tk.Broken, tk.ScheduleIntervalSeconds)
|
||||
}
|
||||
if sch, _ := s.ListSchedulableTasks(ctx); len(sch) != 0 {
|
||||
t.Fatalf("broken task still schedulable: %+v", sch)
|
||||
}
|
||||
|
||||
// Re-enable clears broken.
|
||||
_ = s.SetTaskSchedule(ctx, taskID, 21600)
|
||||
if tk, _ = s.GetTask(ctx, taskID); tk.Broken {
|
||||
t.Fatalf("re-enable did not clear broken")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRunsByTaskWithTrigger(t *testing.T) {
|
||||
s := testStore(t)
|
||||
ctx := context.Background()
|
||||
epSrc, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "a", Port: 993, TLSMode: "ssl"})
|
||||
epDst, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "dst", Host: "b", Port: 993, TLSMode: "ssl"})
|
||||
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: epSrc, DstEndpointID: epDst})
|
||||
|
||||
r1, _ := s.CreateRun(ctx, taskID, "manual")
|
||||
_ = s.FinishRun(ctx, r1, "done", 5, 1, 0)
|
||||
_, _ = s.CreateRun(ctx, taskID, "scheduled") // still running (no finish)
|
||||
|
||||
runs, err := s.ListRunsByTask(ctx, taskID)
|
||||
if err != nil || len(runs) != 2 {
|
||||
t.Fatalf("list runs: %v len=%d", err, len(runs))
|
||||
}
|
||||
// newest first
|
||||
if runs[0].Trigger != "scheduled" || runs[0].FinishedAt != nil {
|
||||
t.Fatalf("run[0]: %+v", runs[0])
|
||||
}
|
||||
if runs[1].Trigger != "manual" || runs[1].FinishedAt == nil || runs[1].TotalCopied != 5 {
|
||||
t.Fatalf("run[1]: %+v", runs[1])
|
||||
}
|
||||
|
||||
// Last finished-at reflects the finished manual run.
|
||||
lf, _ := s.LastFinishedRunAt(ctx, taskID)
|
||||
if lf == nil {
|
||||
t.Fatalf("LastFinishedRunAt nil, want the finished run's time")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user