forked from mgravell/Pipelines.Sockets.Unofficial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientBuilderTests.cs
291 lines (239 loc) · 10.1 KB
/
ClientBuilderTests.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using Xunit;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using NippyWard.Networking.Connections;
using NippyWard.Networking.Connections.Middleware;
namespace NippyWard.Networking.Tests
{
public class ClientBuilderTests : BaseTestConnectionContextTests
{
public ClientBuilderTests(ServicesState serviceState)
: base(serviceState)
{ }
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task ClientTaskTest(EndPoint endPoint)
{
TaskCompletionSource serverClientCompleted, clientCompleted;
serverClientCompleted = new TaskCompletionSource();
clientCompleted = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Task serverTask = CreatServerBuilder
(
this.ServiceProvider,
endPoint,
serverClientCompleted,
() => $"ClientTaskTest_server_{serverClientIndex++}_{endPoint}"
)
.BuildSingleClient();
Task clientTask = CreateClientBuilder
(
this.ServiceProvider,
clientCompleted,
() => $"ClientTaskTest_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 ClientTest(EndPoint endPoint)
{
TaskCompletionSource serverClientCompleted, clientCompleted;
serverClientCompleted = new TaskCompletionSource();
clientCompleted = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Task serverTask = CreatServerBuilder
(
this.ServiceProvider,
endPoint,
serverClientCompleted,
() => $"ClientTest_server_{serverClientIndex++}_{endPoint}"
)
.BuildSingleClient();
Client client = await CreateClientBuilder
(
this.ServiceProvider,
clientCompleted,
() => $"ClientTest_client_{clientIndex++}_{endPoint}"
)
.BuildClient(endPoint);
Task clientTask = CreateClientTask(client, clientCompleted);
await Task.WhenAll(serverTask, clientTask);
Assert.True(serverClientCompleted.Task.IsCompletedSuccessfully);
Assert.True(clientCompleted.Task.IsCompletedSuccessfully);
}
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task ClientFactoryTest(EndPoint endPoint)
{
TaskCompletionSource serverClientCompleted, clientCompleted;
serverClientCompleted = new TaskCompletionSource();
clientCompleted = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Task serverTask = CreatServerBuilder
(
this.ServiceProvider,
endPoint,
serverClientCompleted,
() => $"ClientFactoryTest_server_{serverClientIndex++}_{endPoint}"
)
.BuildSingleClient();
ClientFactory clientFactory = CreateClientBuilder
(
this.ServiceProvider,
clientCompleted,
() => $"ClientFactoryTest_client_{clientIndex++}_{endPoint}"
)
.BuildClientFactory();
Client client = await clientFactory.ConnectAsync(endPoint);
Task clientTask = CreateClientTask(client, clientCompleted);
await Task.WhenAll(serverTask, clientTask);
Assert.True(serverClientCompleted.Task.IsCompletedSuccessfully);
Assert.True(clientCompleted.Task.IsCompletedSuccessfully);
}
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task CancellableClientTaskTest(EndPoint endpoint)
{
CancellationTokenSource clientCts = new CancellationTokenSource();
TaskCompletionSource clientConnected = new TaskCompletionSource();
TaskCompletionSource clientTerminal = new TaskCompletionSource();
TaskCompletionSource serverConnected = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Server server = CreatServerBuilder
(
this.ServiceProvider,
endpoint,
serverConnected,
() => $"CancellableClientTaskTest_server_{serverClientIndex++}_{endpoint}"
)
.ConfigureMaxClients(1)
.BuildServer();
await using(server)
{
await server.StartAsync();
Task clientTask = new ClientBuilder(this.ServiceProvider)
.UseTestConnection
(
() => $"CancellableClientTaskTest_client_{clientIndex++}_{endpoint}"
)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
async (ConnectionContext ctx) =>
{
CancellationTokenRegistration reg = ctx.ConnectionClosed.UnsafeRegister((tcs) => ((TaskCompletionSource)tcs!).SetCanceled(), clientTerminal);
try
{
clientConnected.SetResult();
await clientTerminal.Task;
}
finally
{
reg.Dispose();
}
await next(ctx);
}
)
)
.Build(endpoint, clientCts.Token);
//ensure client and server are "connected"
await Task.WhenAll(serverConnected.Task, clientConnected.Task);
//cancel the client
//this initiates ctx.ConnectionClosed
clientCts.Cancel();
//check if the cancellation gets thrown, because it does not get caught
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => clientTask);
//ensure RunAsync ends, before calling shutdown in disposal
await server.RunAsync();
}
}
[Theory]
[MemberData(nameof(GetEndPoint))]
public async Task DisposableClientTest(EndPoint endpoint)
{
CancellationTokenSource clientCts = new CancellationTokenSource();
TaskCompletionSource clientConnected = new TaskCompletionSource();
TaskCompletionSource clientTerminal = new TaskCompletionSource();
TaskCompletionSource serverConnected = new TaskCompletionSource();
int serverClientIndex = 0;
int clientIndex = 0;
Server server = CreatServerBuilder
(
this.ServiceProvider,
endpoint,
serverConnected,
() => $"DisposableClientTest_server_{serverClientIndex++}_{endpoint}"
)
.ConfigureMaxClients(1)
.BuildServer();
TestConnectionLifetime connectionLifetime = new TestConnectionLifetime(clientTerminal);
await using (server)
{
await server.StartAsync();
Client client = await new ClientBuilder(this.ServiceProvider)
.UseTestConnection
(
() => $"DisposableClientTest_client_{clientIndex++}_{endpoint}"
)
.ConfigureConnection
(
(c) =>
c.Use
(
next =>
async (ConnectionContext ctx) =>
{
ctx.Features.Set<IConnectionLifetimeNotificationFeature>(connectionLifetime);
clientConnected.SetResult();
await clientTerminal.Task;
await next(ctx);
}
)
)
.BuildClient(endpoint);
await using (client)
{
await client.StartAsync();
//ensure client is connected
await clientConnected.Task;
}
//ensure RunAsync ends, before calling shutdown in disposal
await server.RunAsync();
}
Assert.True(clientTerminal.Task.IsCompletedSuccessfully);
}
private class TestConnectionLifetime : IConnectionLifetimeNotificationFeature
{
public CancellationToken ConnectionClosedRequested { get; set; }
private readonly TaskCompletionSource _tcs;
public TestConnectionLifetime
(
TaskCompletionSource tcs
)
{
this._tcs = tcs;
this.ConnectionClosedRequested = CancellationToken.None;
}
public void RequestClose()
{
//then set result, so CancellationTokenSource can get disposed
this._tcs.TrySetResult();
}
}
}
}