Skip to content

Commit

Permalink
Fixed the issue in #114 (#116)
Browse files Browse the repository at this point in the history
- Updated a test to catch this issue
- Updated the WebsocketClient.cs accordingly
  • Loading branch information
JacquelineCasey authored Oct 13, 2022
1 parent ab841fd commit 0fbe49e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
9 changes: 7 additions & 2 deletions test/Websocket.Client.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ 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);

string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
var disconnectionCount = 0;
DisconnectionInfo disconnectionInfo = null;

client.MessageReceived
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 0fbe49e

Please sign in to comment.