-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Issue #2763 ## Solution Simply added a lock around `_handlers` in `ConnectionMultiplexer.Subscription`, like I was suggesting in the issue. ## Unit Test I added one that does exactly what the example code in #2763 was doing & testing for. I used the other tests as template/guide, let me know if something isn't up to spec. --------- Co-authored-by: Nick Craver <[email protected]>
- Loading branch information
1 parent
c0bb4eb
commit fe40d17
Showing
3 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace StackExchange.Redis.Tests.Issues | ||
{ | ||
public class Issue2763Tests : TestBase | ||
{ | ||
public Issue2763Tests(ITestOutputHelper output) : base(output) { } | ||
|
||
[Fact] | ||
public void Execute() | ||
{ | ||
using var conn = Create(); | ||
var subscriber = conn.GetSubscriber(); | ||
|
||
static void Handler(RedisChannel c, RedisValue v) { } | ||
|
||
const int COUNT = 1000; | ||
RedisChannel channel = RedisChannel.Literal("CHANNEL:TEST"); | ||
|
||
List<Action> subscribes = new List<Action>(COUNT); | ||
for (int i = 0; i < COUNT; i++) | ||
subscribes.Add(() => subscriber.Subscribe(channel, Handler)); | ||
Parallel.ForEach(subscribes, action => action()); | ||
|
||
Assert.Equal(COUNT, CountSubscriptionsForChannel(subscriber, channel)); | ||
|
||
List<Action> unsubscribes = new List<Action>(COUNT); | ||
for (int i = 0; i < COUNT; i++) | ||
unsubscribes.Add(() => subscriber.Unsubscribe(channel, Handler)); | ||
Parallel.ForEach(unsubscribes, action => action()); | ||
|
||
Assert.Equal(0, CountSubscriptionsForChannel(subscriber, channel)); | ||
} | ||
|
||
private static int CountSubscriptionsForChannel(ISubscriber subscriber, RedisChannel channel) | ||
{ | ||
ConnectionMultiplexer connMultiplexer = (ConnectionMultiplexer)subscriber.Multiplexer; | ||
connMultiplexer.GetSubscriberCounts(channel, out int handlers, out int _); | ||
return handlers; | ||
} | ||
} | ||
} |