Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public async Task TestFalseHandshake(Type hostConfiguratorType)

var testConnections = 100;
var websocketStates = new WebSocketState[testConnections];
var websockets = new ClientWebSocket[testConnections];

await Parallel.ForEachAsync(Enumerable.Range(0, 100), async (index, ct) =>
{
Expand All @@ -270,13 +271,51 @@ await Parallel.ForEachAsync(Enumerable.Range(0, 100), async (index, ct) =>
{
}

websockets[index] = websocket;
websocketStates[index] = websocket.State;
});

// Increase delay to allow more time for cleanup, especially on macOS
await Task.Delay(3000, TestContext.Current.CancellationToken);

// Explicit cleanup of any websockets that aren't properly closed
for (int i = 0; i < websockets.Length; i++)
{
var websocket = websockets[i];
if (websocket != null && websocket.State != WebSocketState.Closed)
{
try
{
await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
catch (Exception)
{
// Ignore exceptions during cleanup
}
finally
{
websocket.Dispose();
}
}
else if (websocket != null)
{
websocket.Dispose();
}
}

// Update states after cleanup
for (int i = 0; i < websockets.Length; i++)
{
if (websockets[i] != null)
{
websocketStates[i] = websockets[i].State;
}
}

Assert.All(websocketStates, s => Assert.Equal(WebSocketState.Closed, s));

await Task.Delay(1000, TestContext.Current.CancellationToken);
// Add another delay to ensure all resources are released before assertions
await Task.Delay(2000, TestContext.Current.CancellationToken);

Assert.Equal(0, server.SessionCount);
Assert.Equal(0, sessionContainer.GetSessionCount());
Expand Down
Loading