Skip to content

Commit 6d79a78

Browse files
committed
test: assert the order of events
1 parent 4c1802e commit 6d79a78

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/SocketIOClient.UnitTest/SocketIOTests/ConnectTest.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Moq;
33
using SocketIOClient.WebSocketClient;
44
using System;
5+
using System.Collections.Generic;
56
using System.Net.WebSockets;
67
using System.Threading.Tasks;
78

@@ -65,14 +66,21 @@ public async Task TestException()
6566
[TestMethod]
6667
public async Task TestAttempsEq1()
6768
{
69+
using var io = new SocketIO("http://example.com");
70+
var list = new List<string>();
71+
6872
var mockSocket = new Mock<IWebSocketClient>();
6973
mockSocket.Setup(x => x.ConnectAsync(It.IsAny<Uri>())).Throws(new TimeoutException());
7074

7175
var mockReconnectAttemp = new Mock<EventHandler<int>>();
76+
mockReconnectAttemp.Setup(x => x(io, It.IsAny<int>())).Callback(() => list.Add("OnReconnectAttempt"));
77+
7278
var mockReconnectError = new Mock<EventHandler<Exception>>();
79+
mockReconnectError.Setup(x => x(io, It.IsAny<Exception>())).Callback(() => list.Add("OnReconnectError"));
80+
7381
var mockReconnectFaild = new Mock<EventHandler>();
82+
mockReconnectFaild.Setup(x => x(io, It.IsAny<EventArgs>())).Callback(() => list.Add("OnReconnectFailed"));
7483

75-
using var io = new SocketIO("http://example.com");
7684
io.Options.ReconnectionAttempts = 1;
7785
io.Socket = mockSocket.Object;
7886
io.OnReconnectAttempt += mockReconnectAttemp.Object;
@@ -84,19 +92,32 @@ public async Task TestAttempsEq1()
8492
mockSocket.Verify(x => x.ConnectAsync(It.IsAny<Uri>()), Times.Exactly(2));
8593
mockReconnectAttemp.Verify(x => x(io, 1), Times.Once());
8694
mockReconnectError.Verify(x => x(io, It.IsAny<TimeoutException>()), Times.Once());
95+
mockReconnectFaild.Verify(x => x(io, It.IsAny<EventArgs>()), Times.Once());
96+
97+
Assert.AreEqual(3, list.Count);
98+
Assert.AreEqual("OnReconnectAttempt", list[0]);
99+
Assert.AreEqual("OnReconnectError", list[1]);
100+
Assert.AreEqual("OnReconnectFailed", list[2]);
87101
}
88102

89103
[TestMethod]
90104
public async Task TestAttempsEq2()
91105
{
106+
using var io = new SocketIO("http://example.com");
107+
var list = new List<string>();
108+
92109
var mockSocket = new Mock<IWebSocketClient>();
93110
mockSocket.Setup(x => x.ConnectAsync(It.IsAny<Uri>())).Throws(new WebSocketException());
94111

95112
var mockReconnectAttemp = new Mock<EventHandler<int>>();
113+
mockReconnectAttemp.Setup(x => x(io, It.IsAny<int>())).Callback(() => list.Add("OnReconnectAttempt"));
114+
96115
var mockReconnectError = new Mock<EventHandler<Exception>>();
116+
mockReconnectError.Setup(x => x(io, It.IsAny<Exception>())).Callback(() => list.Add("OnReconnectError"));
117+
97118
var mockReconnectFaild = new Mock<EventHandler>();
119+
mockReconnectFaild.Setup(x => x(io, It.IsAny<EventArgs>())).Callback(() => list.Add("OnReconnectFailed"));
98120

99-
using var io = new SocketIO("http://example.com");
100121
io.Options.ReconnectionAttempts = 2;
101122
io.Socket = mockSocket.Object;
102123
io.OnReconnectAttempt += mockReconnectAttemp.Object;
@@ -109,6 +130,14 @@ public async Task TestAttempsEq2()
109130
mockReconnectAttemp.Verify(x => x(io, 1), Times.Once());
110131
mockReconnectAttemp.Verify(x => x(io, 2), Times.Once());
111132
mockReconnectError.Verify(x => x(io, It.IsAny<WebSocketException>()), Times.Exactly(2));
133+
mockReconnectFaild.Verify(x => x(io, It.IsAny<EventArgs>()), Times.Once());
134+
135+
Assert.AreEqual(5, list.Count);
136+
Assert.AreEqual("OnReconnectAttempt", list[0]);
137+
Assert.AreEqual("OnReconnectError", list[1]);
138+
Assert.AreEqual("OnReconnectAttempt", list[2]);
139+
Assert.AreEqual("OnReconnectError", list[3]);
140+
Assert.AreEqual("OnReconnectFailed", list[4]);
112141
}
113142
}
114143
}

0 commit comments

Comments
 (0)