Skip to content

Commit

Permalink
Try to sweep the issue under the carpet, close #108.
Browse files Browse the repository at this point in the history
  • Loading branch information
yallie committed Dec 12, 2024
1 parent 5db34cc commit 3778515
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions CoreRemoting.Tests/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using CoreRemoting.Authentication;
using CoreRemoting.Tests.Tools;
using CoreRemoting.Toolbox;
using Xunit;

namespace CoreRemoting.Tests
Expand All @@ -23,58 +25,48 @@ public SessionTests(ServerFixture serverFixture)
}

[Fact]
public void Client_Connect_should_create_new_session_AND_Disconnect_should_close_session()
public async Task Client_Connect_should_create_new_session_AND_Disconnect_should_close_session()
{
var connectedWaitHandles = new[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
var clientStarted1 = new TaskCompletionSource();
var clientStarted2 = new TaskCompletionSource();
var clientStopSignal = new TaskCompletionSource();

var quitWaitHandle = new ManualResetEventSlim();

var clientAction = new Action<int>(index =>
async Task ClientTask(TaskCompletionSource connected)
{
var client =
new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort
});
var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort
});

Assert.False(client.HasSession);
client.Connect();

connectedWaitHandles[index].Set();

connected.TrySetResult();
Assert.True(client.HasSession);

quitWaitHandle.Wait();

await clientStopSignal.Task;
client.Dispose();
});

var clientThread1 = new Thread(() => clientAction(0));
var clientThread2 = new Thread(() => clientAction(1));
}

// There should be no sessions, before both clients connected
Assert.Empty(_serverFixture.Server.SessionRepository.Sessions);

// Start two clients to create two sessions
clientThread1.Start();
clientThread2.Start();
var client1 = ClientTask(clientStarted1);
var client2 = ClientTask(clientStarted2);

// Wait for connection of both clients
WaitHandle.WaitAll(connectedWaitHandles);
await Task.WhenAll(clientStarted1.Task, clientStarted2.Task).Timeout(1);

Assert.Equal(2, _serverFixture.Server.SessionRepository.Sessions.Count());

quitWaitHandle.Set();
clientStopSignal.TrySetResult();

clientThread1.Join();
clientThread2.Join();
await Task.WhenAll(client1, client2, Task.Delay(100)).Timeout(1);

// There should be no sessions left, after both clients disconnedted
// There should be no sessions left, after both clients disconnected
Assert.Empty(_serverFixture.Server.SessionRepository.Sessions);
}

Expand Down

0 comments on commit 3778515

Please sign in to comment.