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

Commit a51d878

Browse files
mauroakzu
authored andcommitted
Fixed Integration Tests
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.
1 parent 5c9dca4 commit a51d878

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/IntegrationTests/ConnectionSpec.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ public async Task when_clients_connect_and_disconnect_then_server_raises_events(
8888
public async Task when_connect_clients_and_one_client_drops_connection_then_other_client_survives()
8989
{
9090
var fooClient = await GetClientAsync();
91+
var fooClientId = GetClientId();
9192
var barClient = await GetClientAsync();
93+
var barClientId = GetClientId();
9294

93-
await fooClient.ConnectAsync(new MqttClientCredentials(GetClientId()));
94-
await barClient.ConnectAsync(new MqttClientCredentials(GetClientId()));
95+
await fooClient.ConnectAsync(new MqttClientCredentials(fooClientId));
96+
await barClient.ConnectAsync(new MqttClientCredentials(barClientId));
9597

96-
var initialConnectedClients = server.ActiveClients.Count();
98+
var clientIds = new List<string> { fooClientId, barClientId };
99+
var initialConnectedClients = server.ActiveClients.Where(c => clientIds.Contains(c)).Count();
97100
var exceptionThrown = false;
98101

99102
try
@@ -110,18 +113,19 @@ public async Task when_connect_clients_and_one_client_drops_connection_then_othe
110113

111114
while (!serverSignal.IsSet)
112115
{
113-
if (server.ActiveConnections == 1 && server.ActiveClients.Count() == 1)
116+
if (!server.ActiveClients.Any(c => c == fooClientId))
114117
{
115118
serverSignal.Set();
116119
}
117120
}
118121

119122
serverSignal.Wait();
120123

124+
var finalConnectedClients = server.ActiveClients.Where(c => clientIds.Contains(c)).Count();
125+
121126
Assert.Equal(2, initialConnectedClients);
122127
Assert.True(exceptionThrown);
123-
Assert.Equal(1, server.ActiveConnections);
124-
Assert.Equal(1, server.ActiveClients.Count());
128+
Assert.Equal(1, finalConnectedClients);
125129

126130
fooClient.Dispose();
127131
barClient.Dispose();
@@ -167,8 +171,7 @@ public async Task when_connecting_twice_with_same_client_with_disconnecting_then
167171
await client.DisconnectAsync();
168172
await client.ConnectAsync(new MqttClientCredentials(clientId));
169173

170-
Assert.Equal(1, server.ActiveConnections);
171-
Assert.Equal(1, server.ActiveClients.Count());
174+
Assert.Equal(1, server.ActiveClients.Count(c => c == clientId));
172175

173176
client.Dispose();
174177
}

0 commit comments

Comments
 (0)