4
4
using System ;
5
5
using System . Collections . Concurrent ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using System . IO ;
8
9
using System . Net ;
9
10
using System . Reflection ;
12
13
using StackExchange . Redis ;
13
14
using StackExchange . Redis . Maintenance ;
14
15
using StackExchange . Redis . Profiling ;
16
+ using Xunit ;
15
17
16
18
namespace Microsoft . AspNetCore . SignalR . Tests ;
17
19
@@ -244,6 +246,8 @@ public class TestRedisServer
244
246
245
247
public long Publish ( RedisChannel channel , RedisValue message , CommandFlags flags = CommandFlags . None )
246
248
{
249
+ AssertRedisChannel ( channel ) ;
250
+
247
251
if ( _subscriptions . TryGetValue ( channel , out var handlers ) )
248
252
{
249
253
lock ( handlers )
@@ -260,6 +264,8 @@ public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags
260
264
261
265
public void Subscribe ( ChannelMessageQueue messageQueue , int subscriberId , CommandFlags flags = CommandFlags . None )
262
266
{
267
+ AssertRedisChannel ( messageQueue . Channel ) ;
268
+
263
269
Action < RedisChannel , RedisValue > handler = ( channel , value ) =>
264
270
{
265
271
// Workaround for https://github.com/StackExchange/StackExchange.Redis/issues/969
@@ -280,6 +286,8 @@ public void Subscribe(ChannelMessageQueue messageQueue, int subscriberId, Comman
280
286
281
287
public void Unsubscribe ( RedisChannel channel , int subscriberId , CommandFlags flags = CommandFlags . None )
282
288
{
289
+ AssertRedisChannel ( channel ) ;
290
+
283
291
if ( _subscriptions . TryGetValue ( channel , out var list ) )
284
292
{
285
293
lock ( list )
@@ -288,6 +296,11 @@ public void Unsubscribe(RedisChannel channel, int subscriberId, CommandFlags fla
288
296
}
289
297
}
290
298
}
299
+
300
+ internal static void AssertRedisChannel ( RedisChannel channel )
301
+ {
302
+ Assert . False ( channel . IsPattern ) ;
303
+ }
291
304
}
292
305
293
306
public class TestSubscriber : ISubscriber
@@ -333,11 +346,15 @@ public Task<TimeSpan> PingAsync(CommandFlags flags = CommandFlags.None)
333
346
334
347
public long Publish ( RedisChannel channel , RedisValue message , CommandFlags flags = CommandFlags . None )
335
348
{
349
+ TestRedisServer . AssertRedisChannel ( channel ) ;
350
+
336
351
return _server . Publish ( channel , message , flags ) ;
337
352
}
338
353
339
354
public async Task < long > PublishAsync ( RedisChannel channel , RedisValue message , CommandFlags flags = CommandFlags . None )
340
355
{
356
+ TestRedisServer . AssertRedisChannel ( channel ) ;
357
+
341
358
await Task . Yield ( ) ;
342
359
return Publish ( channel , message , flags ) ;
343
360
}
@@ -349,6 +366,8 @@ public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> han
349
366
350
367
public Task SubscribeAsync ( RedisChannel channel , Action < RedisChannel , RedisValue > handler , CommandFlags flags = CommandFlags . None )
351
368
{
369
+ TestRedisServer . AssertRedisChannel ( channel ) ;
370
+
352
371
Subscribe ( channel , handler , flags ) ;
353
372
return Task . CompletedTask ;
354
373
}
@@ -365,6 +384,8 @@ public bool TryWait(Task task)
365
384
366
385
public void Unsubscribe ( RedisChannel channel , Action < RedisChannel , RedisValue > handler = null , CommandFlags flags = CommandFlags . None )
367
386
{
387
+ TestRedisServer . AssertRedisChannel ( channel ) ;
388
+
368
389
_server . Unsubscribe ( channel , _id , flags ) ;
369
390
}
370
391
@@ -380,6 +401,8 @@ public Task UnsubscribeAllAsync(CommandFlags flags = CommandFlags.None)
380
401
381
402
public Task UnsubscribeAsync ( RedisChannel channel , Action < RedisChannel , RedisValue > handler = null , CommandFlags flags = CommandFlags . None )
382
403
{
404
+ TestRedisServer . AssertRedisChannel ( channel ) ;
405
+
383
406
Unsubscribe ( channel , handler , flags ) ;
384
407
return Task . CompletedTask ;
385
408
}
@@ -401,6 +424,8 @@ public void WaitAll(params Task[] tasks)
401
424
402
425
public ChannelMessageQueue Subscribe ( RedisChannel channel , CommandFlags flags = CommandFlags . None )
403
426
{
427
+ TestRedisServer . AssertRedisChannel ( channel ) ;
428
+
404
429
// Workaround for https://github.com/StackExchange/StackExchange.Redis/issues/969
405
430
var redisSubscriberType = typeof ( RedisChannel ) . Assembly . GetType ( "StackExchange.Redis.RedisSubscriber" ) ;
406
431
var ctor = typeof ( ChannelMessageQueue ) . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic ,
@@ -414,6 +439,8 @@ public ChannelMessageQueue Subscribe(RedisChannel channel, CommandFlags flags =
414
439
415
440
public Task < ChannelMessageQueue > SubscribeAsync ( RedisChannel channel , CommandFlags flags = CommandFlags . None )
416
441
{
442
+ TestRedisServer . AssertRedisChannel ( channel ) ;
443
+
417
444
var t = Subscribe ( channel , flags ) ;
418
445
return Task . FromResult ( t ) ;
419
446
}
0 commit comments