Skip to content

Commit e1c9ba3

Browse files
committed
fix unit tests
1 parent 3182077 commit e1c9ba3

File tree

3 files changed

+88
-53
lines changed

3 files changed

+88
-53
lines changed

src/SocketIOClient/Transport/BaseTransport.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public virtual void Dispose()
136136

137137
protected async Task OnTextReceived(string text)
138138
{
139+
// TODO: refactor
139140
#if DEBUG
140141
Debug.WriteLine($"[{Protocol}⬇] {text}");
141142
#endif
@@ -160,6 +161,18 @@ protected async Task OnTextReceived(string text)
160161
{
161162
if (msg.Type == MessageType.Connected)
162163
{
164+
int ms = 0;
165+
while (OpenedMessage is null)
166+
{
167+
await Task.Delay(10);
168+
ms += 10;
169+
if (ms > Options.ConnectionTimeout.TotalMilliseconds)
170+
{
171+
OnError.TryInvoke(new TimeoutException());
172+
return;
173+
}
174+
}
175+
163176
var connectMsg = msg as ConnectedMessage;
164177
connectMsg.Sid = OpenedMessage.Sid;
165178
if ((string.IsNullOrEmpty(Namespace) && string.IsNullOrEmpty(connectMsg.Namespace)) || connectMsg.Namespace == Namespace)

tests/SocketIOClient.UnitTest/SocketIOTest.cs

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,22 @@ public class SocketIOTest
2222
{
2323
[TestMethod]
2424
[DynamicData(nameof(ConnectCases))]
25-
public async Task Should_be_able_to_connect(SocketIOOptions options, string[] res)
25+
public async Task Should_be_able_to_connect(SocketIOOptions options, string[] messages)
2626
{
2727
int i = -1;
2828
var mockHttp = new Mock<IHttpClient>();
2929
mockHttp
3030
.Setup(m => m.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
31-
.ReturnsAsync(() =>
32-
{
33-
var text = GetResponseText(ref i, res);
34-
return new HttpResponseMessage
35-
{
36-
StatusCode = HttpStatusCode.OK,
37-
Content = new StringContent(text, new MediaTypeHeaderValue("text/pain")),
38-
};
39-
});
31+
.ReturnsAsync(() => GetResponseMessage(ref i, messages));
4032
var mockWs = new Mock<IClientWebSocket>();
4133
mockWs
4234
.Setup(w => w.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
4335
.Callback(() => mockWs.SetupGet(w => w.State).Returns(WebSocketState.Open));
4436
mockWs
4537
.Setup(w => w.ReceiveAsync(It.IsAny<int>(), It.IsNotIn(CancellationToken.None)))
46-
.ReturnsAsync(() =>
47-
{
48-
var text = GetResponseText(ref i, res);
49-
var bytes = Encoding.UTF8.GetBytes(text);
50-
return new WebSocketReceiveResult
51-
{
52-
MessageType = TransportMessageType.Text,
53-
Buffer = bytes,
54-
EndOfMessage = true,
55-
Count = bytes.Length,
56-
};
57-
});
38+
.ReturnsAsync(() => GetWebSocketResult(ref i, messages));
5839
using var io = new SocketIO("http://localhost:11002", options);
59-
io.HttpClientProvider = ()=> mockHttp.Object;
40+
io.HttpClientProvider = () => mockHttp.Object;
6041
io.ClientWebSocketProvider = () => mockWs.Object;
6142

6243
await io.ConnectAsync();
@@ -125,6 +106,29 @@ private static string GetResponseText(ref int i, string[] messages)
125106
return string.Empty;
126107
}
127108

109+
private static HttpResponseMessage GetResponseMessage(ref int i, string[] messages)
110+
{
111+
string text = GetResponseText(ref i, messages);
112+
return new HttpResponseMessage
113+
{
114+
StatusCode = HttpStatusCode.OK,
115+
Content = new StringContent(text, new MediaTypeHeaderValue("text/pain")),
116+
};
117+
}
118+
119+
private static WebSocketReceiveResult GetWebSocketResult(ref int i, string[] messages)
120+
{
121+
var text = GetResponseText(ref i, messages);
122+
var bytes = Encoding.UTF8.GetBytes(text);
123+
return new WebSocketReceiveResult
124+
{
125+
MessageType = TransportMessageType.Text,
126+
Buffer = bytes,
127+
EndOfMessage = true,
128+
Count = bytes.Length,
129+
};
130+
}
131+
128132
[TestMethod]
129133
[DynamicData(nameof(UpgradeToWebSocketCases))]
130134
public async Task Should_upgrade_transport(EngineIO eio, string handshake, string[] messages)
@@ -140,25 +144,14 @@ public async Task Should_upgrade_transport(EngineIO eio, string handshake, strin
140144
.Callback(() => mockWs.SetupGet(w => w.State).Returns(WebSocketState.Open));
141145
mockWs
142146
.Setup(w => w.ReceiveAsync(It.IsAny<int>(), It.IsNotIn(CancellationToken.None)))
143-
.ReturnsAsync(() =>
144-
{
145-
var text = GetResponseText(ref i, messages);
146-
var bytes = Encoding.UTF8.GetBytes(text);
147-
return new WebSocketReceiveResult
148-
{
149-
MessageType = TransportMessageType.Text,
150-
Buffer = bytes,
151-
EndOfMessage = true,
152-
Count = bytes.Length,
153-
};
154-
});
147+
.ReturnsAsync(() => GetWebSocketResult(ref i, messages));
155148
using var io = new SocketIO("http://localhost:11002", new SocketIOOptions
156149
{
157150
AutoUpgrade = true,
158151
Transport = TransportProtocol.Polling,
159152
EIO = eio,
160153
});
161-
io.HttpClientProvider = ()=> mockHttp.Object;
154+
io.HttpClientProvider = () => mockHttp.Object;
162155
io.ClientWebSocketProvider = () => mockWs.Object;
163156

164157
await io.ConnectAsync();
@@ -169,7 +162,7 @@ public async Task Should_upgrade_transport(EngineIO eio, string handshake, strin
169162

170163
private static IEnumerable<object[]> UpgradeToWebSocketCases =>
171164
UpgradeToWebSocketTupleCases.Select(x => new object[] { x.eio, x.handshake, x.messages });
172-
165+
173166
private static IEnumerable<(EngineIO eio, string handshake, string[] messages)> UpgradeToWebSocketTupleCases
174167
{
175168
get
@@ -223,28 +216,21 @@ public async Task Should_not_upgrade_transport(SocketIOOptions options, string h
223216
.ReturnsAsync(handshake);
224217
mockHttp
225218
.Setup(m => m.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
226-
.ReturnsAsync(() =>
227-
{
228-
var text = GetResponseText(ref i, messages);
229-
return new HttpResponseMessage
230-
{
231-
StatusCode = HttpStatusCode.OK,
232-
Content = new StringContent(text, new MediaTypeHeaderValue("text/pain")),
233-
};
234-
});
219+
.ReturnsAsync(() => GetResponseMessage(ref i, messages));
235220
using var io = new SocketIO("http://localhost:11002", options);
236-
io.HttpClientProvider = ()=> mockHttp.Object;
221+
io.HttpClientProvider = () => mockHttp.Object;
237222

238223
await io.ConnectAsync();
239224

240225
io.Connected.Should().BeTrue();
241226
io.Options.Transport.Should().Be(TransportProtocol.Polling);
242227
}
243-
244-
private static IEnumerable<object[]> NotUpgradeToWebSocketCases =>
228+
229+
private static IEnumerable<object[]> NotUpgradeToWebSocketCases =>
245230
NotUpgradeToWebSocketTupleCases.Select(x => new object[] { x.options, x.handshake, x.messages });
246-
247-
private static IEnumerable<(SocketIOOptions options, string handshake, string[] messages)> NotUpgradeToWebSocketTupleCases
231+
232+
private static IEnumerable<(SocketIOOptions options, string handshake, string[] messages)>
233+
NotUpgradeToWebSocketTupleCases
248234
{
249235
get
250236
{
@@ -323,11 +309,11 @@ public async Task Should_throw_an_exception_when_server_is_unavailable_and_recon
323309
Transport = TransportProtocol.Polling,
324310
Reconnection = false
325311
});
326-
io.HttpClientProvider = ()=> mockHttp.Object;
312+
io.HttpClientProvider = () => mockHttp.Object;
327313
io.ClientWebSocketProvider = () => mockWs.Object;
328314

329315
await io
330-
.Invoking(async x=>await x.ConnectAsync())
316+
.Invoking(async x => await x.ConnectAsync())
331317
.Should()
332318
.ThrowAsync<ConnectionException>()
333319
.WithMessage("Cannot connect to server '*'");

tests/SocketIOClient.UnitTest/Transport/Http/HttpTransportTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,5 +524,41 @@ public async Task ConcurrentlySend(EngineIO eio, int times)
524524
h => h.PostAsync(null, It.IsAny<IEnumerable<byte[]>>(), CancellationToken.None),
525525
Times.Exactly(times));
526526
}
527+
528+
[TestMethod]
529+
public async Task OpenedMessage_should_be_in_front_of_ConnectedMessage()
530+
{
531+
var mockHttpPollingHandler = new Mock<IHttpPollingHandler>();
532+
mockHttpPollingHandler.SetupProperty(h => h.OnTextReceived);
533+
var transport = new HttpTransport(new TransportOptions
534+
{
535+
EIO = EngineIO.V3,
536+
ConnectionTimeout = TimeSpan.FromSeconds(1),
537+
}, mockHttpPollingHandler.Object);
538+
var msgs = new List<IMessage>();
539+
transport.OnReceived = m => msgs.Add(m);
540+
541+
var connectedTask = mockHttpPollingHandler.Object.OnTextReceived("40");
542+
await Task.Delay(100);
543+
_ = mockHttpPollingHandler.Object.OnTextReceived(
544+
"0{\"sid\":\"test\",\"upgrades\":[\"websocket\"],\"pingInterval\":10000,\"pingTimeout\":5000}");
545+
await connectedTask;
546+
547+
msgs.Should().BeEquivalentTo(new object[]
548+
{
549+
new
550+
{
551+
Type = MessageType.Opened,
552+
Sid = "test",
553+
Upgrades = new[] { "websocket" },
554+
PingInterval = 10000,
555+
PingTimeout = 5000,
556+
},
557+
new
558+
{
559+
Type = MessageType.Connected,
560+
},
561+
}, options => options.WithStrictOrdering());
562+
}
527563
}
528564
}

0 commit comments

Comments
 (0)