Download this file
| 1 | package schedule |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostwrite" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type replacementWork struct{ valid *bool } |
| 12 | |
| 13 | func (w replacementWork) With(_ context.Context, _ hostwrite.Identity, fn func() error) error { |
| 14 | if !*w.valid { |
| 15 | return hostwrite.ErrUnavailable |
| 16 | } |
| 17 | return fn() |
| 18 | } |
| 19 | func TestSelectedHeldReplacementKeepsRegistrationAndRefusesStale(t *testing.T) { |
| 20 | reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | defer reg.Close() |
| 25 | valid := true |
| 26 | selected := SelectedTimers{Store: reg, Work: replacementWork{&valid}, Context: context.Background(), Host: "shop.example"} |
| 27 | rows := []registry.Timer{{Host: "shop.example", Name: "one", Kind: "once", Spec: "spec", Path: "/tick", NextAt: 123, DueAt: 120, Registration: "from-source"}} |
| 28 | if err := selected.ReplaceTimers("shop.example", rows); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | got, err := reg.GetTimer("shop.example", "one") |
| 32 | if err != nil || got.Registration != "from-source" || got.DueAt != 120 { |
| 33 | t.Fatal(got, err) |
| 34 | } |
| 35 | valid = false |
| 36 | if err := selected.ReplaceTimers("shop.example", nil); err == nil { |
| 37 | t.Fatal("stale landing cleared timers") |
| 38 | } |
| 39 | got, err = reg.GetTimer("shop.example", "one") |
| 40 | if err != nil || got.Registration != "from-source" { |
| 41 | t.Fatal(got, err) |
| 42 | } |
| 43 | } |
| 44 | |