Skip to content

Commit da88059

Browse files
committed
add IDisposable implementation to ISession and ensure session disposal on connection failure
1 parent d664b89 commit da88059

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

src/SocketIOClient/SocketIOClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2;net462;net6.0</TargetFrameworks>
4+
<!--<TargetFrameworks>netstandard2;net462;net6.0</TargetFrameworks>-->
5+
<TargetFramework>netstandard2</TargetFramework>
56
<AssemblyName>SocketIOClient</AssemblyName>
67
<RootNamespace>SocketIOClient</RootNamespace>
78
<Authors>doghappy</Authors>

src/SocketIOClient/V2/Session/HttpSession.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,6 @@ private async Task OnNextTextMessage(string text)
8080
return;
8181
}
8282
await OnNextAsync(message);
83-
// var newMessage= _engineIOAdapter.ProcessMessage(message);
84-
// if (newMessage is null)
85-
// {
86-
// return;
87-
// }
88-
// OnNextAsync(newMessage);
89-
// var messages = _engineIOAdapter.HandleMessage(message);
90-
// if (message.Type is MessageType.Ping)
91-
// {
92-
// var lastPing = _dateTimeProvider.Now;
93-
// // TODO: using var cts = new CancellationTokenSource(Timeout);
94-
// _httpAdapter.SendAsync(new ProtocolMessage
95-
// {
96-
// Text = "3",
97-
// }, CancellationToken.None);
98-
// var pong = new PongMessage
99-
// {
100-
// Duration = _dateTimeProvider.Now - lastPing;
101-
// };
102-
// NotifyObservers(pong);
103-
// }
10483
}
10584

10685
private async Task OnNextBytesMessage(byte[] bytes)
@@ -160,4 +139,12 @@ public void Subscribe(IMyObserver<IMessage> observer)
160139
}
161140
_observers.Add(observer);
162141
}
142+
143+
public void Dispose()
144+
{
145+
if (_engineIOAdapter is IDisposable disposable)
146+
{
147+
disposable.Dispose();
148+
}
149+
}
163150
}

src/SocketIOClient/V2/Session/ISession.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
34
using SocketIOClient.Core;
@@ -6,7 +7,7 @@
67

78
namespace SocketIOClient.V2.Session;
89

9-
public interface ISession : IMyObserver<ProtocolMessage>, IMyObservable<IMessage>, IMyObserver<IMessage>
10+
public interface ISession : IMyObserver<ProtocolMessage>, IMyObservable<IMessage>, IMyObserver<IMessage>, IDisposable
1011
{
1112
int PendingDeliveryCount { get; }
1213
Task SendAsync(object[] data, CancellationToken cancellationToken);

src/SocketIOClient/V2/SocketIO.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
8888
}
8989
catch (Exception e)
9090
{
91+
session.Dispose();
9192
var ex = new ConnectionException($"Cannot connect to server '{ServerUri}'", e);
9293
OnReconnectError?.Invoke(this, ex);
9394
if (i == attempts - 1)

tests/SocketIOClient.UnitTests/V2/SocketIOTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ await _io
165165
.ThrowAsync<OperationCanceledException>();
166166
}
167167

168+
[Fact]
169+
public async Task ConnectAsync_FailedToConnect_SessionIsDisposed()
170+
{
171+
_session.ConnectAsync(Arg.Any<CancellationToken>()).ThrowsAsync(new Exception("Test"));
172+
173+
await _io
174+
.Invoking(async x => await x.ConnectAsync())
175+
.Should()
176+
.ThrowAsync<ConnectionException>();
177+
178+
_session.Received().Dispose();
179+
}
180+
168181
[Fact]
169182
public async Task EmitAsync_AckEventAction_PacketIdIncrementBy1()
170183
{

0 commit comments

Comments
 (0)