File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed
Sources/RediStack/Commands
Tests/RediStackIntegrationTests/Commands Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -37,8 +37,11 @@ extension RedisCommand {
37
37
/// - Invariant: If no `match` pattern is provided, all active channels will be returned.
38
38
/// - Parameter match: An optional pattern of channel names to filter for.
39
39
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)
42
45
}
43
46
44
47
/// [PUBSUB NUMPAT](https://redis.io/commands/pubsub#codepubsub-numpatcode)
Original file line number Diff line number Diff line change @@ -189,6 +189,41 @@ final class RedisPubSubCommandsTests: RediStackIntegrationTestCase {
189
189
190
190
self . waitForExpectations ( timeout: 1 )
191
191
}
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
+ }
192
227
}
193
228
194
229
final class RedisPubSubCommandsPoolTests : RediStackConnectionPoolIntegrationTestCase {
You can’t perform that action at this time.
0 commit comments