Files

42 lines
1.4 KiB
Go

package events
import (
"testing"
"time"
)
// TestDueSlot guards the rule that a schedule fires once per slot: a slot
// missed while down collapses into one firing, and a new schedule doesn't fire
// the slot it was created in.
func TestDueSlot(t *testing.T) {
at := func(s string) time.Time {
v, err := time.Parse(time.RFC3339, s)
if err != nil {
t.Fatal(err)
}
return v
}
tests := []struct {
name string
now string
since string
every time.Duration
want string
wantFires bool
}{
{"created mid slot", "2026-01-01T10:45:00Z", "2026-01-01T10:30:00Z", time.Hour, "2026-01-01T10:00:00Z", false},
{"next slot begins", "2026-01-01T11:00:00Z", "2026-01-01T10:30:00Z", time.Hour, "2026-01-01T11:00:00Z", true},
{"down for hours fires the latest slot once", "2026-01-01T14:20:00Z", "2026-01-01T10:30:00Z", time.Hour, "2026-01-01T14:00:00Z", true},
{"same slot again is the same slot", "2026-01-01T14:59:00Z", "2026-01-01T10:30:00Z", time.Hour, "2026-01-01T14:00:00Z", true},
{"minute interval", "2026-01-01T10:31:10Z", "2026-01-01T10:30:20Z", time.Minute, "2026-01-01T10:31:00Z", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
slot, fires := dueSlot(at(tt.now), at(tt.since), tt.every)
if !slot.Equal(at(tt.want)) || fires != tt.wantFires {
t.Errorf("dueSlot = %v, %v; want %v, %v", slot, fires, tt.want, tt.wantFires)
}
})
}
}