@@ -352,6 +352,67 @@ await WaitForConditionAsync(
352352 client . Dispose ( ) ;
353353 }
354354
355+ [ Fact ]
356+ public async Task ReconnectBackoff_ContinuesAfterFailedRetry_WhenNoNewerConnectionOwnsSocket ( )
357+ {
358+ using var server = new LoopbackWebSocketServer ( ) ;
359+ await server . StartAsync ( ) ;
360+ var client = new ReconnectBackoffRaceClient ( server . WebSocketUrl , "token" , _logger ) ;
361+ var statuses = new ConcurrentQueue < ConnectionStatus > ( ) ;
362+ client . StatusChanged += ( _ , s ) => statuses . Enqueue ( s ) ;
363+
364+ await client . ConnectAsync ( ) ;
365+ await client . FirstConnected . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
366+ await WaitForConditionAsync ( ( ) => server . AcceptedCount >= 1 , TimeSpan . FromSeconds ( 2 ) ) ;
367+
368+ await server . CloseSocketAsync ( 0 ) ;
369+ server . StopAccepting ( ) ;
370+
371+ await WaitForConditionAsync (
372+ ( ) => statuses . Count ( s => s == ConnectionStatus . Connecting ) >= 4 ,
373+ TimeSpan . FromSeconds ( 4 ) ) ;
374+
375+ Assert . Equal ( 1 , client . OnConnectedCallCount ) ;
376+ Assert . True ( _logger . Logs . Count (
377+ line => line . Contains ( "reconnecting in" , StringComparison . OrdinalIgnoreCase ) ) >= 2 ) ;
378+
379+ client . Dispose ( ) ;
380+ }
381+
382+ [ Fact ]
383+ public async Task ReconnectBackoff_ReconnectsCurrentClosingSocket_WhenSupersededLoopIsActive ( )
384+ {
385+ using var server = new LoopbackWebSocketServer ( ) ;
386+ await server . StartAsync ( ) ;
387+ var client = new ReconnectBackoffRaceClient ( server . WebSocketUrl , "token" , _logger ) ;
388+ var statuses = new ConcurrentQueue < ConnectionStatus > ( ) ;
389+ client . StatusChanged += ( _ , s ) => statuses . Enqueue ( s ) ;
390+
391+ await client . ConnectAsync ( ) ;
392+ await client . FirstConnected . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
393+ await WaitForConditionAsync ( ( ) => server . AcceptedCount >= 1 , TimeSpan . FromSeconds ( 2 ) ) ;
394+
395+ await server . CloseSocketAsync ( 0 ) ;
396+ await WaitForConditionAsync (
397+ ( ) => statuses . Count ( s => s == ConnectionStatus . Connecting ) >= 2 ,
398+ TimeSpan . FromSeconds ( 2 ) ) ;
399+
400+ await client . ConnectAsync ( ) ;
401+ await client . SecondConnected . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
402+ await WaitForConditionAsync ( ( ) => server . AcceptedCount >= 2 , TimeSpan . FromSeconds ( 2 ) ) ;
403+
404+ await server . CloseSocketAsync ( 1 ) ;
405+
406+ var reconnect = await Task . WhenAny (
407+ client . ThirdConnected ,
408+ Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ) ;
409+
410+ Assert . Same ( client . ThirdConnected , reconnect ) ;
411+ Assert . True ( server . AcceptedCount >= 3 ) ;
412+
413+ client . Dispose ( ) ;
414+ }
415+
355416 private static async Task WaitForConditionAsync ( Func < bool > predicate , TimeSpan timeout )
356417 {
357418 var start = DateTime . UtcNow ;
@@ -532,6 +593,13 @@ await socket.CloseOutputAsync(
532593 }
533594 }
534595
596+ public void StopAccepting ( )
597+ {
598+ _cts . Cancel ( ) ;
599+ try { _listener . Stop ( ) ; } catch { }
600+ try { _acceptLoop ? . Wait ( TimeSpan . FromSeconds ( 1 ) ) ; } catch { }
601+ }
602+
535603 public void Dispose ( )
536604 {
537605 _cts . Cancel ( ) ;
0 commit comments