test(scheduling): pin the schedule interval's duration contract
scheduling.Duration landed in f29169c2 (archie-core-1169) with no test over the
JSON contract the schedules document and the jobs table both depend on: the
interval marshals the human string time.ParseDuration reads, unmarshals both
that string and the nanosecond count every schedule stored before the shape
existed carries, and refuses a non-duration string with ErrInvalidSpec. Pin it,
because a regression here fails silently in the worst direction -- the document
writes a count and an operator is back to typing 1800000000000 for one hour.
This commit is contained in:
1 parent
eb95be607d
commit
b78722dc96
1 file changed
+39
@@ -1,7 +1,9 @@
|
||||
package scheduling
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -16,6 +18,43 @@ func zoned(year int, month time.Month, day, hour, minute, sec int) time.Time {
|
||||
return time.Date(year, month, day, hour, minute, sec, 0, testZone)
|
||||
}
|
||||
|
||||
// TestScheduleIntervalJSON pins the wire contract the schedules document and
|
||||
// the jobs table both depend on: the interval writes the human string
|
||||
// time.ParseDuration reads -- never the nanosecond count that once asked an
|
||||
// operator to type 1800000000000 for one hour -- and reads both forms, because
|
||||
// every schedule stored before this shape existed carries a count.
|
||||
func TestScheduleIntervalJSON(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
encoded string
|
||||
want Duration
|
||||
wantString string
|
||||
}{
|
||||
{name: "the string the document writes", encoded: `"30m"`, want: Duration(30 * time.Minute), wantString: "30m0s"},
|
||||
{name: "the nanosecond count a legacy document carries", encoded: `1800000000000`, want: Duration(30 * time.Minute), wantString: "30m0s"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var got Duration
|
||||
if err := json.Unmarshal([]byte(tc.encoded), &got); err != nil {
|
||||
t.Fatalf("Unmarshal(%s): %v", tc.encoded, err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("Unmarshal(%s) = %v, want %v", tc.encoded, got, tc.want)
|
||||
}
|
||||
encoded, err := json.Marshal(got)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal: %v", err)
|
||||
}
|
||||
if string(encoded) != fmt.Sprintf("%q", tc.wantString) {
|
||||
t.Errorf("Marshal(%v) = %s, want %q: the document writes the string, never a count", got, encoded, tc.wantString)
|
||||
}
|
||||
})
|
||||
}
|
||||
if err := json.Unmarshal([]byte(`"banana"`), new(Duration)); !errors.Is(err, ErrInvalidSpec) {
|
||||
t.Errorf("Unmarshal(banana) = %v, want ErrInvalidSpec", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleCronNextRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user