| 1 | package bot |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestBackoffDelay(t *testing.T) { |
| 9 | hourly := &ScheduleSpec{Every: "1h"} |
| 10 | if err := hourly.validate(); err != nil { |
| 11 | t.Fatal(err) |
| 12 | } |
| 13 | cases := []struct { |
| 14 | fails int |
| 15 | want time.Duration |
| 16 | }{ |
| 17 | {0, 0}, // no failures — the schedule stands |
| 18 | {1, 2 * time.Hour}, |
| 19 | {2, 4 * time.Hour}, |
| 20 | {3, 8 * time.Hour}, |
| 21 | {5, 24 * time.Hour}, // 32h doubled — capped at a day |
| 22 | {50, 24 * time.Hour}, // deep outage must not overflow past the cap |
| 23 | } |
| 24 | for _, c := range cases { |
| 25 | if got := backoffDelay(hourly, c.fails); got != c.want { |
| 26 | t.Errorf("backoffDelay(1h, %d) = %s, want %s", c.fails, got, c.want) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Daily (at) schedules never stretch — their cadence is already gentle. |
| 31 | daily := &ScheduleSpec{At: "08:00", TZ: "Europe/Dublin"} |
| 32 | if err := daily.validate(); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | for _, fails := range []int{0, 1, 7} { |
| 36 | if got := backoffDelay(daily, fails); got != 0 { |
| 37 | t.Errorf("backoffDelay(daily, %d) = %s, want 0", fails, got) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestTaskResultSourcesDown(t *testing.T) { |
| 43 | cases := []struct { |
| 44 | res taskResult |
| 45 | want bool |
| 46 | }{ |
| 47 | {taskResult{fetches: 2, fetchOK: 0}, true}, // every source failed |
| 48 | {taskResult{fetches: 2, fetchOK: 1}, false}, // partial reads still count as an edition |
| 49 | {taskResult{fetches: 0, fetchOK: 0}, false}, // a no-fetch task (daily poem) can't be "down" |
| 50 | } |
| 51 | for _, c := range cases { |
| 52 | if got := c.res.sourcesDown(); got != c.want { |
| 53 | t.Errorf("sourcesDown(%+v) = %v, want %v", c.res, got, c.want) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |