Skip to content

Commit

Permalink
Demonstrate failing event dispatch when one of the clients disconnect…
Browse files Browse the repository at this point in the history
…s unexpectedly.
  • Loading branch information
yallie committed Nov 21, 2024
1 parent be91e7a commit 04b7675
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
56 changes: 55 additions & 1 deletion CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using CoreRemoting.Channels.Websocket;
using CoreRemoting.DependencyInjection;
using CoreRemoting.Serialization;
using CoreRemoting.Tests.ExternalTypes;
using CoreRemoting.Tests.Tools;
Expand Down Expand Up @@ -163,7 +166,7 @@ void ClientAction()
clientThread.Start();
clientThread.Join();

_serverFixture.Server.Config.MessageEncryption = true;
_serverFixture.Server.Config.MessageEncryption = false;

Assert.True(_remoteServiceCalled);
Assert.Equal(0, _serverFixture.ServerErrorCount);
Expand Down Expand Up @@ -509,5 +512,56 @@ public void NonSerializableError_method_throws_Exception()
_serverFixture.ServerErrorCount = 0;
}
}

[Fact]
public async Task Disposed_client_subscription_doesnt_break_other_clients()
{
async Task roundtrip(bool encryption)
{
var oldEncryption = _serverFixture.Server.Config.MessageEncryption;
_serverFixture.Server.Config.MessageEncryption = encryption;

try
{
RemotingClient createClient() => new RemotingClient(new ClientConfig()
{
ServerPort = _serverFixture.Server.Config.NetworkPort,
MessageEncryption = encryption,
});

using var client1 = createClient();
using var client2 = createClient();

client1.Connect();
client2.Connect();

var proxy1 = client1.CreateProxy<ITestService>();
var fired1 = new TaskCompletionSource<bool>();
proxy1.ServiceEvent += () => fired1.TrySetResult(true);

var proxy2 = client2.CreateProxy<ITestService>();
var fired2 = new TaskCompletionSource<bool>();
proxy2.ServiceEvent += () => fired2.TrySetResult(true);

// early disposal, proxy1 subscription isn't canceled
client1.Disconnect();

proxy2.FireServiceEvent();
Assert.True(await fired2.Task);
Assert.True(fired2.Task.IsCompleted);
Assert.False(fired1.Task.IsCompleted);
}
finally
{
_serverFixture.Server.Config.MessageEncryption = oldEncryption;
}
}

// works!
await roundtrip(encryption: false);

// fails!
await roundtrip(encryption: true);
}
}
}
4 changes: 4 additions & 0 deletions CoreRemoting.Tests/ServerFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using CoreRemoting.DependencyInjection;
using CoreRemoting.Tests.Tools;
using Xunit;
Expand Down Expand Up @@ -65,7 +66,10 @@ public ServerFixture()
public void Dispose()
{
if (Server != null)
{
Thread.Sleep(100); // work around WatsonTcp 6.0.2 bug
Server.Dispose();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion CoreRemoting/RemotingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ internal RemotingSession(int keySize, byte[] clientPublicKey, IRemotingServer se
HandlerKey = handlerKey,
DelegateArguments = arguments
};

var remoteDelegateInvocationWebsocketMessage =
_server.MessageEncryptionManager
.CreateWireMessage(
Expand Down

0 comments on commit 04b7675

Please sign in to comment.