33using System . Diagnostics ;
44using System . IO ;
55using System . Collections . Concurrent ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
68
79namespace COM3D2 . LiveLink . Tests
810{
@@ -70,27 +72,29 @@ public int StopCLIProcess(Process cliProcess)
7072 return cliProcess . ExitCode ;
7173 }
7274
73- public void StartClientCoreAndServerCLI ( out LiveLinkCore clientCore , out Process serverCLI )
75+ public void StartClientCoreAndServerCLI ( out LiveLinkCore clientCore , out Process serverCLI , out string address )
7476 {
75- serverCLI = CreateServerProcess ( out string address ) ;
77+ serverCLI = CreateServerProcess ( out address ) ;
7678 Console . WriteLine ( $ "address = { address } ") ;
7779
7880 clientCore = new LiveLinkCore ( ) ;
7981 Assert . IsTrue ( clientCore . StartClient ( address ) , "Failed to connect client to server." ) ;
80- clientCore . WaitForConnection ( ) ;
8182
8283 serverCLI . StandardInput . WriteLine ( $ "waitfor --connection --time 1") ;
8384 Assert . IsTrue ( clientCore . IsConnected ) ;
8485 }
8586
87+ public void StartClientCoreAndServerCLI ( out LiveLinkCore clientCore , out Process serverCLI )
88+ {
89+ StartClientCoreAndServerCLI ( out clientCore , out serverCLI , out string address ) ;
90+ }
91+
8692 public void StartServerCoreAndClientCLI ( out LiveLinkCore serverCore , out Process clientCLI )
8793 {
8894 serverCore = CreateServer ( ) ;
8995 clientCLI = CreateClientProcess ( serverCore . Address ) ;
9096
91- serverCore . WaitForConnection ( 1000 ) ;
92-
93- Assert . IsTrue ( serverCore . IsConnected ) ;
97+ Assert . IsTrue ( serverCore . WaitForConnection ( 1000 ) , "Client did not connect to server." ) ;
9498 }
9599
96100 [ TestMethod ]
@@ -104,7 +108,7 @@ public void TestClientCLI()
104108 int exitCode = - 1 ;
105109 try
106110 {
107- serverCore . WaitForConnection ( ) ;
111+ serverCore . WaitForConnection ( 1000 ) ;
108112 serverCore . SendString ( testString ) ;
109113 serverCore . Flush ( ) ;
110114
@@ -223,6 +227,34 @@ public void TestClientNoReadBlock()
223227 AssertContains ( output , testString ) ;
224228 }
225229
230+ [ TestMethod ]
231+ public void TestServerWaitForConnectionTimeout ( )
232+ {
233+ LiveLinkCore serverCore = CreateServer ( ) ;
234+
235+ var task = Task . Run ( ( ) => serverCore . WaitForConnection ( 10 ) ) ;
236+ Assert . IsTrue ( task . Wait ( 20 ) ) ;
237+ }
238+
239+ [ TestMethod ]
240+ public void TestServerListenForConnection ( )
241+ {
242+ LiveLinkCore serverCore = CreateServer ( ) ;
243+ Process clientCLI = CreateClientProcess ( serverCore . Address ) ;
244+
245+ Thread . Sleep ( 1000 ) ;
246+
247+ try
248+ {
249+ Assert . IsTrue ( serverCore . IsConnected , "serverCore.IsConnected = false" ) ;
250+ }
251+ finally
252+ {
253+ StopCLIProcess ( clientCLI ) ;
254+ }
255+ }
256+
257+
226258 [ TestMethod ]
227259 public void TestSendFile ( )
228260 {
@@ -275,14 +307,14 @@ public void TestFileCommand()
275307 serverCLI . StandardInput . WriteLine ( $ "send --file { filePath } ") ;
276308 serverCLI . StandardInput . WriteLine ( $ "flush") ;
277309
278- System . Threading . Thread . Sleep ( 100 ) ;
310+ Thread . Sleep ( 100 ) ;
279311
280312 bool recieved = clientCore . TryReadMessage ( out MemoryStream message ) ;
281313
282314 clientCore . Dispose ( ) ;
283315 int exitCode = StopCLIProcess ( serverCLI ) ;
284316 Console . WriteLine ( "Server Output - - - - - - - - -" ) ;
285- Console . Write ( serverCLI . StandardOutput . ReadAllAvailable ( ) ) ;
317+ Console . WriteLine ( serverCLI . StandardOutput . ReadAllAvailable ( ) ) ;
286318 Debug . Write ( serverCLI . StandardError . ReadAllAvailable ( ) ) ;
287319 Console . WriteLine ( "- - - - - - - - - - - - - - - -" ) ;
288320 Assert . That . ExitZero ( serverCLI ) ;
@@ -292,26 +324,29 @@ public void TestFileCommand()
292324 Assert . That . StreamsAreEqual ( fileStream , message ) ;
293325 }
294326 }
295-
296-
327+
297328 [ TestMethod ]
298- public void TestServerCLIDisconnect ( )
329+ public void TestServerIsConnected ( )
299330 {
300- StartClientCoreAndServerCLI ( out LiveLinkCore clientCore , out Process serverCLI ) ;
331+ StartServerCoreAndClientCLI ( out LiveLinkCore serverCore , out Process clientCLI ) ;
301332
302- Console . WriteLine ( $ "clientCore .IsConnected = { clientCore . IsConnected } ") ;
333+ Console . WriteLine ( $ "serverCore .IsConnected = { serverCore . IsConnected } ") ;
303334
304- serverCLI . StandardInput . WriteLine ( "disconnect" ) ;
335+ clientCLI . StandardInput . WriteLine ( "disconnect" ) ;
305336
306- System . Threading . Thread . Sleep ( 100 ) ;
337+ Thread . Sleep ( 1000 ) ;
307338
308- clientCore . ReadAll ( ) ;
339+ Console . WriteLine ( "Client Output - - - - - - - - -" ) ;
340+ Console . WriteLine ( clientCLI . StandardOutput . ReadAllAvailable ( ) ) ;
341+ Console . WriteLine ( "- - - - - - - - - - - - - - - -" ) ;
342+
343+ StopCLIProcess ( clientCLI ) ;
344+ Assert . That . ExitZero ( clientCLI ) ;
345+
346+ Console . WriteLine ( $ "serverCore.IsConnected = { serverCore . IsConnected } ") ;
347+ Assert . IsFalse ( serverCore . IsConnected , "serverCore.IsConnected is still still true." ) ;
309348
310- Console . WriteLine ( $ "clientCore.IsConnected = { clientCore . IsConnected } ") ;
311- Assert . IsFalse ( clientCore . IsConnected ) ;
312349
313- StopCLIProcess ( serverCLI ) ;
314- Assert . That . ExitZero ( serverCLI ) ;
315350 }
316351
317352 [ TestMethod ]
@@ -326,7 +361,7 @@ public void TestServerCLIUnexpectedDisconnect()
326361 }
327362
328363 [ TestMethod ]
329- public void TestServerCoreDisconnect ( )
364+ public void TestClientIsConnectedCLI ( )
330365 {
331366 StartServerCoreAndClientCLI ( out LiveLinkCore serverCore , out Process clientCLI ) ;
332367
@@ -348,6 +383,71 @@ public void TestServerCoreDisconnect()
348383 Assert . That . ExitZero ( clientCLI ) ;
349384 }
350385
386+ [ TestMethod ]
387+ public void TestServerCrashing ( )
388+ {
389+ StartClientCoreAndServerCLI ( out LiveLinkCore clientCore , out Process serverCLI , out string address ) ;
390+
391+ for ( int i = 0 ; i < 1 ; i ++ )
392+ {
393+ Console . WriteLine ( $ "Attempt { i } ") ;
394+
395+ // Crash area
396+ serverCLI . StandardInput . WriteLine ( "disconnect" ) ;
397+ while ( clientCore . IsConnected )
398+ {
399+ Thread . Sleep ( 0 ) ;
400+ }
401+ if ( serverCLI . HasExited ) break ;
402+
403+ // Reconnect for next attempt
404+ clientCore . Disconnect ( ) ;
405+ serverCLI . StandardInput . WriteLine ( $ "start --server { address } ") ;
406+ serverCLI . StandardInput . WriteLine ( $ "waitfor --connection") ;
407+
408+ bool connected = false ;
409+ for ( int j = 0 ; j < 10 ; j ++ )
410+ {
411+ if ( connected = clientCore . StartClient ( address ) ) break ;
412+ Thread . Sleep ( 0 ) ;
413+ }
414+ if ( serverCLI . HasExited ) break ;
415+ Assert . IsTrue ( connected , "Failed to connect client to server." ) ;
416+ }
417+ StopCLIProcess ( serverCLI ) ;
418+ Assert . That . ExitZero ( serverCLI ) ;
419+ }
420+
421+ [ TestMethod ]
422+ public void TestClientCrashingCLI ( )
423+ {
424+ StartServerCoreAndClientCLI ( out LiveLinkCore serverCore , out Process clientCLI ) ;
425+
426+ for ( int i = 0 ; i < 100 ; i ++ )
427+ {
428+ Console . WriteLine ( $ "Attempt { i } ") ;
429+
430+ // Crash area
431+ clientCLI . StandardInput . WriteLine ( "disconnect" ) ;
432+ int j = 0 ;
433+ while ( serverCore . IsConnected && ! clientCLI . HasExited )
434+ {
435+ if ( j ++ >= 300 ) break ;
436+ Thread . Sleep ( 1 ) ;
437+ }
438+ if ( clientCLI . HasExited ) break ;
439+ Assert . IsFalse ( serverCore . IsConnected , "Client never disconnected" ) ;
440+
441+ // Reconnect for next attempt
442+ serverCore . Disconnect ( ) ;
443+ serverCore . StartServer ( serverCore . Address ) ;
444+ clientCLI . StandardInput . WriteLine ( $ "start --client { serverCore . Address } ") ;
445+ Assert . IsTrue ( serverCore . WaitForConnection ( 1000 ) , "Client did not reconnect." ) ;
446+ }
447+ StopCLIProcess ( clientCLI ) ;
448+ Assert . That . ExitZero ( clientCLI ) ;
449+ }
450+
351451
352452
353453 void AssertContains ( string output , string mustContain )
0 commit comments