Skip to content

Commit 8d5d25c

Browse files
committed
#307 cancel reconnecting when calling Disconnect or Dispose
1 parent 746bb6b commit 8d5d25c

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

src/SocketIOClient/SocketIO.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -546,24 +546,23 @@ private void OnMessageReceived(IMessage msg)
546546

547547
public async Task DisconnectAsync()
548548
{
549-
if (Connected)
549+
_connCts.TryCancel();
550+
_connCts.TryDispose();
551+
var msg = new DisconnectedMessage
552+
{
553+
Namespace = _namespace
554+
};
555+
try
556+
{
557+
await Transport.SendAsync(msg, CancellationToken.None).ConfigureAwait(false);
558+
}
559+
catch (Exception e)
550560
{
551-
var msg = new DisconnectedMessage
552-
{
553-
Namespace = _namespace
554-
};
555-
try
556-
{
557-
await Transport.SendAsync(msg, CancellationToken.None).ConfigureAwait(false);
558-
}
559-
catch (Exception e)
560-
{
561561
#if DEBUG
562-
Debug.WriteLine(e);
562+
Debug.WriteLine(e);
563563
#endif
564-
}
565-
await InvokeDisconnect(DisconnectReason.IOClientDisconnect);
566564
}
565+
await InvokeDisconnect(DisconnectReason.IOClientDisconnect);
567566
}
568567

569568
/// <summary>
@@ -797,6 +796,8 @@ public void AddExpectedException(Type type)
797796

798797
public void Dispose()
799798
{
799+
_connCts.TryCancel();
800+
_connCts.TryDispose();
800801
Transport.TryDispose();
801802
_ackHandlers.Clear();
802803
_onAnyHandlers.Clear();

tests/SocketIOClient.UnitTest/SocketIOTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,5 +575,50 @@ public async Task Should_be_able_to_add_header()
575575

576576
mockWs.Verify(w => w.AddHeader("h1", "v1"), Times.Once());
577577
}
578+
579+
private static async Task Should_be_able_to_cancel_reconnecting(Func<SocketIO,Task> action)
580+
{
581+
using var io = new SocketIO("http://localhost:11003", new SocketIOOptions
582+
{
583+
Transport = TransportProtocol.WebSocket,
584+
ConnectionTimeout = TimeSpan.FromSeconds(1),
585+
ReconnectionAttempts = 4,
586+
});
587+
var mockTransport = new Mock<ITransport>();
588+
mockTransport
589+
.Setup(m=>m.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
590+
.Throws<TimeoutException>();
591+
io.Transport = mockTransport.Object;
592+
593+
int attempts = 0;
594+
io.OnReconnectAttempt += (s, e) => attempts = e;
595+
var task = io.ConnectAsync();
596+
await Task.Delay(2000);
597+
await action(io);
598+
int r1 = attempts;
599+
await Task.Delay(1000);
600+
int r2 = attempts;
601+
602+
attempts.Should().BeGreaterThan(0);
603+
r1.Should().Be(attempts);
604+
r2.Should().Be(attempts);
605+
task.Status.Should().Be(TaskStatus.Faulted);
606+
}
607+
608+
[TestMethod]
609+
public async Task Should_be_able_to_cancel_reconnecting_after_disposed()
610+
{
611+
await Should_be_able_to_cancel_reconnecting(io =>
612+
{
613+
io.Dispose();
614+
return Task.CompletedTask;
615+
});
616+
}
617+
618+
[TestMethod]
619+
public async Task Should_be_able_to_cancel_reconnecting_after_disconnected()
620+
{
621+
await Should_be_able_to_cancel_reconnecting(async io => await io.DisconnectAsync());
622+
}
578623
}
579624
}

0 commit comments

Comments
 (0)