From 5586efeae7939e60ff02b7e7caa22e0de8c15685 Mon Sep 17 00:00:00 2001 From: Bahtya Date: Thu, 9 Apr 2026 20:11:53 +0800 Subject: [PATCH] test: add test for CancelationPubSub error path Add TestCancelationPubSubReceiveError to verify that when Receive() fails in CancelationPubSub(), an error is returned and the pubsub connection is not leaked. This provides test coverage for the pubsub.Close() fix that was missing in the previous commit. Bahtya --- internal/rdb/rdb_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/rdb/rdb_test.go b/internal/rdb/rdb_test.go index 5249a29..eed4ad3 100644 --- a/internal/rdb/rdb_test.go +++ b/internal/rdb/rdb_test.go @@ -3274,6 +3274,29 @@ func TestCancelationPubSub(t *testing.T) { mu.Unlock() } +func TestCancelationPubSubReceiveError(t *testing.T) { + // Use a client connected to a non-existent Redis server to trigger + // a Receive() error. This verifies that the pubsub connection is + // closed on error, preventing connection leaks. + client := redis.NewClient(&redis.Options{ + Addr: "localhost:0", // invalid port — connection will fail + }) + r := NewRDB(client) + defer r.Close() + + pubsub, err := r.CancelationPubSub() + if err == nil { + // If no error, we must clean up the pubsub. + if pubsub != nil { + pubsub.Close() + } + t.Fatal("(*RDB).CancelationPubSub() expected to return an error when redis is unreachable") + } + if pubsub != nil { + t.Error("(*RDB).CancelationPubSub() expected nil pubsub on error") + } +} + func TestWriteResult(t *testing.T) { r := setup(t) defer r.Close()