89 lines
2.8 KiB
Go

package main
import (
"context"
"droboticflow/internal/store"
"go.mongodb.org/mongo-driver/mongo"
"testing"
"time"
)
func TestPassDelayPreservesDefaultsAndPacesBackfill(t *testing.T) {
for _, tc := range []struct {
name, mode string
ready bool
elapsed time.Duration
minimum time.Duration
want time.Duration
}{
{"default backlog", "periodic", false, time.Second, 0, 0},
{"default idle", "periodic", true, time.Second, 0, time.Minute},
{"legacy continuous", "continuous", false, time.Second, 0, time.Minute},
{"paced bootstrap", "periodic", false, time.Second, 5 * time.Second, 4 * time.Second},
{"slow pass already paid budget", "periodic", false, 6 * time.Second, 5 * time.Second, 0},
{"small remaining budget", "periodic", false, 4999 * time.Millisecond, 5 * time.Second, 100 * time.Millisecond},
{"idle cadence retained", "periodic", true, time.Second, 5 * time.Second, time.Minute},
} {
t.Run(tc.name, func(t *testing.T) {
if got := passDelay(tc.mode, tc.ready, tc.elapsed, time.Minute, tc.minimum); got != tc.want {
t.Fatalf("delay=%s want=%s", got, tc.want)
}
})
}
}
func TestPacingRejectsUnboundedOrInvalidConfiguration(t *testing.T) {
for _, tc := range []struct {
batch int
min time.Duration
want bool
}{{200, 0, true}, {1, time.Minute, true}, {50, 5 * time.Second, true}, {0, 0, false}, {201, 0, false}, {50, -1, false}, {50, time.Minute + 1, false}} {
if got := validPacing(tc.batch, tc.min); got != tc.want {
t.Fatalf("batch=%d minimum=%s valid=%v want=%v", tc.batch, tc.min, got, tc.want)
}
}
}
func TestDaemonRetryDelayAndAdaptiveBackfillCooldown(t *testing.T) {
for _, tc := range []struct {
attempt int
want time.Duration
}{
{-1, 2 * time.Second},
{0, 2 * time.Second},
{1, 4 * time.Second},
{2, 8 * time.Second},
{3, 15 * time.Second},
{10, 15 * time.Second},
} {
if got := daemonRetryDelay(tc.attempt); got != tc.want {
t.Fatalf("attempt=%d got=%s want=%s", tc.attempt, got, tc.want)
}
}
for _, tc := range []struct {
ready bool
elapsed time.Duration
want time.Duration
}{
{true, 5 * time.Second, 0},
{false, 1999 * time.Millisecond, 0},
{false, 2 * time.Second, 500 * time.Millisecond},
{false, 4 * time.Second, 500 * time.Millisecond},
} {
if got := adaptiveBackfillCooldown(tc.ready, tc.elapsed); got != tc.want {
t.Fatalf("ready=%v elapsed=%s got=%s want=%s", tc.ready, tc.elapsed, got, tc.want)
}
}
}
func TestDaemonRetryDoesNotHideHardFailures(t *testing.T) {
for _, e := range []error{store.ErrQueryReplicaLease, store.ErrQueryReplicaUnsupported, mongo.CommandError{Code: 13}, mongo.WriteException{WriteConcernError: &mongo.WriteConcernError{Code: 64}}} {
if daemonRetryable(e) {
t.Fatal("hard failure retried", e)
}
}
if !daemonRetryable(context.DeadlineExceeded) {
t.Fatal("read timeout stopped daemon")
}
}