@@ -35,6 +35,19 @@ namespace Neo4j.Driver.Tests
3535{
3636 public class ConnectionPoolTests
3737 {
38+ internal static ConnectionPool NewConnectionPoolWithNoConnectionTimeoutValidation (
39+ IConnection connection ,
40+ BlockingCollection < IPooledConnection > availableConnections = null ,
41+ ConcurrentSet < IPooledConnection > inUseConnections = null )
42+ {
43+ var testConfigWithIdleTimeoutAndLifetimeCheckDisabled = new Config
44+ {
45+ MaxConnectionLifetime = Config . InfiniteInterval ,
46+ ConnectionIdleTimeout = Config . InfiniteInterval
47+ } ;
48+ return new ConnectionPool ( connection , availableConnections , inUseConnections ,
49+ poolSettings : new ConnectionPoolSettings ( testConfigWithIdleTimeoutAndLifetimeCheckDisabled ) ) ;
50+ }
3851 public class AcquireMethod
3952 {
4053 private readonly ITestOutputHelper _output ;
@@ -45,6 +58,17 @@ private IConnection MockedConnection
4558 {
4659 var mock = new Mock < IPooledConnection > ( ) ;
4760 mock . Setup ( x => x . IsOpen ) . Returns ( true ) ;
61+ mock . Setup ( x => x . IdleTimer ) . Returns ( MockedTimer ) ;
62+ mock . Setup ( x => x . LifetimeTimer ) . Returns ( MockedTimer ) ;
63+ return mock . Object ;
64+ }
65+ }
66+
67+ private ITimer MockedTimer
68+ {
69+ get {
70+ var mock = new Mock < ITimer > ( ) ;
71+ mock . Setup ( t => t . ElapsedMilliseconds ) . Returns ( 0 ) ;
4872 return mock . Object ;
4973 }
5074 }
@@ -72,7 +96,7 @@ public void ShouldCallConnInit()
7296 public void ShouldBlockWhenMaxPoolSizeReached ( )
7397 {
7498 var connectionPoolSettings = new ConnectionPoolSettings ( new Config { MaxConnectionPoolSize = 2 } ) ;
75- var pool = new ConnectionPool ( MockedConnection , settings : connectionPoolSettings ) ;
99+ var pool = new ConnectionPool ( MockedConnection , poolSettings : connectionPoolSettings ) ;
76100 var conn = pool . Acquire ( ) ;
77101 pool . Acquire ( ) ;
78102 pool . NumberOfAvailableConnections . Should ( ) . Be ( 0 ) ;
@@ -105,7 +129,7 @@ public void ShouldThrowClientExceptionWhenFailedToAcquireWithinTimeout()
105129 MaxConnectionPoolSize = 2 ,
106130 ConnectionAcquisitionTimeout = TimeSpan . FromMilliseconds ( 0 )
107131 } ) ;
108- var pool = new ConnectionPool ( MockedConnection , settings : connectionPoolSettings ) ;
132+ var pool = new ConnectionPool ( MockedConnection , poolSettings : connectionPoolSettings ) ;
109133 var conn = pool . Acquire ( ) ;
110134 pool . Acquire ( ) ;
111135 pool . NumberOfAvailableConnections . Should ( ) . Be ( 0 ) ;
@@ -120,7 +144,7 @@ public void ShouldThrowClientExceptionWhenFailedToAcquireWithinTimeout()
120144 public void ShouldNotExceedIdleLimit ( )
121145 {
122146 var connectionPoolSettings = new ConnectionPoolSettings ( new Config { MaxIdleConnectionPoolSize = 2 } ) ;
123- var pool = new ConnectionPool ( MockedConnection , settings : connectionPoolSettings ) ;
147+ var pool = new ConnectionPool ( MockedConnection , poolSettings : connectionPoolSettings ) ;
124148
125149 var conns = new List < IConnection > ( ) ;
126150 for ( var i = 0 ; i < 4 ; i ++ )
@@ -142,7 +166,7 @@ public void ShouldNotExceedIdleLimit()
142166 public void ShouldAcquireFromPoolIfAvailable ( )
143167 {
144168 var connectionPoolSettings = new ConnectionPoolSettings ( new Config { MaxIdleConnectionPoolSize = 2 } ) ;
145- var pool = new ConnectionPool ( MockedConnection , settings : connectionPoolSettings ) ;
169+ var pool = new ConnectionPool ( MockedConnection , poolSettings : connectionPoolSettings ) ;
146170
147171 for ( var i = 0 ; i < 4 ; i ++ )
148172 {
@@ -211,6 +235,8 @@ public void ShouldReuseWhenOpenConnectionInQueue()
211235 var conns = new BlockingCollection < IPooledConnection > ( ) ;
212236 var mock = new Mock < IPooledConnection > ( ) ;
213237 mock . Setup ( x => x . IsOpen ) . Returns ( true ) ;
238+ mock . Setup ( x => x . IdleTimer ) . Returns ( MockedTimer ) ;
239+ mock . Setup ( x => x . LifetimeTimer ) . Returns ( MockedTimer ) ;
214240
215241 conns . Add ( mock . Object ) ;
216242 var pool = new ConnectionPool ( MockedConnection , conns ) ;
@@ -237,7 +263,7 @@ public void ShouldReuseOpenConnectionWhenOpenAndClosedConnectionsInQueue()
237263
238264 conns . Add ( unhealthyMock . Object ) ;
239265 conns . Add ( healthyMock . Object ) ;
240- var pool = new ConnectionPool ( MockedConnection , conns ) ;
266+ var pool = NewConnectionPoolWithNoConnectionTimeoutValidation ( MockedConnection , conns ) ;
241267
242268 pool . NumberOfAvailableConnections . Should ( ) . Be ( 2 ) ;
243269 pool . NumberOfInUseConnections . Should ( ) . Be ( 0 ) ;
@@ -268,7 +294,7 @@ public void ShouldCloseIdleTooLongConn()
268294 var enableIdleTooLongTest = TimeSpan . FromMilliseconds ( 100 ) ;
269295 var poolSettings = new ConnectionPoolSettings (
270296 new Config { MaxIdleConnectionPoolSize = 2 , ConnectionIdleTimeout = enableIdleTooLongTest } ) ;
271- var pool = new ConnectionPool ( MockedConnection , conns , settings : poolSettings ) ;
297+ var pool = new ConnectionPool ( MockedConnection , conns , poolSettings : poolSettings ) ;
272298
273299 pool . NumberOfAvailableConnections . Should ( ) . Be ( 1 ) ;
274300 pool . NumberOfInUseConnections . Should ( ) . Be ( 0 ) ;
@@ -301,8 +327,12 @@ public void ShouldReuseIdleNotTooLongConn()
301327 conns . Add ( mock . Object ) ;
302328 var enableIdleTooLongTest = TimeSpan . FromMilliseconds ( 100 ) ;
303329 var poolSettings = new ConnectionPoolSettings (
304- new Config { MaxIdleConnectionPoolSize = 2 , ConnectionIdleTimeout = enableIdleTooLongTest } ) ;
305- var pool = new ConnectionPool ( MockedConnection , conns , settings : poolSettings ) ;
330+ new Config {
331+ MaxIdleConnectionPoolSize = 2 ,
332+ ConnectionIdleTimeout = enableIdleTooLongTest ,
333+ MaxConnectionLifetime = Config . InfiniteInterval , // disable life time check
334+ } ) ;
335+ var pool = new ConnectionPool ( MockedConnection , conns , poolSettings : poolSettings ) ;
306336
307337 pool . NumberOfAvailableConnections . Should ( ) . Be ( 1 ) ;
308338 pool . NumberOfInUseConnections . Should ( ) . Be ( 0 ) ;
@@ -343,7 +373,7 @@ public void ShouldAcquireNewWhenBeingUsedConcurrentlyBy(int numberOfThreads)
343373 mockConns . Enqueue ( mock ) ;
344374 }
345375
346- var pool = new ConnectionPool ( MockedConnection , conns ) ;
376+ var pool = NewConnectionPoolWithNoConnectionTimeoutValidation ( MockedConnection , conns ) ;
347377
348378 pool . NumberOfAvailableConnections . Should ( ) . Be ( numberOfThreads ) ;
349379 pool . NumberOfInUseConnections . Should ( ) . Be ( 0 ) ;
@@ -407,7 +437,7 @@ public void ShouldCloseAcquiredConnectionIfPoolDisposeStarted()
407437 // Given
408438 var conns = new BlockingCollection < IPooledConnection > ( ) ;
409439 var healthyMock = new Mock < IPooledConnection > ( ) ;
410- var pool = new ConnectionPool ( MockedConnection , conns ) ;
440+ var pool = NewConnectionPoolWithNoConnectionTimeoutValidation ( MockedConnection , conns ) ;
411441
412442 pool . NumberOfAvailableConnections . Should ( ) . Be ( 0 ) ;
413443 pool . NumberOfInUseConnections . Should ( ) . Be ( 0 ) ;
@@ -453,6 +483,7 @@ public void ShouldTimeoutAfterAcquireTimeoutIfPoolIsFull()
453483 public void ShouldTimeoutAfterAcquireTimeoutWhenConnectionIsNotValidated ( )
454484 {
455485 Config config = Config . Builder . WithConnectionAcquisitionTimeout ( TimeSpan . FromSeconds ( 5 ) )
486+ . WithConnectionTimeout ( Config . InfiniteInterval )
456487 . ToConfig ( ) ;
457488
458489 var notValidConnection = new Mock < IPooledConnection > ( ) ;
@@ -577,12 +608,15 @@ public void ShouldCloseTheConnectionWhenConnectionIsReusableButThePoolIsFull()
577608
578609 var availableConns = new BlockingCollection < IPooledConnection > ( ) ;
579610 var pooledConnMock = new Mock < IPooledConnection > ( ) ;
580- for ( int i = 0 ; i < Config . DefaultConfig . MaxIdleConnectionPoolSize ; i ++ )
611+ var poolSettings = new ConnectionPoolSettings (
612+ new Config { MaxConnectionPoolSize = 10 } ) ;
613+
614+ for ( int i = 0 ; i < poolSettings . MaxIdleConnectionPoolSize ; i ++ )
581615 {
582616 availableConns . Add ( pooledConnMock . Object ) ;
583617 }
584618
585- var pool = new ConnectionPool ( null , availableConns , inUseConns ) ;
619+ var pool = new ConnectionPool ( null , availableConns , inUseConns , poolSettings : poolSettings ) ;
586620
587621 pool . NumberOfAvailableConnections . Should ( ) . Be ( 10 ) ;
588622 pool . NumberOfInUseConnections . Should ( ) . Be ( 1 ) ;
@@ -609,7 +643,7 @@ public void ShouldStartTimerBeforeReturnToPoolWhenIdleDetectionEnabled()
609643 var poolSettings = new ConnectionPoolSettings (
610644 new Config { MaxIdleConnectionPoolSize = 2 , ConnectionIdleTimeout = enableIdleTooLongTest } ) ;
611645 ;
612- var pool = new ConnectionPool ( null , null , inUseConns , settings : poolSettings ) ;
646+ var pool = new ConnectionPool ( null , null , inUseConns , poolSettings : poolSettings ) ;
613647
614648 pool . NumberOfAvailableConnections . Should ( ) . Be ( 0 ) ;
615649 pool . NumberOfInUseConnections . Should ( ) . Be ( 1 ) ;
0 commit comments