forked from Marfusios/websocket-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocketClientTests.cs
217 lines (175 loc) · 6.73 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
using System;
using System.Net.WebSockets;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Websocket.Client.Tests.Integration
{
public class WebsocketClientTests
{
private static readonly Uri WebsocketUrl = new Uri("wss://www.bitmex.com/realtime");
[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);
await client.Send("ping");
await client.Send("ping");
await client.Send("ping");
await 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();
await client.Send("ping");
await client.Send("ping");
await 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("Unrecognized request"))
{
received = msgText;
receivedEvent.Set();
}
});
await client.Start();
await 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.ReconnectTimeoutMs = 2 * 1000; // 2sec
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.ReconnectTimeoutMs = 5 * 1000; // 5sec
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.ReconnectTimeoutMs = 5 * 1000; // 5sec
string received = null;
var receivedCount = 0;
var receivedEvent = new ManualResetEvent(false);
client.MessageReceived.Subscribe(msg =>
{
receivedCount++;
received = msg.Text;
});
await client.Start();
#pragma warning disable 4014
Task.Run(async () =>
#pragma warning restore 4014
{
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(WebSocketState.Aborted, 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);
}
}
}
}