Skip to content

Commit 3ca471b

Browse files
PeterAdams-AMordil
authored andcommitted
Get pubsub numsub working
1 parent e08b426 commit 3ca471b

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

Sources/RediStack/Commands/PubSubCommands.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ extension RedisCommand {
5252
/// [PUBSUB NUMSUB](https://redis.io/commands/pubsub#codepubsub-numsub-channel-1--channel-ncode)
5353
/// - Parameter channels: A list of channel names to collect the subscriber counts for.
5454
public static func pubsubNumsub(forChannels channels: [RedisChannelName]) -> RedisCommand<[RedisChannelName: Int]> {
55-
let args = channels.map { $0.convertedToRESPValue() }
56-
return .init(keyword: "PUBSUB NUMSUB", arguments: args) {
55+
var args: [RESPValue] = [.init(bulk: "NUMSUB")]
56+
args.append(convertingContentsOf: channels)
57+
return .init(keyword: "PUBSUB", arguments: args) {
5758
let response = try $0.map(to: [RESPValue].self)
5859
assert(response.count == channels.count * 2, "Unexpected response size!")
5960

@@ -62,11 +63,12 @@ extension RedisCommand {
6263
return try channels
6364
.enumerated()
6465
.reduce(into: [:]) { (result, next) in
65-
assert(next.element.rawValue == response[next.offset].string, "Unexpected value in current index!")
66+
let responseOffset = next.offset * 2
67+
assert(next.element.rawValue == response[responseOffset].string, "Unexpected value in current index!")
6668

67-
guard let count = response[next.offset + 1].int else {
69+
guard let count = response[responseOffset + 1].int else {
6870
throw RedisClientError.assertionFailure(
69-
message: "Unexpected value at position \(next.offset + 1) in \(response)"
71+
message: "Unexpected value at position \(responseOffset + 1) in \(response)"
7072
)
7173
}
7274
result[next.element] = count

Tests/RediStackIntegrationTests/Commands/PubSubCommandsTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,42 @@ final class RedisPubSubCommandsTests: RediStackIntegrationTestCase {
232232
let allChannels = try queryConnection.send(.pubsubChannels()).wait()
233233
XCTAssertGreaterThanOrEqual(allChannels.count, channelNames.count)
234234
}
235+
236+
func test_pubSubNumsub() throws {
237+
let fn = #function
238+
let subscriber = try self.makeNewConnection()
239+
defer { try? subscriber.close().wait() }
240+
241+
let channelNames = (1...5).map {
242+
RedisChannelName("\(fn)\($0)")
243+
}
244+
245+
for channelName in channelNames {
246+
try subscriber.subscribe(
247+
to: channelName,
248+
messageReceiver: { _, _ in },
249+
onSubscribe: nil,
250+
onUnsubscribe: nil
251+
).wait()
252+
}
253+
XCTAssertTrue(subscriber.isSubscribed)
254+
defer {
255+
// Unsubscribe (clean up)
256+
try? subscriber.unsubscribe(from: channelNames).wait()
257+
XCTAssertFalse(subscriber.isSubscribed)
258+
}
259+
260+
// Make another connection to query on.
261+
let queryConnection = try self.makeNewConnection()
262+
defer { try? queryConnection.close().wait() }
263+
264+
let notSubscribedChannel = RedisChannelName("\(fn)_notsubbed")
265+
let numSubs = try queryConnection.send(.pubsubNumsub(forChannels: [channelNames[0], notSubscribedChannel])).wait()
266+
XCTAssertEqual(numSubs.count, 2)
267+
268+
XCTAssertGreaterThanOrEqual(numSubs[channelNames[0]] ?? 0, 1)
269+
XCTAssertEqual(numSubs[notSubscribedChannel], 0)
270+
}
235271
}
236272

237273
final class RedisPubSubCommandsPoolTests: RediStackConnectionPoolIntegrationTestCase {

0 commit comments

Comments
 (0)