Skip to content

Commit 9958e2d

Browse files
committed
Fix pubsub channels
1 parent f0d123f commit 9958e2d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Sources/RediStack/Commands/PubSubCommands.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ extension RedisCommand {
3737
/// - Invariant: If no `match` pattern is provided, all active channels will be returned.
3838
/// - Parameter match: An optional pattern of channel names to filter for.
3939
public static func pubsubChannels(matching match: String? = nil) -> RedisCommand<[RedisChannelName]> {
40-
let args: [RESPValue] = match.map { [.init(bulk: $0)] } ?? []
41-
return .init(keyword: "PUBSUB CHANNELS", arguments: args)
40+
var args: [RESPValue] = [.init(bulk: "CHANNELS")]
41+
if let match = match {
42+
args.append(.init(bulk: match))
43+
}
44+
return .init(keyword: "PUBSUB", arguments: args)
4245
}
4346

4447
/// [PUBSUB NUMPAT](https://redis.io/commands/pubsub#codepubsub-numpatcode)

Tests/RediStackIntegrationTests/Commands/PubSubCommandsTests.swift

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

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

194229
final class RedisPubSubCommandsPoolTests: RediStackConnectionPoolIntegrationTestCase {

0 commit comments

Comments
 (0)