Skip to content

Commit edadec9

Browse files
committed
Get pubsub numsub working
This was a cherry-pick of 3ca471b
1 parent a0af03c commit edadec9

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

Sources/RediStack/Commands/PubSubCommands.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ extension RedisClient {
8888
return try channels
8989
.enumerated()
9090
.reduce(into: [:]) { (result, next) in
91-
assert(next.element.rawValue == response[next.offset].string, "Unexpected value in current index!")
91+
let responseOffset = next.offset * 2
92+
assert(next.element.rawValue == response[responseOffset].string, "Unexpected value in current index!")
9293

93-
guard let count = response[next.offset + 1].int else {
94+
guard let count = response[responseOffset + 1].int else {
9495
throw RedisClientError.assertionFailure(
95-
message: "Unexpected value at position \(next.offset + 1) in \(response)"
96+
message: "Unexpected value at position \(responseOffset + 1) in \(response)"
9697
)
9798
}
9899
result[next.element] = count

Tests/RediStackIntegrationTests/Commands/PubSubCommandsTests.swift

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

236272
final class RedisPubSubCommandsPoolTests: RediStackConnectionPoolIntegrationTestCase {

0 commit comments

Comments
 (0)