Skip to content

Commit 996daab

Browse files
committed
add cancellation support to ConnectAsync and corresponding tests
1 parent 8dfd8f6 commit 996daab

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/SocketIOClient/V2/SocketIO.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ private Uri ServerUri
6565
public event EventHandler<Exception> OnReconnectError;
6666

6767
public async Task ConnectAsync()
68+
{
69+
await ConnectAsync(CancellationToken.None);
70+
}
71+
72+
public async Task ConnectAsync(CancellationToken cancellationToken)
6873
{
6974
var attempts = Options.Reconnection ? Options.ReconnectionAttempts : 1;
7075
for (int i = 0; i < attempts; i++)
7176
{
77+
cancellationToken.ThrowIfCancellationRequested();
7278
// TODO: IDisposable
7379
var session = SessionFactory.New(Options.EIO);
7480
using var cts = new CancellationTokenSource(Options.ConnectionTimeout);

tests/SocketIOClient.UnitTests/V2/SocketIOTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ public async Task ConnectAsync_ConnectedMessageReceived_ConnectedIsTrueIdHasValu
127127
_io.Id.Should().Be("123");
128128
}
129129

130+
[Fact]
131+
public async Task ConnectAsyncCancellationToken_GivenACanceledToken_ThrowConnectionException()
132+
{
133+
_session.ConnectAsync(Arg.Any<CancellationToken>()).ThrowsAsync(new Exception("Test"));
134+
135+
await _io
136+
.Invoking(async x =>
137+
{
138+
using var cts = new CancellationTokenSource();
139+
await cts.CancelAsync();
140+
await x.ConnectAsync(cts.Token);
141+
})
142+
.Should()
143+
.ThrowAsync<OperationCanceledException>();
144+
}
145+
130146
[Fact]
131147
public async Task EmitAsync_AckEventAction_PacketIdIncrementBy1()
132148
{

0 commit comments

Comments
 (0)