Skip to content

Commit

Permalink
Fixed additional potential places where incrementing values could fai…
Browse files Browse the repository at this point in the history
…l in concurrent situations.
  • Loading branch information
DJGosnell committed Sep 17, 2023
1 parent fc930c4 commit e6a8040
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/NexNet.IntegrationTests/NexusServerTests_NexusInvocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public async Task NexusInvokesOnAll(Type type)
connectedNexus.OnConnectedEvent = async nexus =>
{
// Second connection
if (++connectedCount == 2)
if (Interlocked.Increment(ref connectedCount) == 2)
{
await nexus.Context.Clients.All.ClientTask();
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public async Task NexusInvokesOnGroup(Type type)
{
nexus.Context.Groups.Add("group");
// Second connection
if (++connectedCount == 2)
if (Interlocked.Increment(ref connectedCount) == 2)
{
await nexus.Context.Clients.Group("group").ClientTask();
}
Expand Down Expand Up @@ -234,7 +234,7 @@ public async Task NexusInvokesOnGroups(Type type)
{
connectedNexus.OnConnectedEvent = async nexus =>
{
if (++connectedCount == 1) {
if (Interlocked.Increment(ref connectedCount) == 1) {
nexus.Context.Groups.Add("group");
}
// Second connection
Expand Down Expand Up @@ -276,7 +276,7 @@ public async Task NexusInvokesOnOthers(Type type)
connectedNexus.OnConnectedEvent = async nexus =>
{
// Second connection
if (++connectedCount == 2)
if (Interlocked.Increment(ref connectedCount) == 2)
{
await nexus.Context.Clients.Others.ClientTask();
await Task.Delay(10);
Expand All @@ -288,8 +288,8 @@ public async Task NexusInvokesOnOthers(Type type)
var (client1, clientNexus1) = CreateClient(CreateClientConfig(type));
var (client2, clientNexus2) = CreateClient(CreateClientConfig(type));
#pragma warning disable CS1998
clientNexus1.ClientTaskEvent = async _ => invocationCount++;
clientNexus2.ClientTaskEvent = async _ => invocationCount++;
clientNexus1.ClientTaskEvent = async _ => Interlocked.Increment(ref invocationCount);
clientNexus2.ClientTaskEvent = async _ => Interlocked.Increment(ref invocationCount);
#pragma warning restore CS1998
await server.StartAsync().Timeout(1);

Expand All @@ -316,7 +316,7 @@ public async Task NexusInvokesOnClient(Type type)
connectedNexus.OnConnectedEvent = async nexus =>
{
// Second connection
if (++connectedCount == 2)
if (Interlocked.Increment(ref connectedCount) == 2)
{
await nexus.Context.Clients.Client(nexus.Context.Id).ClientTask();
await Task.Delay(10);
Expand All @@ -328,8 +328,8 @@ public async Task NexusInvokesOnClient(Type type)
var (client1, clientNexus1) = CreateClient(CreateClientConfig(type));
var (client2, clientNexus2) = CreateClient(CreateClientConfig(type));
#pragma warning disable CS1998
clientNexus1.ClientTaskEvent = async _ => invocationCount++;
clientNexus2.ClientTaskEvent = async _ => invocationCount++;
clientNexus1.ClientTaskEvent = async _ => Interlocked.Increment(ref invocationCount);
clientNexus2.ClientTaskEvent = async _ => Interlocked.Increment(ref invocationCount);
#pragma warning restore CS1998
await server.StartAsync().Timeout(1);

Expand All @@ -356,7 +356,7 @@ public async Task NexusInvokesOnClients(Type type)
connectedNexus.OnConnectedEvent = async nexus =>
{
// Second connection
if (++connectedCount == 2)
if (Interlocked.Increment(ref connectedCount) == 2)
{
// ReSharper disable once AccessToModifiedClosure
var clientIds = server!.GetContext().Clients.GetIds().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task WriterChunks()

messenger.OnSendCustomHeaderWithBody = (type, header, body, token) =>
{
if (++invocations == 2)
if (Interlocked.Increment(ref invocations) == 2)
tcs.SetResult();
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task Server_PipeReaderReceivesDataMultipleTimesWithLargeData(Type t
var result = await pipe.Input.ReadAsync().Timeout(1);
pipe.Input.AdvanceTo(result.Buffer.End);
if (++count == iterations)
if (Interlocked.Increment(ref count) == iterations)
tcs.SetResult();
};

Expand Down

0 comments on commit e6a8040

Please sign in to comment.