Skip to content

Commit 746bb6b

Browse files
committed
refactor unit tests
1 parent e1c9ba3 commit 746bb6b

File tree

6 files changed

+609
-512
lines changed

6 files changed

+609
-512
lines changed

src/SocketIOClient/SocketIO.cs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,16 @@ private Uri ServerUri
9292

9393
int _attempts;
9494

95-
[Obsolete]
96-
/// <summary>
97-
/// Whether or not the socket is disconnected from the server.
98-
/// </summary>
99-
public bool Disconnected => !Connected;
100-
10195
public SocketIOOptions Options { get; }
10296

10397
public IJsonSerializer JsonSerializer { get; set; }
104-
98+
public ITransport Transport { get; set; }
10599
public Func<IHttpClient> HttpClientProvider { get; set; }
106100

107101
public Func<IClientWebSocket> ClientWebSocketProvider { get; set; }
108102

109103
List<IDisposable> _resources = new List<IDisposable>();
110104

111-
BaseTransport _transport;
112-
113105
List<Type> _expectedExceptions;
114106

115107
int _packetId;
@@ -177,7 +169,8 @@ private void Initialize()
177169
typeof(WebSocketException),
178170
typeof(HttpRequestException),
179171
typeof(OperationCanceledException),
180-
typeof(TaskCanceledException)
172+
typeof(TaskCanceledException),
173+
typeof(TransportException),
181174
};
182175
}
183176

@@ -205,7 +198,7 @@ private async Task InitTransportAsync()
205198
if (Options.Transport == TransportProtocol.Polling)
206199
{
207200
var handler = HttpPollingHandler.CreateHandler(transportOptions.EIO, GetHttpClient());
208-
_transport = new HttpTransport(transportOptions, handler);
201+
Transport = new HttpTransport(transportOptions, handler);
209202
}
210203
else
211204
{
@@ -215,14 +208,14 @@ private async Task InitTransportAsync()
215208
throw new ArgumentNullException(nameof(ClientWebSocketProvider), $"{ClientWebSocketProvider} returns a null");
216209
}
217210
_resources.Add(ws);
218-
_transport = new WebSocketTransport(transportOptions, ws);
211+
Transport = new WebSocketTransport(transportOptions, ws);
219212
}
220-
_resources.Add(_transport);
221-
_transport.Namespace = _namespace;
213+
_resources.Add(Transport);
214+
Transport.Namespace = _namespace;
222215
SetHeaders();
223-
_transport.SetProxy(Options.Proxy);
224-
_transport.OnReceived = OnMessageReceived;
225-
_transport.OnError = OnErrorReceived;
216+
Transport.SetProxy(Options.Proxy);
217+
Transport.OnReceived = OnMessageReceived;
218+
Transport.OnError = OnErrorReceived;
226219
}
227220

228221
private string GetAuth(object auth)
@@ -241,7 +234,7 @@ private void SetHeaders()
241234
{
242235
try
243236
{
244-
_transport.AddHeader(item.Key, item.Value);
237+
Transport.AddHeader(item.Key, item.Value);
245238
}
246239
catch (Exception e)
247240
{
@@ -277,7 +270,7 @@ private void ConnectInBackground(CancellationToken cancellationToken)
277270
{
278271
using (var cts = new CancellationTokenSource(Options.ConnectionTimeout))
279272
{
280-
await _transport.ConnectAsync(serverUri, cts.Token).ConfigureAwait(false);
273+
await Transport.ConnectAsync(serverUri, cts.Token).ConfigureAwait(false);
281274
break;
282275
}
283276
}
@@ -561,7 +554,7 @@ public async Task DisconnectAsync()
561554
};
562555
try
563556
{
564-
await _transport.SendAsync(msg, CancellationToken.None).ConfigureAwait(false);
557+
await Transport.SendAsync(msg, CancellationToken.None).ConfigureAwait(false);
565558
}
566559
catch (Exception e)
567560
{
@@ -660,7 +653,7 @@ internal async Task ClientAckAsync(int packetId, CancellationToken cancellationT
660653
Id = packetId
661654
};
662655
}
663-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
656+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
664657
}
665658

666659
/// <summary>
@@ -688,7 +681,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
688681
Event = eventName,
689682
Json = result.Json
690683
};
691-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
684+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
692685
}
693686
else
694687
{
@@ -698,7 +691,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
698691
Event = eventName,
699692
Json = result.Json
700693
};
701-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
694+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
702695
}
703696
}
704697
else
@@ -708,7 +701,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
708701
Namespace = _namespace,
709702
Event = eventName
710703
};
711-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
704+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
712705
}
713706
}
714707

@@ -740,7 +733,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
740733
Id = _packetId,
741734
OutgoingBytes = new List<byte[]>(result.Bytes)
742735
};
743-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
736+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
744737
}
745738
else
746739
{
@@ -751,7 +744,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
751744
Id = _packetId,
752745
Json = result.Json
753746
};
754-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
747+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
755748
}
756749
}
757750
else
@@ -762,7 +755,7 @@ public async Task EmitAsync(string eventName, CancellationToken cancellationToke
762755
Namespace = _namespace,
763756
Id = _packetId
764757
};
765-
await _transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
758+
await Transport.SendAsync(msg, cancellationToken).ConfigureAwait(false);
766759
}
767760
}
768761

@@ -775,7 +768,7 @@ private async Task InvokeDisconnect(string reason)
775768
OnDisconnected.TryInvoke(this, reason);
776769
try
777770
{
778-
await _transport.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
771+
await Transport.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
779772
}
780773
catch (Exception e)
781774
{
@@ -804,7 +797,7 @@ public void AddExpectedException(Type type)
804797

805798
public void Dispose()
806799
{
807-
_transport.TryDispose();
800+
Transport.TryDispose();
808801
_ackHandlers.Clear();
809802
_onAnyHandlers.Clear();
810803
_eventHandlers.Clear();

src/SocketIOClient/Transport/BaseTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace SocketIOClient.Transport
1414
{
15-
public abstract class BaseTransport : IDisposable
15+
public abstract class BaseTransport : ITransport
1616
{
1717
protected BaseTransport(TransportOptions options)
1818
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Net;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using SocketIOClient.Messages;
6+
7+
namespace SocketIOClient.Transport
8+
{
9+
public interface ITransport : IDisposable
10+
{
11+
Action<IMessage> OnReceived { get; set; }
12+
Action<Exception> OnError { get; set; }
13+
string Namespace { get; set; }
14+
Task SendAsync(IMessage msg, CancellationToken cancellationToken);
15+
Task ConnectAsync(Uri uri, CancellationToken cancellationToken);
16+
Task DisconnectAsync(CancellationToken cancellationToken);
17+
void AddHeader(string key, string val);
18+
void SetProxy(IWebProxy proxy);
19+
}
20+
}

src/SocketIOClient/Transport/WebSockets/WebSocketTransport.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,16 @@ private void Listen(CancellationToken cancellationToken)
8585
catch (Exception e)
8686
{
8787
OnError.TryInvoke(e);
88-
break;
88+
#if DEBUG
89+
Debug.WriteLine($"[{Protocol}❌] {e}");
90+
#endif
91+
return;
8992
}
9093
}
9194

9295
if (result == null)
9396
{
94-
break;
97+
return;
9598
}
9699

97100
try

0 commit comments

Comments
 (0)