From 16c24251846fb17335bd6c8bf6870a4b2ab71422 Mon Sep 17 00:00:00 2001 From: Julian Sommer <115516918+juliansommer@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:11:10 +1000 Subject: [PATCH] fix: stop the utils tests waiting out production defaults The reconnect tests paid the client's one-second default sleep between attempts, which ConnectNATS already lets a caller override; the retry test scales its floor with the delay it sets, so it still proves a retry happened. 7.9s to 5.0s. --- spinifex/utils/nats_test.go | 12 +++++++++--- spinifex/utils/process_test.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spinifex/utils/nats_test.go b/spinifex/utils/nats_test.go index d328f5a2f..51f426ab1 100644 --- a/spinifex/utils/nats_test.go +++ b/spinifex/utils/nats_test.go @@ -471,14 +471,16 @@ func TestConnectNATSWithRetry_Success(t *testing.T) { func TestConnectNATSWithRetry_RetriesOnFailure(t *testing.T) { start := time.Now() _, err := ConnectNATSWithRetry("nats://127.0.0.1:14222", "", "", - WithMaxWait(500*time.Millisecond), - WithRetryDelay(50*time.Millisecond), + WithMaxWait(100*time.Millisecond), + WithRetryDelay(20*time.Millisecond), ) elapsed := time.Since(start) require.Error(t, err) assert.Contains(t, err.Error(), "NATS connect failed") - assert.GreaterOrEqual(t, elapsed, 100*time.Millisecond, "should have retried at least once") + // Two attempts sleep 20ms then 40ms, so anything past the first delay + // alone can only have come from a retry. + assert.GreaterOrEqual(t, elapsed, 40*time.Millisecond, "should have retried at least once") assert.Less(t, elapsed, 5*time.Second, "should fail within a few seconds") } @@ -529,6 +531,9 @@ func TestConnectNATS_ReconnectCallbackFires(t *testing.T) { reconnected := make(chan struct{}, 1) nc, err := ConnectNATS(ns.ClientURL(), "", "", + // The default sleep between reconnect attempts is a second, which is + // most of this test's runtime and none of what it proves. + WithReconnectWait(50*time.Millisecond), WithReconnectHandler(func(_ *nats.Conn) { select { case reconnected <- struct{}{}: @@ -603,6 +608,7 @@ func TestConnectNATS_DisconnectReconnectCallbacks(t *testing.T) { reconnects := make(chan struct{}, 4) nc, err := ConnectNATS("nats://127.0.0.1:"+strconv.Itoa(port), "", "", + WithReconnectWait(50*time.Millisecond), WithDisconnectHandler(func(_ *nats.Conn, _ error) { disconnects <- struct{}{} }), WithReconnectHandler(func(_ *nats.Conn) { reconnects <- struct{}{} }), ) diff --git a/spinifex/utils/process_test.go b/spinifex/utils/process_test.go index ccaec5c8b..f5c121e75 100644 --- a/spinifex/utils/process_test.go +++ b/spinifex/utils/process_test.go @@ -57,7 +57,7 @@ func TestKillProcessEscalatesToSigkillWithinDeadline(t *testing.T) { require.Equal(t, "ready", scanner.Text()) const pollInterval = 20 * time.Millisecond - const gracePeriod = 300 * time.Millisecond + const gracePeriod = 100 * time.Millisecond start := time.Now() err = killProcessWithTiming(pid, pollInterval, gracePeriod)