mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
newTaskTimeouts keeps every task it fired for in a persisted set and never removes one. A timed-out task can be revived (a rerun or a finding follow-up moves it back to running), and its next timeout is a new event, but the task id is still in the set, so on_task_timeout never fires for it again. Keep in the set only the tasks that are timed out now: a task that left the timeout state drops out, and fires again when it next times out. A scan while it stays timed out still fires nothing. Chaitanya Deshpande dcchaitanyadeshpande@gmail.com
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// Tasks time out in deadline order, not in id order: a task created later with
|
|
// a shorter timeout can time out first. Each timeout must still fire once.
|
|
func TestSchedulerTaskTimeoutsFireOutOfIDOrder(t *testing.T) {
|
|
skipWithoutDSN(t)
|
|
m, err := NewManager(t.TempDir(), "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { m.Close() })
|
|
sc := &Scheduler{pg: m.pg}
|
|
sc.newTaskTimeouts() // consume timeouts other tests left behind
|
|
|
|
var ids [2]int64
|
|
for i := range ids {
|
|
task, err := m.pg.CreateTaskWithOptions("timeout order", "goal", db.TaskCreateOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids[i] = task.ID
|
|
t.Cleanup(func() { _ = m.pg.DeleteTask(task.ID) })
|
|
}
|
|
older, newer := ids[0], ids[1]
|
|
timeOut := func(id int64) {
|
|
t.Helper()
|
|
if _, err := m.pg.Exec(`UPDATE tasks SET status='timeout' WHERE id=$1`, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
fired := func() []int64 {
|
|
var out []int64
|
|
for _, e := range sc.newTaskTimeouts() {
|
|
if e.TaskID == older || e.TaskID == newer {
|
|
out = append(out, e.TaskID)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
timeOut(newer)
|
|
if got := fired(); len(got) != 1 || got[0] != newer {
|
|
t.Fatalf("after the newer task timed out: fired %v, want [%d]", got, newer)
|
|
}
|
|
timeOut(older)
|
|
if got := fired(); len(got) != 1 || got[0] != older {
|
|
t.Fatalf("after the older task timed out: fired %v, want [%d]", got, older)
|
|
}
|
|
if got := fired(); len(got) != 0 {
|
|
t.Fatalf("second scan fired %v again, want nothing", got)
|
|
}
|
|
}
|
|
|
|
// A timed-out task can be revived (rerun, follow-up) and time out again. The
|
|
// second timeout is a new event and must fire on_task_timeout again; a scan
|
|
// while the task is still timed out must not.
|
|
func TestSchedulerTaskTimeoutFiresAgainAfterRevive(t *testing.T) {
|
|
skipWithoutDSN(t)
|
|
m, err := NewManager(t.TempDir(), "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { m.Close() })
|
|
sc := &Scheduler{pg: m.pg}
|
|
sc.newTaskTimeouts() // consume timeouts other tests left behind
|
|
|
|
task, err := m.pg.CreateTaskWithOptions("timeout revive", "goal", db.TaskCreateOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = m.pg.DeleteTask(task.ID) })
|
|
setStatus := func(status string) {
|
|
t.Helper()
|
|
if _, err := m.pg.Exec(`UPDATE tasks SET status=$1 WHERE id=$2`, status, task.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
fired := func() int {
|
|
n := 0
|
|
for _, e := range sc.newTaskTimeouts() {
|
|
if e.TaskID == task.ID {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
setStatus("timeout")
|
|
if got := fired(); got != 1 {
|
|
t.Fatalf("first timeout fired %d times, want 1", got)
|
|
}
|
|
if got := fired(); got != 0 {
|
|
t.Fatalf("scan while still timed out fired %d times, want 0", got)
|
|
}
|
|
setStatus("running") // revived
|
|
if got := fired(); got != 0 {
|
|
t.Fatalf("scan while running fired %d times, want 0", got)
|
|
}
|
|
setStatus("timeout")
|
|
if got := fired(); got != 1 {
|
|
t.Fatalf("timeout after a revive fired %d times, want 1", got)
|
|
}
|
|
}
|