@@ -303,6 +303,59 @@ def test_should_flag_reading_without_writer(self):
303
303
pool .ensure_routing_table_is_fresh (READ_ACCESS )
304
304
assert pool .missing_writer
305
305
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
+
306
359
# TODO: fix flaky test
307
360
# def test_concurrent_refreshes_should_not_block_if_fresh(self):
308
361
# address = ("127.0.0.1", 9001)
@@ -481,15 +534,15 @@ def test_should_error_to_writer_in_absent_of_reader(self):
481
534
assert not pool .missing_writer
482
535
483
536
484
- class RoutingConnectionPoolRemoveTestCase (StubTestCase ):
537
+ class RoutingConnectionPoolDeactivateTestCase (StubTestCase ):
485
538
def test_should_remove_router_from_routing_table_if_present (self ):
486
539
with StubCluster ({9001 : "router.script" }):
487
540
address = ("127.0.0.1" , 9001 )
488
541
with RoutingPool (address ) as pool :
489
542
pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
490
543
target = ("127.0.0.1" , 9001 )
491
544
assert target in pool .routing_table .routers
492
- pool .remove (target )
545
+ pool .deactivate (target )
493
546
assert target not in pool .routing_table .routers
494
547
495
548
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):
499
552
pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
500
553
target = ("127.0.0.1" , 9004 )
501
554
assert target in pool .routing_table .readers
502
- pool .remove (target )
555
+ pool .deactivate (target )
503
556
assert target not in pool .routing_table .readers
504
557
505
558
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):
509
562
pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
510
563
target = ("127.0.0.1" , 9006 )
511
564
assert target in pool .routing_table .writers
512
- pool .remove (target )
565
+ pool .deactivate (target )
513
566
assert target not in pool .routing_table .writers
514
567
515
568
def test_should_not_fail_if_absent (self ):
@@ -518,4 +571,4 @@ def test_should_not_fail_if_absent(self):
518
571
with RoutingPool (address ) as pool :
519
572
pool .ensure_routing_table_is_fresh (WRITE_ACCESS )
520
573
target = ("127.0.0.1" , 9007 )
521
- pool .remove (target )
574
+ pool .deactivate (target )
0 commit comments