From e306d57a64de1ceea05355848e6a445c26622900 Mon Sep 17 00:00:00 2001 From: jackcasey067 Date: Mon, 4 Jul 2022 14:23:31 -0500 Subject: [PATCH] Fixed the issue in https://github.com/Marfusios/websocket-client/issues/114 - Updated a test to catch this issue - Updated the WebsocketClient.cs accordingly --- src/Websocket.Client/WebsocketClient.cs | 6 +++++- test/Websocket.Client.Tests/ConnectionTests.cs | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Websocket.Client/WebsocketClient.cs b/src/Websocket.Client/WebsocketClient.cs index f186d5c..0885239 100644 --- a/src/Websocket.Client/WebsocketClient.cs +++ b/src/Websocket.Client/WebsocketClient.cs @@ -191,9 +191,13 @@ public void Dispose() Logger.Error(e, L($"Failed to dispose client, error: {e.Message}")); } + if (IsRunning) + { + _disconnectedSubject.OnNext(DisconnectionInfo.Create(DisconnectionType.Exit, _client, null)); + } + IsRunning = false; IsStarted = false; - _disconnectedSubject.OnNext(DisconnectionInfo.Create(DisconnectionType.Exit, _client, null)); _disconnectedSubject.OnCompleted(); } diff --git a/test/Websocket.Client.Tests/ConnectionTests.cs b/test/Websocket.Client.Tests/ConnectionTests.cs index c6f38f1..d09bf8e 100644 --- a/test/Websocket.Client.Tests/ConnectionTests.cs +++ b/test/Websocket.Client.Tests/ConnectionTests.cs @@ -146,6 +146,8 @@ public async Task Starting_MultipleTimes_ShouldWorkWithNoExceptions() [Fact] public async Task Stopping_ShouldWorkCorrectly() { + var disconnectionCount = 0; + using (var client = _context.CreateClient()) { client.ReconnectTimeout = TimeSpan.FromSeconds(7); @@ -153,7 +155,6 @@ public async Task Stopping_ShouldWorkCorrectly() string received = null; var receivedCount = 0; var receivedEvent = new ManualResetEvent(false); - var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.MessageReceived @@ -186,13 +187,17 @@ public async Task Stopping_ShouldWorkCorrectly() // check that reconnection is disabled await Task.Delay(8000); Assert.Equal(1, receivedCount); - Assert.InRange(disconnectionCount, 1, 2); + Assert.Equal(1, disconnectionCount); Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type); Assert.Equal(WebSocketCloseStatus.InternalServerError, disconnectionInfo.CloseStatus); Assert.Equal("server error 500", disconnectionInfo.CloseStatusDescription); Assert.False(client.IsRunning); Assert.False(client.IsStarted); } + + // Disposing a disconnected socket should not cause DisconnectionHappened to trigger. + await Task.Delay(200); + Assert.Equal(1, disconnectionCount); } [Fact]