@@ -27,43 +27,45 @@ public MultiChannelTcpServerTests(ITestOutputHelper testOutputHelper)
2727 {
2828 _testOutputHelper = testOutputHelper ;
2929 _localAddress = IPAddress . Loopback . ToString ( ) ;
30- // OperatingSystem.IsWindows() ? "127.0.0.1" : "0.0.0.0";
3130 _testOutputHelper . WriteLine ( $ "Local address is: { _localAddress } ") ;
31+ string isDocker = Environment . GetEnvironmentVariable ( IsContainerizedEnvVar ) ;
32+ if ( ! string . IsNullOrEmpty ( isDocker ) && isDocker . ToLower ( ) == "true" )
33+ {
34+ _testOutputHelper . WriteLine ( "Test are running inside a Docker container." ) ;
35+ _isRunningInContainer = true ;
36+ }
37+
38+ _connErrorsRatio = 0.05 ;
3239 }
3340
3441 public void Dispose ( )
3542 {
36- if ( _server != null )
37- {
38- _server . Dispose ( ) ;
39- }
4043 }
4144
4245 [ Theory ]
43- [ InlineData ( 100 ) ]
44- [ InlineData ( 500 ) ]
45- [ InlineData ( 1000 ) ]
46- [ InlineData ( 5000 ) ]
47- [ InlineData ( 10000 ) ]
48- [ InlineData ( 20000 ) ]
49- [ InlineData ( 50000 ) ]
50- public async Task TestCollectMultipleClientsToOneChannelServer ( int clientsNumber )
46+ [ InlineData ( 10000 , 100 ) ]
47+ [ InlineData ( 10001 , 500 ) ]
48+ [ InlineData ( 10002 , 1000 ) ]
49+ [ InlineData ( 10003 , 5000 ) ]
50+ [ InlineData ( 10004 , 10000 ) ]
51+ [ InlineData ( 10005 , 20000 ) ]
52+ [ InlineData ( 10006 , 50000 ) ]
53+ public async Task TestCollectMultipleClientsToOneChannelServer ( int serverPort , int clientsNumber )
5154 {
52- Random rand = new Random ( DateTimeOffset . Now . Millisecond ) ;
53- int serverPort = rand . Next ( 17000 , 25000 ) ;
5455 ServerChannelConfiguration mainInsecureChannel = new ServerChannelConfiguration ( )
5556 {
5657 IpAddress = _localAddress ,
5758 Port = serverPort ,
58- IsSecure = false
59+ IsSecure = false ,
60+ // Prefix = _isRunningInContainer ?
5961 } ;
6062 _testOutputHelper . WriteLine ( $ "Server port is: { serverPort } ") ;
6163
6264 int clientsCounter = 0 ;
6365 int clientsConnectError = 0 ;
6466 int serverErrorsCounter = 0 ;
6567
66- _server = new MultiChannelTcpServer ( new [ ] { mainInsecureChannel } , new LoggerFactory ( ) ,
68+ ITcpServer server = new MultiChannelTcpServer ( new [ ] { mainInsecureChannel } , new LoggerFactory ( ) ,
6769 c =>
6870 {
6971 Interlocked . Increment ( ref clientsCounter ) ;
@@ -73,15 +75,16 @@ public async Task TestCollectMultipleClientsToOneChannelServer(int clientsNumber
7375 Interlocked . Increment ( ref serverErrorsCounter ) ;
7476 } ) ;
7577
76- Task < OperationResult > startTask = _server . StartAsync ( ) ;
78+ Task < OperationResult > startTask = server . StartAsync ( ) ;
7779 startTask . Wait ( ) ;
7880 OperationResult startResult = startTask . Result ;
7981 Assert . True ( startResult . Success ) ;
8082 // 2. Create N TcpClients
8183 IList < TestTcpClient > clients = new List < TestTcpClient > ( ) ;
8284 for ( int c = 0 ; c < clientsNumber ; c ++ )
8385 {
84- clients . Add ( new TestTcpClient ( true , _localAddress , ( UInt16 ) serverPort ) ) ;
86+ clients . Add ( new TestTcpClient ( true , _localAddress , ( UInt16 ) serverPort ,
87+ 50 , 50 , 16 , false ) ) ;
8588 }
8689
8790 // 3. Open N connections
@@ -109,14 +112,15 @@ public async Task TestCollectMultipleClientsToOneChannelServer(int clientsNumber
109112
110113 await Task . WhenAll ( clientsConnTask ) ;
111114 Assert . Equal ( 0 , serverErrorsCounter ) ;
112- Assert . Equal ( clientsNumber , clientsCounter ) ;
115+ int successfullyConnectedClients = ( int ) ( ( 1.0 - _connErrorsRatio ) * clientsNumber ) ;
116+ Assert . InRange ( clientsCounter , successfullyConnectedClients , clientsNumber ) ;
113117
114118 for ( int c = 0 ; c < clientsNumber ; c ++ )
115119 {
116120 clients [ c ] . Close ( ) ;
117121 }
118122
119- Task < OperationResult > stopTask = _server . StopAsync ( ) ;
123+ Task < OperationResult > stopTask = server . StopAsync ( ) ;
120124 stopTask . Wait ( ) ;
121125 OperationResult stopResult = stopTask . Result ;
122126 Assert . True ( stopResult . Success ) ;
@@ -128,15 +132,13 @@ public async Task TestCollectMultipleClientsToOneChannelServer(int clientsNumber
128132 }
129133
130134 [ Theory ]
131- [ InlineData ( 50 ) ]
132- [ InlineData ( 100 ) ]
133- [ InlineData ( 500 ) ]
134- [ InlineData ( 5000 ) ]
135- [ InlineData ( 10000 ) ]
136- public async Task TestInteractWithClients ( int clientsNumber )
135+ [ InlineData ( 11000 , 50 ) ]
136+ [ InlineData ( 11001 , 100 ) ]
137+ [ InlineData ( 11002 , 500 ) ]
138+ [ InlineData ( 11003 , 5000 ) ]
139+ [ InlineData ( 11004 , 10000 ) ]
140+ public async Task TestInteractWithClients ( int serverPort , int clientsNumber )
137141 {
138- Random rand = new Random ( ( int ) DateTimeOffset . Now . Ticks ) ;
139- int serverPort = rand . Next ( 17000 , 25000 ) ;
140142 ServerChannelConfiguration mainInsecureChannel = new ServerChannelConfiguration ( )
141143 {
142144 IpAddress = _localAddress ,
@@ -145,22 +147,22 @@ public async Task TestInteractWithClients(int clientsNumber)
145147 } ;
146148 _testOutputHelper . WriteLine ( $ "Server port is: { serverPort } ") ;
147149
148- _server = new MultiChannelTcpServer ( new [ ] { mainInsecureChannel } , new LoggerFactory ( ) ,
150+ ITcpServer server = new MultiChannelTcpServer ( new [ ] { mainInsecureChannel } , new LoggerFactory ( ) ,
149151 null , null ) ;
150152 int clientsCounter = 0 ;
151- _server . AssignConnectionHandler ( c =>
153+ server . AssignConnectionHandler ( c =>
152154 {
153155 Interlocked . Increment ( ref clientsCounter ) ;
154156 } ) ;
155- _server . AssignHandler ( async ( d , c ) =>
157+ server . AssignHandler ( async ( d , c ) =>
156158 {
157- //_testOutputHelper.WriteLine("Client connected!");
158- string msg = System . Text . Encoding . Default . GetString ( d ) ;
159- //_testOutputHelper.WriteLine($"Client {c.Id} send data: {msg}");
159+ // _testOutputHelper.WriteLine("Client connected!");
160+ // string msg = System.Text.Encoding.Default.GetString(d);
161+ // _testOutputHelper.WriteLine($"Client {c.Id} send data: {msg}");
160162 return d . Reverse ( ) . ToArray ( ) ;
161163
162164 } ) ;
163- OperationResult startResult = await _server . StartAsync ( ) ;
165+ OperationResult startResult = await server . StartAsync ( ) ;
164166 if ( ! startResult . Success )
165167 {
166168 _testOutputHelper . WriteLine ( startResult . Message ) ;
@@ -216,23 +218,23 @@ public async Task TestInteractWithClients(int clientsNumber)
216218 }
217219
218220
219- OperationResult stopResult = await _server . StopAsync ( ) ;
221+ OperationResult stopResult = await server . StopAsync ( ) ;
220222 Assert . True ( stopResult . Success ) ;
221223
222224 for ( int c = 0 ; c < clientsNumber ; c ++ )
223225 {
224226 clients [ c ] . Dispose ( ) ;
225227 }
228+
229+ server . Dispose ( ) ;
226230 }
227231
228232 [ Theory ]
229- [ InlineData ( 50 ) ]
230- [ InlineData ( 100 ) ]
231- [ InlineData ( 500 ) ]
232- public async Task TestInteractWithClientsWithTlsProtectedChannel ( int clientsNumber )
233+ [ InlineData ( 12000 , 50 ) ]
234+ [ InlineData ( 12001 , 100 ) ]
235+ [ InlineData ( 12002 , 500 ) ]
236+ public async Task TestInteractWithClientsWithTlsProtectedChannel ( int serverPort , int clientsNumber )
233237 {
234- Random rand = new Random ( ( int ) DateTimeOffset . Now . Ticks ) ;
235- int serverPort = rand . Next ( 17000 , 25000 ) ;
236238 ServerChannelConfiguration mainSecureChannel = new ServerChannelConfiguration ( )
237239 {
238240 IpAddress = _localAddress ,
@@ -242,21 +244,21 @@ public async Task TestInteractWithClientsWithTlsProtectedChannel(int clientsNumb
242244 } ;
243245 _testOutputHelper . WriteLine ( $ "Server port is: { serverPort } ") ;
244246
245- _server = new MultiChannelTcpServer ( new [ ] { mainSecureChannel } , new LoggerFactory ( ) ,
247+ ITcpServer server = new MultiChannelTcpServer ( new [ ] { mainSecureChannel } , new LoggerFactory ( ) ,
246248 null , null ) ;
247249 int clientsCounter = 0 ;
248- _server . AssignConnectionHandler ( c =>
250+ server . AssignConnectionHandler ( c =>
249251 {
250252 Interlocked . Increment ( ref clientsCounter ) ;
251253 } ) ;
252- _server . AssignHandler ( async ( d , c ) =>
254+ server . AssignHandler ( async ( d , c ) =>
253255 {
254256 // _testOutputHelper.WriteLine("Client connected!");
255257 // string msg = System.Text.Encoding.Default.GetString(d);
256258 // _testOutputHelper.WriteLine($"Client {c.Id} send data: {msg}");
257259 return d . Reverse ( ) . ToArray ( ) ;
258260 } ) ;
259- OperationResult startResult = await _server . StartAsync ( ) ;
261+ OperationResult startResult = await server . StartAsync ( ) ;
260262 if ( ! startResult . Success )
261263 {
262264 _testOutputHelper . WriteLine ( startResult . Message ) ;
@@ -306,29 +308,30 @@ public async Task TestInteractWithClientsWithTlsProtectedChannel(int clientsNumb
306308 }
307309
308310
309- OperationResult stopResult = await _server . StopAsync ( ) ;
311+ OperationResult stopResult = await server . StopAsync ( ) ;
310312 Assert . True ( stopResult . Success ) ;
311313
312314 for ( int c = 0 ; c < clientsNumber ; c ++ )
313315 {
314316 clients [ c ] . Dispose ( ) ;
315317 }
318+
319+ server . Dispose ( ) ;
316320 }
317321
318322 [ Theory ]
319- [ InlineData ( 50 , 100 ) ]
320- public async Task TestMultiChannelTcpServer ( int mainChannelClientsNumber , int additionalChannelClientsNumber )
323+ [ InlineData ( 13000 , 50 , 13001 , 100 ) ]
324+ public async Task TestMultiChannelTcpServer ( int mainServerPort , int mainChannelClientsNumber ,
325+ int additionalServerPort , int additionalChannelClientsNumber )
321326 {
322327 Random rand = new Random ( ( int ) DateTimeOffset . Now . Ticks ) ;
323- int mainServerPort = rand . Next ( 17000 , 25000 ) ;
324328 ServerChannelConfiguration mainSecureChannel = new ServerChannelConfiguration ( )
325329 {
326330 IpAddress = _localAddress ,
327331 Port = mainServerPort ,
328332 IsSecure = true ,
329333 CertificatePath = Path . GetFullPath ( TestCertificatePath )
330334 } ;
331- int additionalServerPort = rand . Next ( 17000 , 25000 ) ;
332335 ServerChannelConfiguration additionalNonSecureChannel = new ServerChannelConfiguration ( )
333336 {
334337 IpAddress = _localAddress ,
@@ -338,22 +341,22 @@ public async Task TestMultiChannelTcpServer(int mainChannelClientsNumber, int ad
338341
339342 _testOutputHelper . WriteLine ( $ "Server port are: ({ mainServerPort } , { additionalServerPort } )") ;
340343
341- _server = new MultiChannelTcpServer ( new [ ] { mainSecureChannel , additionalNonSecureChannel } ,
344+ ITcpServer server = new MultiChannelTcpServer ( new [ ] { mainSecureChannel , additionalNonSecureChannel } ,
342345 new NullLoggerFactory ( ) ,
343346 null , null ) ;
344347 int clientsCounter = 0 ;
345- _server . AssignConnectionHandler ( c =>
348+ server . AssignConnectionHandler ( c =>
346349 {
347350 Interlocked . Increment ( ref clientsCounter ) ;
348351 } ) ;
349- _server . AssignHandler ( async ( d , c ) =>
352+ server . AssignHandler ( async ( d , c ) =>
350353 {
351354 // _testOutputHelper.WriteLine("Client connected!");
352355 // string msg = System.Text.Encoding.Default.GetString(d);
353356 // _testOutputHelper.WriteLine($"Client {c.Id} send data: {msg}");
354357 return d . Reverse ( ) . ToArray ( ) ;
355358 } ) ;
356- OperationResult startResult = await _server . StartAsync ( ) ;
359+ OperationResult startResult = await server . StartAsync ( ) ;
357360 if ( ! startResult . Success )
358361 {
359362 _testOutputHelper . WriteLine ( startResult . Message ) ;
@@ -406,13 +409,19 @@ public async Task TestMultiChannelTcpServer(int mainChannelClientsNumber, int ad
406409 clients [ c ] . Close ( ) ;
407410 }
408411
409- OperationResult stopResult = await _server . StopAsync ( ) ;
412+ OperationResult stopResult = await server . StopAsync ( ) ;
410413 Assert . True ( stopResult . Success ) ;
414+
415+ server . Dispose ( ) ;
411416 }
412-
417+
418+ private const string IsContainerizedEnvVar = "IS_CONTAINERIZED" ;
413419 private const string TestCertificatePath = "../../../testCerts/certificate.pfx" ;
420+
414421 private readonly string _localAddress ;
415422 private readonly ITestOutputHelper _testOutputHelper ;
416- private ITcpServer _server ;
423+ // In test.Dockerfile there must be an ENV IS_CONTAINERIZED (ENV IS_CONTAINERIZED=true )
424+ private bool _isRunningInContainer ;
425+ private double _connErrorsRatio ;
417426 }
418427}
0 commit comments