Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Fixed Integration Tests
Browse files Browse the repository at this point in the history
Given that the Server instance is a class instance, it's used by all the tests in the class and if the tests run in parallel then some asserts could fail if we are considering that the connected clients of the server are only the clients that the current test connected.
To avoid this we should always query the Server.ActiveClients property (and not the Server.ActiveConnections) and filtering by the client ids connected in the test.
  • Loading branch information
mauroa authored and kzu committed Jul 31, 2019
1 parent 5c9dca4 commit a51d878
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/IntegrationTests/ConnectionSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ public async Task when_clients_connect_and_disconnect_then_server_raises_events(
public async Task when_connect_clients_and_one_client_drops_connection_then_other_client_survives()
{
var fooClient = await GetClientAsync();
var fooClientId = GetClientId();
var barClient = await GetClientAsync();
var barClientId = GetClientId();

await fooClient.ConnectAsync(new MqttClientCredentials(GetClientId()));
await barClient.ConnectAsync(new MqttClientCredentials(GetClientId()));
await fooClient.ConnectAsync(new MqttClientCredentials(fooClientId));
await barClient.ConnectAsync(new MqttClientCredentials(barClientId));

var initialConnectedClients = server.ActiveClients.Count();
var clientIds = new List<string> { fooClientId, barClientId };
var initialConnectedClients = server.ActiveClients.Where(c => clientIds.Contains(c)).Count();
var exceptionThrown = false;

try
Expand All @@ -110,18 +113,19 @@ public async Task when_connect_clients_and_one_client_drops_connection_then_othe

while (!serverSignal.IsSet)
{
if (server.ActiveConnections == 1 && server.ActiveClients.Count() == 1)
if (!server.ActiveClients.Any(c => c == fooClientId))
{
serverSignal.Set();
}
}

serverSignal.Wait();

var finalConnectedClients = server.ActiveClients.Where(c => clientIds.Contains(c)).Count();

Assert.Equal(2, initialConnectedClients);
Assert.True(exceptionThrown);
Assert.Equal(1, server.ActiveConnections);
Assert.Equal(1, server.ActiveClients.Count());
Assert.Equal(1, finalConnectedClients);

fooClient.Dispose();
barClient.Dispose();
Expand Down Expand Up @@ -167,8 +171,7 @@ public async Task when_connecting_twice_with_same_client_with_disconnecting_then
await client.DisconnectAsync();
await client.ConnectAsync(new MqttClientCredentials(clientId));

Assert.Equal(1, server.ActiveConnections);
Assert.Equal(1, server.ActiveClients.Count());
Assert.Equal(1, server.ActiveClients.Count(c => c == clientId));

client.Dispose();
}
Expand Down

0 comments on commit a51d878

Please sign in to comment.