package dregarnuhr import ( "errors" "fmt" "math" "testing" "time" ) func TestRetryDelay(t *testing.T) { for _, tc := range []struct { base, attempt int want time.Duration }{ {30, 1, 30 * time.Second}, {30, 2, time.Minute}, {30, 3, 2 * time.Minute}, {30, 1000000, 24 * time.Hour}, {86401, 1, 24 * time.Hour}, {math.MaxInt, math.MaxInt, 24 * time.Hour}, {0, 1, time.Second}, } { if got := RetryDelay(tc.base, tc.attempt); got != tc.want { t.Errorf("%+v got=%s", tc, got) } } } func TestCompletionPolicy(t *testing.T) { now := time.Unix(1000, 0) for _, tc := range []struct { name string job Job err error status, level string }{ {"success", Job{Attempts: 1, MaxAttempts: 3}, nil, "succeeded", "info"}, {"retry", Job{Attempts: 2, MaxAttempts: 3}, errors.New("failed"), "queued", "warning"}, {"exhausted", Job{Attempts: 3, MaxAttempts: 3}, errors.New("failed"), "failed", "error"}, {"cancel", Job{Attempts: 1, MaxAttempts: 3, CancelRequested: true}, errors.New("cancelled"), "cancelled", "info"}, {"skip-before-cancel", Job{Attempts: 1, MaxAttempts: 3, CancelRequested: true}, fmt.Errorf("wrapped: %w", ErrSkipped), "skipped", "info"}, } { t.Run(tc.name, func(t *testing.T) { got := Complete(tc.job, tc.err, 30, now) if got.Status != tc.status || got.Level != tc.level { t.Fatalf("%+v", got) } if tc.status == "queued" && !got.RetryAt.Equal(now.Add(time.Minute)) { t.Fatal(got) } if tc.status != "queued" && !got.RetryAt.IsZero() { t.Fatal("terminal job retried") } }) } } func TestAdmissionPolicy(t *testing.T) { s := Settings{Concurrency: 4} if !CanStart(s, 0, true) || CanStart(s, 1, true) || !CanStart(s, 3, false) || CanStart(s, 4, false) { t.Fatal("admission policy changed") } s.Paused = true if CanStart(s, 0, false) { t.Fatal("paused worker admitted") } }