@@ -440,5 +440,55 @@ private async Task<SnowflakeDbConnection> OpenConnectionAsync(string connectionS
440440 await connection . OpenAsync ( ) . ConfigureAwait ( false ) ;
441441 return connection ;
442442 }
443+
444+ [ Test ]
445+ public void TestReturningCancelledSessionsToThePool ( [ Values ] bool cancelAsync )
446+ {
447+ var connectionString = ConnectionString + "minPoolSize=0;maxPoolSize=2;application=TestReturningCancelledSessionsToThePool" ;
448+
449+ var pool = SnowflakeDbConnectionPool . ConnectionManager . GetPool ( connectionString ) ;
450+ pool . ClearSessions ( ) ;
451+
452+ // pool is empty
453+ Assert . AreEqual ( 0 , pool . GetCurrentState ( ) . IdleSessionsCount ) ;
454+ Assert . AreEqual ( 0 , pool . GetCurrentState ( ) . BusySessionsCount ) ;
455+
456+ var cts = new CancellationTokenSource ( ) ;
457+ var task = Task . Run ( async ( ) =>
458+ {
459+ using ( var connection = new SnowflakeDbConnection ( connectionString ) )
460+ {
461+ await connection . OpenAsync ( cts . Token ) ;
462+ using ( var command = connection . CreateCommand ( ) )
463+ {
464+ command . CommandText = $ "SELECT SYSTEM$WAIT(20)";
465+ await command . ExecuteNonQueryAsync ( cts . Token ) ;
466+ }
467+ }
468+ } , CancellationToken . None ) ;
469+
470+ Thread . Sleep ( 2000 ) ;
471+
472+ // one busy session
473+ Assert . AreEqual ( 0 , pool . GetCurrentState ( ) . IdleSessionsCount ) ;
474+ Assert . AreEqual ( 1 , pool . GetCurrentState ( ) . BusySessionsCount ) ;
475+
476+ if ( cancelAsync )
477+ #if NET8_0_OR_GREATER
478+ cts . CancelAsync ( ) ;
479+ #else
480+ cts . Cancel ( ) ;
481+ #endif
482+ else
483+ cts . Cancel ( ) ;
484+
485+ // operation cancelled properly
486+ var thrown = Assert . Throws < AggregateException > ( ( ) => task . Wait ( ) ) ;
487+ Assert . IsInstanceOf < OperationCanceledException > ( thrown . InnerException ) ;
488+
489+ // one idle session
490+ Assert . AreEqual ( 1 , pool . GetCurrentState ( ) . IdleSessionsCount ) ;
491+ Assert . AreEqual ( 0 , pool . GetCurrentState ( ) . BusySessionsCount ) ;
492+ }
443493 }
444494}
0 commit comments