test(filter): fold active-subscriber check into TestCleanup

Merge TestCleanupKeepsActiveSubscribers into TestCleanup as a single
subscription lifecycle: active (refreshed) subscriber survives cleanup,
then is removed once it goes idle past the timeout.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Igor Sirotin 2026-07-06 09:45:53 +01:00
parent bad12e44b6
commit 5ec810cd09
No known key found for this signature in database
GPG Key ID: 657CBF870237B455

View File

@ -107,8 +107,11 @@ func TestRemoveBogus(t *testing.T) {
require.Error(t, err)
}
// TestCleanup verifies that a subscriber which has not been refreshed within
// the idle timeout is removed by the periodic cleanup.
// TestCleanup verifies the idle-subscription cleanup lifecycle: a subscriber
// that keeps getting refreshed (e.g. via pings) survives the periodic cleanup
// while active, and is removed only once it goes idle past the timeout. This
// also guards against a regression where the idle check was inverted, deleting
// active subscribers while keeping idle ones forever.
func TestCleanup(t *testing.T) {
subs := NewSubscribersMap(1 * time.Second)
@ -118,7 +121,6 @@ func TestCleanup(t *testing.T) {
go subs.cleanUp(ctx, 100*time.Millisecond)
peerId := createPeerID(t)
subs.Set(peerId, PUBSUB_TOPIC, []string{"topic1", "topic2"})
hasSubs := subs.Has(peerId)
@ -127,33 +129,6 @@ func TestCleanup(t *testing.T) {
_, exists := subs.Get(peerId)
require.True(t, exists)
// Let the subscriber go idle past the timeout; it must be cleaned up.
time.Sleep(1500 * time.Millisecond)
hasSubs = subs.Has(peerId)
require.False(t, hasSubs)
_, exists = subs.Get(peerId)
require.False(t, exists)
}
// TestCleanupKeepsActiveSubscribers verifies that a subscriber which keeps
// getting refreshed (e.g. via pings) survives the periodic cleanup, and is
// only removed once it goes idle past the timeout. This guards against a
// regression where the idle check was inverted and active subscribers were
// deleted while idle ones were kept forever.
func TestCleanupKeepsActiveSubscribers(t *testing.T) {
timeout := 1 * time.Second
subs := NewSubscribersMap(timeout)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go subs.cleanUp(ctx, 100*time.Millisecond)
peerId := createPeerID(t)
subs.Set(peerId, PUBSUB_TOPIC, []string{"topic1", "topic2"})
// Keep the subscriber active for well over the timeout by refreshing it
// periodically. It must never be cleaned up while active.
for i := 0; i < 15; i++ {
@ -166,4 +141,7 @@ func TestCleanupKeepsActiveSubscribers(t *testing.T) {
require.Eventually(t, func() bool {
return !subs.Has(peerId)
}, 3*time.Second, 100*time.Millisecond, "idle subscriber was not cleaned up")
_, exists = subs.Get(peerId)
require.False(t, exists)
}