forked from mgravell/Pipelines.Sockets.Unofficial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerBuilderTests.cs
267 lines (225 loc) · 9.11 KB
/
ServerBuilderTests.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using Xunit;
using Xunit.Sdk;
using Xunit.Abstractions;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using NippyWard.Networking.Connections;
namespace NippyWard.Networking.Tests
{
public class ServerBuilderTests : BaseTestConnectionContextTests
{
public ServerBuilderTests(ServicesState serviceState)
: base(serviceState)
{ }
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task ServerTaskSingleClientTest(EndPoint endPoint)
{
TaskCompletionSource serverClientCompleted, clientCompleted;
serverClientCompleted = new TaskCompletionSource();
clientCompleted = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Task serverTask = CreatServerBuilder
(
this.ServiceProvider,
endPoint,
serverClientCompleted,
() => $"ServerTaskSingleClientTest_server_{serverClientIndex++}_{endPoint}"
)
.BuildSingleClient();
Task clientTask = CreateClientBuilder
(
this.ServiceProvider,
clientCompleted,
() => $"ServerTaskSingleClientTest_client_{clientIndex++}_{endPoint}"
)
.Build(endPoint);
await Task.WhenAll(serverTask, clientTask);
Assert.True(serverClientCompleted.Task.IsCompletedSuccessfully);
Assert.True(clientCompleted.Task.IsCompletedSuccessfully);
}
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task CancellableServerTaskMultiClientTest(EndPoint endpoint)
{
int maxClient = 10;
int serverCount = 0;
int clientCount = 0;
CancellationTokenSource cts = new CancellationTokenSource();
TaskCompletionSource[] tcs = new TaskCompletionSource[maxClient];
Task[] clients = new Task[maxClient];
int serverClientIndex = 0;
int clientIndex = 0;
for (int i = 0; i < maxClient; i++)
{
tcs[i] = new TaskCompletionSource();
}
Task serverTask = new ServerBuilder(this.ServiceProvider)
.UseTestConnection
(
endpoint,
() => $"CancellableServerTaskMultiClientTest_server_{serverClientIndex++}_{endpoint}"
)
.ConfigureMaxClients(10)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
async (ConnectionContext ctx) =>
{
int index = Interlocked.Increment(ref serverCount);
TaskCompletionSource t = tcs[--index];
CancellationTokenRegistration reg = ctx.ConnectionClosed.UnsafeRegister((tcs) => ((TaskCompletionSource?)tcs!).SetCanceled(), t);
try
{
await t.Task;
}
finally
{
reg.Dispose();
}
await next(ctx);
}
)
)
.BuildMultiClient(cts.Token);
ClientFactory clientFactory = new ClientBuilder(this.ServiceProvider)
.UseTestConnection
(
() => $"CancellableServerTaskMultiClientTest_client_{clientIndex++}_{endpoint}"
)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
(ConnectionContext ctx) =>
{
int index = Interlocked.Increment(ref clientCount);
tcs[--index].SetResult();
return next(ctx);
}
)
)
.BuildClientFactory();
for (int i = 0; i < maxClient; i++)
{
clients[i] = clientFactory.RunClientAsync(endpoint);
}
//await all clients (doing nothing)
await Task.WhenAll(clients);
//check if all clients connected
Assert.Equal(maxClient, clientCount);
//shutdown the server with a timeout to allow connections to gracefully close
//as there is no way to await the clients when using a server as Task only
cts.CancelAfter(100);
//await the server
await serverTask;
//check if all clients have connected to server
Assert.Equal(maxClient, serverCount);
//ceck if the server shut down cleanly
Assert.True(serverTask.IsCompletedSuccessfully);
}
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task DisposableServerMultiClientTest(EndPoint endpoint)
{
int maxClient = 10;
int serverCount = 0;
int clientCount = 0;
CancellationTokenSource cts = new CancellationTokenSource();
TaskCompletionSource[] tcs = new TaskCompletionSource[maxClient];
Task[] clients = new Task[maxClient];
int serverClientIndex = 0;
int clientIndex = 0;
for (int i = 0; i < maxClient; i++)
{
tcs[i] = new TaskCompletionSource();
}
Server server = new ServerBuilder(this.ServiceProvider)
.UseTestConnection
(
endpoint,
() => $"DisposableServerMultiClientTest_server_{serverClientIndex++}_{endpoint}"
)
.ConfigureMaxClients(10)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
async (ConnectionContext ctx) =>
{
int index = Interlocked.Increment(ref serverCount);
TaskCompletionSource t = tcs[--index];
CancellationTokenRegistration reg = ctx.ConnectionClosed.UnsafeRegister((tcs) => ((TaskCompletionSource)tcs!).SetCanceled(), t);
try
{
await t.Task;
}
finally
{
reg.Dispose();
}
await next(ctx);
}
)
)
.BuildServer();
ClientFactory clientFactory = new ClientBuilder(this.ServiceProvider)
.UseTestConnection
(
() => $"DisposableServerMultiClientTest_client_{clientIndex++}_{endpoint}"
)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
(ConnectionContext ctx) =>
{
int index = Interlocked.Increment(ref clientCount);
tcs[--index].SetResult();
return next(ctx);
}
)
)
.BuildClientFactory();
//use IAsyncDisposable
await using (server)
{
await server.StartAsync();
for (int i = 0; i < maxClient; i++)
{
clients[i] = clientFactory.RunClientAsync(endpoint);
}
//await all clients (doing nothing)
await Task.WhenAll(clients);
//check if all clients have connected
Assert.Equal(maxClient, clientCount);
//await all server connections
await server.Connections;
//check if all clients have connected to server
Assert.Equal(maxClient, serverCount);
//ensure RunAsync ends, before calling shutdown in disposal
await server.RunAsync();
}
}
}
}