@@ -303,6 +303,59 @@ def test_should_flag_reading_without_writer(self):
303303 pool .ensure_routing_table_is_fresh (READ_ACCESS )
304304 assert pool .missing_writer
305305
306+ def test_should_purge_idle_connections_from_connection_pool (self ):
307+ with StubCluster ({9006 : "router.script" , 9001 : "router_with_multiple_servers.script" }):
308+ address = ("127.0.0.1" , 9006 )
309+ with RoutingPool (address ) as pool :
310+ # close the acquired connection with init router and then set it to be idle
311+ conn = pool .acquire (WRITE_ACCESS )
312+ conn .close ()
313+ conn .in_use = False
314+
315+ table = pool .routing_table
316+ assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 ),
317+ ("127.0.0.1" , 9003 )}
318+ assert table .readers == {("127.0.0.1" , 9004 ), ("127.0.0.1" , 9005 )}
319+ assert table .writers == {("127.0.0.1" , 9006 )}
320+ assert set (pool .connections .keys ()) == {("127.0.0.1" , 9006 )}
321+
322+ # immediately expire the routing table to enforce update a new routing table
323+ pool .routing_table .ttl = 0
324+ pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
325+ table = pool .routing_table
326+ assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 )}
327+ assert table .readers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9003 )}
328+ assert table .writers == {("127.0.0.1" , 9004 )}
329+
330+ assert set (pool .connections .keys ()) == {("127.0.0.1" , 9001 )}
331+
332+ def test_should_not_purge_idle_connections_from_connection_pool (self ):
333+ with StubCluster ({9006 : "router.script" , 9001 : "router_with_multiple_servers.script" }):
334+ address = ("127.0.0.1" , 9006 )
335+ with RoutingPool (address ) as pool :
336+ # close the acquired connection with init router and then set it to be inUse
337+ conn = pool .acquire (WRITE_ACCESS )
338+ conn .close ()
339+ conn .in_use = True
340+
341+ table = pool .routing_table
342+ assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 ),
343+ ("127.0.0.1" , 9003 )}
344+ assert table .readers == {("127.0.0.1" , 9004 ), ("127.0.0.1" , 9005 )}
345+ assert table .writers == {("127.0.0.1" , 9006 )}
346+ assert set (pool .connections .keys ()) == {("127.0.0.1" , 9006 )}
347+
348+ # immediately expire the routing table to enforce update a new routing table
349+ pool .routing_table .ttl = 0
350+ pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
351+ table = pool .routing_table
352+ assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 )}
353+ assert table .readers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9003 )}
354+ assert table .writers == {("127.0.0.1" , 9004 )}
355+
356+ assert set (pool .connections .keys ()) == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9006 )}
357+
358+
306359 # TODO: fix flaky test
307360 # def test_concurrent_refreshes_should_not_block_if_fresh(self):
308361 # address = ("127.0.0.1", 9001)
@@ -481,15 +534,15 @@ def test_should_error_to_writer_in_absent_of_reader(self):
481534 assert not pool .missing_writer
482535
483536
484- class RoutingConnectionPoolRemoveTestCase (StubTestCase ):
537+ class RoutingConnectionPoolDeactivateTestCase (StubTestCase ):
485538 def test_should_remove_router_from_routing_table_if_present (self ):
486539 with StubCluster ({9001 : "router.script" }):
487540 address = ("127.0.0.1" , 9001 )
488541 with RoutingPool (address ) as pool :
489542 pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
490543 target = ("127.0.0.1" , 9001 )
491544 assert target in pool .routing_table .routers
492- pool .remove (target )
545+ pool .deactivate (target )
493546 assert target not in pool .routing_table .routers
494547
495548 def test_should_remove_reader_from_routing_table_if_present (self ):
@@ -499,7 +552,7 @@ def test_should_remove_reader_from_routing_table_if_present(self):
499552 pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
500553 target = ("127.0.0.1" , 9004 )
501554 assert target in pool .routing_table .readers
502- pool .remove (target )
555+ pool .deactivate (target )
503556 assert target not in pool .routing_table .readers
504557
505558 def test_should_remove_writer_from_routing_table_if_present (self ):
@@ -509,7 +562,7 @@ def test_should_remove_writer_from_routing_table_if_present(self):
509562 pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
510563 target = ("127.0.0.1" , 9006 )
511564 assert target in pool .routing_table .writers
512- pool .remove (target )
565+ pool .deactivate (target )
513566 assert target not in pool .routing_table .writers
514567
515568 def test_should_not_fail_if_absent (self ):
@@ -518,4 +571,4 @@ def test_should_not_fail_if_absent(self):
518571 with RoutingPool (address ) as pool :
519572 pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
520573 target = ("127.0.0.1" , 9007 )
521- pool .remove (target )
574+ pool .deactivate (target )
0 commit comments