Skip to content

Commit ccb335a

Browse files
committed
fix flaky tests
1 parent c38486d commit ccb335a

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

map_broker_redis_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,7 +4003,7 @@ func TestRedisMapBroker_RefreshTTLOnSuppress_RefreshesMetaAndStream(t *testing.T
40034003
require.Greater(t, metaBefore, int64(0), "meta_key must have a TTL set")
40044004
require.Greater(t, streamBefore, int64(0), "stream_key must have a TTL set")
40054005

4006-
// Burn enough time for any TTL refresh to be observable above clock noise.
4006+
// Burn enough time for a missing refresh to be visible above clock noise.
40074007
time.Sleep(150 * time.Millisecond)
40084008

40094009
// Suppressed keepalive: same key, if_new + refresh_ttl_on_suppress.
@@ -4016,16 +4016,28 @@ func TestRedisMapBroker_RefreshTTLOnSuppress_RefreshesMetaAndStream(t *testing.T
40164016
require.True(t, res.Suppressed)
40174017
require.Equal(t, SuppressReasonKeyExists, res.SuppressReason)
40184018

4019-
metaAfter := pttl(metaKey)
4020-
streamAfter := pttl(streamKey)
4021-
// PTTL should be reset close to the configured TTL again — strictly
4022-
// greater than the value captured before the sleep.
4023-
require.Greater(t, metaAfter, metaBefore-100,
4024-
"meta_key TTL must be refreshed by suppressed keepalive (was %d, now %d)", metaBefore, metaAfter)
4025-
require.Greater(t, metaAfter, int64(60_000),
4026-
"meta_key TTL must be near MetaTTL after refresh, got %d ms", metaAfter)
4027-
require.Greater(t, streamAfter, streamBefore-100,
4028-
"stream_key TTL must be refreshed by suppressed keepalive (was %d, now %d)", streamBefore, streamAfter)
4019+
// Both PTTLs must read back near their configured TTLs. The keepalive
4020+
// is idempotent, so retry it: a single shot can be misread when the
4021+
// publish + PTTL round-trip is slow, which makes a genuinely refreshed
4022+
// TTL look decayed. Without a refresh the TTLs only keep falling, so a
4023+
// broken branch still fails here — by timeout, never by luck.
4024+
pttlAbove := func(key string, minMS int64) bool {
4025+
ms, err := client.Do(ctx, client.B().Pttl().Key(key).Build()).AsInt64()
4026+
return err == nil && ms > minMS
4027+
}
4028+
require.Eventually(t, func() bool {
4029+
_, err := broker.Publish(ctx, channel, "k", MapPublishOptions{
4030+
Data: []byte("v1"),
4031+
KeyMode: KeyModeIfNew,
4032+
RefreshTTLOnSuppress: true,
4033+
})
4034+
if err != nil {
4035+
return false
4036+
}
4037+
return pttlAbove(metaKey, 119_900) && pttlAbove(streamKey, 59_900)
4038+
}, 5*time.Second, 100*time.Millisecond,
4039+
"meta_key (was %d) and stream_key (was %d) TTLs must be refreshed by suppressed keepalive",
4040+
metaBefore, streamBefore)
40294041
})
40304042
}
40314043

writer_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,13 @@ func TestQueueReclaimsCapacityWhenIdle(t *testing.T) {
622622
require.NoError(t, n.hub.broadcastPublication(
623623
ch, sp, &Publication{Data: []byte(`{"a":1}`)}, nil, nil, ChannelBatchConfig{}))
624624
}
625-
time.Sleep(300 * time.Millisecond)
625+
// Wait for the drain itself rather than for a fixed wall-clock delay: the
626+
// shrink is deferred from the moment the queue goes empty, so polling until
627+
// then leaves the whole shrink delay as slack for the check below. Sleeping
628+
// a fixed amount instead spends an unknown part of that delay on the drain.
629+
require.Eventually(t, func() bool {
630+
return c.messageWriter.messages.Len() == 0
631+
}, 5*time.Second, 5*time.Millisecond, "writer never drained the queued messages")
626632
require.Greater(t, c.messageWriter.messages.Cap(), 2,
627633
"a busy connection should hold its ring rather than rebuild it every frame")
628634

0 commit comments

Comments
 (0)