Skip to content

Commit

Permalink
Fix edge case - changing target url to failing server should be killable
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Feb 23, 2023
1 parent 4003a53 commit 638cf7f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- Project defaults -->

<PropertyGroup>
<Version>4.6.0</Version>
<Version>4.6.1</Version>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ private async Task<bool> StopInternal(WebSocket client, WebSocketCloseStatus sta
if (!IsRunning)
{
Logger.Info(L("Client is already stopped"));
IsStarted = false;
return false;
}

Expand Down
54 changes: 53 additions & 1 deletion test/Websocket.Client.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public async Task Dispose_ShouldWorkCorrectly()
public async Task Stopping_InvalidServer_ShouldStopReconnection()
{
using var client = _context.CreateInvalidClient(new Uri("wss://google.com"));
client.ErrorReconnectTimeout = TimeSpan.FromSeconds(5);
client.ErrorReconnectTimeout = TimeSpan.FromSeconds(4);

string received = null;
var receivedCount = 0;
Expand Down Expand Up @@ -549,5 +549,57 @@ public async Task Stopping_InvalidServer_ShouldStopReconnection()
Assert.Equal(0, receivedCount);
Assert.Null(received);
}

[Fact]
public async Task Stopping_AfterChangingToInvalidServer_ShouldStopReconnection()
{
using var client = _context.CreateClient();
client.ErrorReconnectTimeout = TimeSpan.FromSeconds(4);

string received = null;
var receivedCount = 0;
var disconnectionCount = 0;
DisconnectionInfo disconnectionInfo = null;

client
.MessageReceived
.Subscribe(msg =>
{
_output.WriteLine($"Received: '{msg}'");
receivedCount++;
received = msg.Text;
});

client.DisconnectionHappened.Subscribe(x =>
{
disconnectionCount++;
disconnectionInfo = x;
});

_ = client.Start();
await Task.Delay(TimeSpan.FromSeconds(1));

Assert.True(client.IsStarted, "IsStarted should be true");
await client.Stop(WebSocketCloseStatus.NormalClosure, string.Empty);
await Task.Delay(TimeSpan.FromSeconds(6));
Assert.False(client.IsRunning, "IsRunning should be false");

client.Url = _context.InvalidUri;
_ = client.Start();
await Task.Delay(TimeSpan.FromSeconds(1));

Assert.True(client.IsStarted, "IsStarted should be true");
await client.Stop(WebSocketCloseStatus.NormalClosure, string.Empty);
await Task.Delay(TimeSpan.FromSeconds(6));

Assert.False(client.IsRunning, "IsRunning is true and shouldn't");
Assert.False(client.IsStarted, "IsStarted is true and shouldn't");

Assert.Equal(3, disconnectionCount);
Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type);

Assert.Equal(1, receivedCount);
Assert.NotNull(received);
}
}
}
7 changes: 7 additions & 0 deletions test/Websocket.Client.Tests/Testserver/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public TestContext(ITestOutputHelper output)

public WebSocketClient NativeTestClient { get; set; }

public Uri InvalidUri { get; } = new("wss://invalid-url.local");

public IWebsocketClient CreateClient()
{
var httpClient = _factory.CreateClient(); // This is needed since _factory.Server would otherwise be null
Expand All @@ -39,6 +41,11 @@ public IWebsocketClient CreateClient(Uri serverUrl)
throw new InvalidOperationException("Connection to websocket server failed, check url");
}
if (uri == InvalidUri)
{
throw new InvalidOperationException("Connection to websocket server failed, check url");
}
NativeTestClient = _factory.Server.CreateWebSocketClient();
var ws = await NativeTestClient.ConnectAsync(uri, token).ConfigureAwait(false);
//await Task.Delay(1000, token);
Expand Down

0 comments on commit 638cf7f

Please sign in to comment.