@@ -47,7 +47,7 @@ public void ShouldEnsureInitialRouter()
4747 var pool = new ClusterConnectionPool ( connSettings , poolSettings , bufferSettings , uris , null ) ;
4848
4949 pool . ToString ( ) . Should ( ) . Be (
50- "[{bolt://123:456/ : _availableConnections : {[]}, _inUseConnections: {[]}}]" ) ;
50+ "[{bolt://123:456/ : _idleConnections : {[]}, _inUseConnections: {[]}}]" ) ;
5151 }
5252 }
5353
@@ -139,7 +139,7 @@ public void ShouldAddNewConnectionPoolIfDoesNotExist()
139139 var pool = new ClusterConnectionPool ( mockedConnectionPool . Object , connectionPoolDict ) ;
140140
141141 // When
142- pool . Update ( new [ ] { ServerUri } ) ;
142+ pool . Update ( new [ ] { ServerUri } , new Uri [ 0 ] ) ;
143143
144144 // Then
145145 connectionPoolDict . Count . Should ( ) . Be ( 1 ) ;
@@ -148,7 +148,7 @@ public void ShouldAddNewConnectionPoolIfDoesNotExist()
148148 }
149149
150150 [ Fact ]
151- public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled ( )
151+ public void ShouldRemoveNewlyCreatedPoolIfDisposeAlreadyCalled ( )
152152 {
153153 // Given
154154 var mockedConnectionPool = new Mock < IConnectionPool > ( ) ;
@@ -157,7 +157,7 @@ public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled()
157157
158158 // When
159159 pool . Dispose ( ) ;
160- var exception = Record . Exception ( ( ) => pool . Update ( new [ ] { ServerUri } ) ) ;
160+ var exception = Record . Exception ( ( ) => pool . Update ( new [ ] { ServerUri } , new Uri [ 0 ] ) ) ;
161161
162162 // Then
163163 mockedConnectionPool . Verify ( x => x . Close ( ) ) ;
@@ -173,20 +173,40 @@ public void ShouldRemoveServerPoolIfNotPresentInNewServers()
173173 var mockedConnectionPool = new Mock < IConnectionPool > ( ) ;
174174 var connectionPoolDict = new ConcurrentDictionary < Uri , IConnectionPool > ( ) ;
175175 connectionPoolDict . GetOrAdd ( ServerUri , mockedConnectionPool . Object ) ;
176+ mockedConnectionPool . Setup ( x => x . NumberOfInUseConnections ) . Returns ( 0 ) ; // no need to explicitly config this
176177 var pool = new ClusterConnectionPool ( mockedConnectionPool . Object , connectionPoolDict ) ;
177178
178179 // When
179- pool . Update ( new Uri [ 0 ] ) ;
180+ pool . Update ( new Uri [ 0 ] , new [ ] { ServerUri } ) ;
180181
181182 // Then
183+ mockedConnectionPool . Verify ( x => x . Deactivate ( ) , Times . Once ) ; // first deactivate then remove
182184 connectionPoolDict . Count . Should ( ) . Be ( 0 ) ;
183185 }
186+
187+ [ Fact ]
188+ public void ShouldDeactivateServerPoolIfNotPresentInNewServersButHasInUseConnections ( )
189+ {
190+ // Given
191+ var mockedConnectionPool = new Mock < IConnectionPool > ( ) ;
192+ var connectionPoolDict = new ConcurrentDictionary < Uri , IConnectionPool > ( ) ;
193+ connectionPoolDict . GetOrAdd ( ServerUri , mockedConnectionPool . Object ) ;
194+ mockedConnectionPool . Setup ( x => x . NumberOfInUseConnections ) . Returns ( 10 ) ; // non-zero number
195+ var pool = new ClusterConnectionPool ( mockedConnectionPool . Object , connectionPoolDict ) ;
196+
197+ // When
198+ pool . Update ( new Uri [ 0 ] , new [ ] { ServerUri } ) ;
199+
200+ // Then
201+ mockedConnectionPool . Verify ( x => x . Deactivate ( ) , Times . Once ) ;
202+ connectionPoolDict . Count . Should ( ) . Be ( 1 ) ;
203+ }
184204 }
185205
186- public class PurgeMethod
206+ public class AddMethod
187207 {
188208 [ Fact ]
189- public void ShouldRemovedIfExist ( )
209+ public void ShouldActivateIfExist ( )
190210 {
191211 // Given
192212 var mockedConnectionPool = new Mock < IConnectionPool > ( ) ;
@@ -196,24 +216,64 @@ public void ShouldRemovedIfExist()
196216 var pool = new ClusterConnectionPool ( null , connectionPoolDict ) ;
197217
198218 // When
199- pool . Purge ( ServerUri ) ;
219+ pool . Add ( new [ ] { ServerUri } ) ;
200220
201221 // Then
202- mockedConnectionPool . Verify ( x => x . Close ( ) , Times . Once ) ;
203- connectionPoolDict . Count . Should ( ) . Be ( 0 ) ;
204- connectionPoolDict . ContainsKey ( ServerUri ) . Should ( ) . BeFalse ( ) ;
222+ mockedConnectionPool . Verify ( x => x . Activate ( ) , Times . Once ) ;
223+ connectionPoolDict . Count . Should ( ) . Be ( 1 ) ;
224+ connectionPoolDict . ContainsKey ( ServerUri ) . Should ( ) . BeTrue ( ) ;
225+ }
226+
227+ [ Fact ]
228+ public void ShouldAddIfNotFound ( )
229+ {
230+ // Given
231+ var connectionPoolDict = new ConcurrentDictionary < Uri , IConnectionPool > ( ) ;
232+ var fakePoolMock = new Mock < IConnectionPool > ( ) ;
233+
234+ var pool = new ClusterConnectionPool ( fakePoolMock . Object , connectionPoolDict ) ;
235+
236+ // When
237+ pool . Add ( new [ ] { ServerUri } ) ;
238+
239+ // Then
240+ connectionPoolDict . Count . Should ( ) . Be ( 1 ) ;
241+ connectionPoolDict . ContainsKey ( ServerUri ) . Should ( ) . BeTrue ( ) ;
242+ connectionPoolDict [ ServerUri ] . Should ( ) . Be ( fakePoolMock . Object ) ;
243+ }
244+ }
245+
246+ public class DeactivateMethod
247+ {
248+ [ Fact ]
249+ public void ShouldDeactivateIfExist ( )
250+ {
251+ // Given
252+ var mockedConnectionPool = new Mock < IConnectionPool > ( ) ;
253+ var connectionPoolDict = new ConcurrentDictionary < Uri , IConnectionPool > ( ) ;
254+ connectionPoolDict . GetOrAdd ( ServerUri , mockedConnectionPool . Object ) ;
255+
256+ var pool = new ClusterConnectionPool ( null , connectionPoolDict ) ;
257+
258+ // When
259+ pool . Deactivate ( ServerUri ) ;
260+
261+ // Then
262+ mockedConnectionPool . Verify ( x => x . Deactivate ( ) , Times . Once ) ;
263+ connectionPoolDict . Count . Should ( ) . Be ( 1 ) ;
264+ connectionPoolDict . ContainsKey ( ServerUri ) . Should ( ) . BeTrue ( ) ;
205265 }
206266
207267 [ Fact ]
208- public void ShouldRemoveNothingIfNotFound ( )
268+ public void ShouldDeactivateNothingIfNotFound ( )
209269 {
210270 // Given
211271 var connectionPoolDict = new ConcurrentDictionary < Uri , IConnectionPool > ( ) ;
212272
213273 var pool = new ClusterConnectionPool ( null , connectionPoolDict ) ;
214274
215275 // When
216- pool . Purge ( ServerUri ) ;
276+ pool . Deactivate ( ServerUri ) ;
217277
218278 // Then
219279 connectionPoolDict . Count . Should ( ) . Be ( 0 ) ;
@@ -273,4 +333,4 @@ public void ShouldReturnCorrectCountForPresentAddress()
273333 }
274334 }
275335 }
276- }
336+ }
0 commit comments