forked from Marfusios/websocket-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocketClientTests.cs
246 lines (200 loc) · 7.75 KB
/
WebsocketClientTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Net.WebSockets;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Serilog.Events;
using Xunit;
using Xunit.Abstractions;
namespace Websocket.Client.Tests.Integration
{
public class WebsocketClientTests
{
private static readonly Uri WebsocketUrl = new Uri("wss://www.bitmex.com/realtime");
private readonly ITestOutputHelper _output;
public WebsocketClientTests(ITestOutputHelper output)
{
_output = output;
InitLogging(_output);
}
[Fact]
public async Task OnStarting_ShouldGetInfoResponse()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
string received = null;
var receivedEvent = new ManualResetEvent(false);
client.MessageReceived.Subscribe(msg =>
{
received = msg.Text;
receivedEvent.Set();
});
await client.Start();
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
Assert.NotNull(received);
}
}
[Fact]
public async Task SendMessageBeforeStart_ShouldWorkAfterStart()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
client.Send("ping");
client.Send("ping");
client.Send("ping");
client.Send("ping");
client
.MessageReceived
.Where(x => x.Text.ToLower().Contains("pong"))
.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;
if(receivedCount >= 7)
receivedEvent.Set();
});
await client.Start();
client.Send("ping");
client.Send("ping");
client.Send("ping");
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
Assert.NotNull(received);
}
}
[Fact]
public async Task SendBinaryMessage_ShouldWork()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
string received = null;
var receivedEvent = new ManualResetEvent(false);
client.MessageReceived.Subscribe(msg =>
{
var msgText = msg.Text ?? string.Empty;
if (msgText.Contains("400"))
{
received = msgText;
receivedEvent.Set();
}
});
await client.Start();
client.Send(new byte[] {10, 14, 15, 16});
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
Assert.NotNull(received);
}
}
[Fact]
public async Task Starting_MultipleTimes_ShouldWorkWithNoExceptions()
{
for (int i = 0; i < 3; i++)
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
await client.Start();
await Task.Delay(i * 20);
}
}
}
[Fact]
public async Task DisabledReconnecting_ShouldWorkAsExpected()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
client.IsReconnectionEnabled = false;
client.ReconnectTimeout = TimeSpan.FromSeconds(2);
client.MessageReceived.Subscribe(msg =>
{
receivedCount++;
if(receivedCount >= 2)
receivedEvent.Set();
});
await client.Start();
await Task.Delay(5000);
await client.Stop(WebSocketCloseStatus.Empty, string.Empty);
await Task.Delay(5000);
await client.Start();
await Task.Delay(1000);
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
Assert.Equal(2, receivedCount);
}
}
[Fact]
public async Task DisabledReconnecting_ShouldWorkAtRuntime()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
var receivedCount = 0;
client.IsReconnectionEnabled = true;
client.ReconnectTimeout = TimeSpan.FromSeconds(5);
client.MessageReceived.Subscribe(msg =>
{
receivedCount++;
if (receivedCount >= 2)
client.IsReconnectionEnabled = false;
});
await client.Start();
await Task.Delay(17000);
Assert.Equal(2, receivedCount);
}
}
[Fact]
public async Task OnClose_ShouldWorkCorrectly()
{
using (IWebsocketClient client = new WebsocketClient(WebsocketUrl))
{
client.ReconnectTimeout = TimeSpan.FromSeconds(5);
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
var disconnectionCount = 0;
DisconnectionInfo disconnectionInfo = null;
client.MessageReceived.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;
});
client.DisconnectionHappened.Subscribe(x =>
{
disconnectionCount++;
disconnectionInfo = x;
});
await client.Start();
_ = Task.Run(async () =>
{
await Task.Delay(2000);
var success = await client.Stop(WebSocketCloseStatus.InternalServerError, "server error 500");
Assert.True(success);
receivedEvent.Set();
});
receivedEvent.WaitOne(TimeSpan.FromSeconds(30));
Assert.NotNull(received);
Assert.Equal(1, receivedCount);
var nativeClient = client.NativeClient;
Assert.NotNull(nativeClient);
Assert.Equal(1, disconnectionCount);
Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type);
Assert.Equal(WebSocketCloseStatus.InternalServerError, disconnectionInfo.CloseStatus);
Assert.Equal("server error 500", disconnectionInfo.CloseStatusDescription);
Assert.Equal(WebSocketState.Closed, nativeClient.State);
Assert.Equal(WebSocketCloseStatus.InternalServerError, nativeClient.CloseStatus);
Assert.Equal("server error 500", nativeClient.CloseStatusDescription);
// check that reconnection is disabled
await Task.Delay(7000);
Assert.Equal(1, receivedCount);
}
}
private void InitLogging(ITestOutputHelper output)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.TestOutput(output, LogEventLevel.Verbose)
.CreateLogger();
}
}
}