Skip to content

Commit 5f758ba

Browse files
committed
Fix pubsub channels
This was a cherry-pick of 9958e2d Only the unit test was needed to be back-ported
1 parent 8bf26fb commit 5f758ba

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Tests/RediStackIntegrationTests/Commands/PubSubCommandsTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,41 @@ final class RedisPubSubCommandsTests: RediStackIntegrationTestCase {
188188

189189
self.waitForExpectations(timeout: 1)
190190
}
191+
192+
func test_pubSubChannels() throws {
193+
let fn = #function
194+
let subscriber = try self.makeNewConnection()
195+
defer { try? subscriber.close().wait() }
196+
197+
let channelNames = (1...10).map {
198+
RedisChannelName("\(fn)\($0)\($0 % 2 == 0 ? "_even" : "_odd")")
199+
}
200+
201+
for channelName in channelNames {
202+
try subscriber.subscribe(
203+
to: channelName,
204+
messageReceiver: { _, _ in },
205+
onSubscribe: nil,
206+
onUnsubscribe: nil
207+
).wait()
208+
}
209+
XCTAssertTrue(subscriber.isSubscribed)
210+
defer {
211+
// Unsubscribe (clean up)
212+
try? subscriber.unsubscribe(from: channelNames).wait()
213+
XCTAssertFalse(subscriber.isSubscribed)
214+
}
215+
216+
// Make another connection to query on.
217+
let queryConnection = try self.makeNewConnection()
218+
defer { try? queryConnection.close().wait() }
219+
220+
let oddChannels = try queryConnection.activeChannels(matching: "\(fn)*_odd").wait()
221+
XCTAssertEqual(oddChannels.count, channelNames.count / 2)
222+
223+
let allChannels = try queryConnection.activeChannels().wait()
224+
XCTAssertGreaterThanOrEqual(allChannels.count, channelNames.count)
225+
}
191226
}
192227

193228
final class RedisPubSubCommandsPoolTests: RediStackConnectionPoolIntegrationTestCase {

0 commit comments

Comments
 (0)