def setUp(self): self.table = RoutingTable([("192.168.1.1", 7687), ("192.168.1.2", 7687)], [("192.168.1.3", 7687)], [], 0) self.new_table = RoutingTable([("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)], [("127.0.0.1", 9004), ("127.0.0.1", 9005)], [("127.0.0.1", 9006)], 300)
class RoutingTableUpdateTestCase(TestCase): def setUp(self): self.table = RoutingTable([("192.168.1.1", 7687), ("192.168.1.2", 7687)], [("192.168.1.3", 7687)], [], 0) self.new_table = RoutingTable([("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)], [("127.0.0.1", 9004), ("127.0.0.1", 9005)], [("127.0.0.1", 9006)], 300) def test_update_should_replace_routers(self): self.table.update(self.new_table) assert self.table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)} def test_update_should_replace_readers(self): self.table.update(self.new_table) assert self.table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)} def test_update_should_replace_writers(self): self.table.update(self.new_table) assert self.table.writers == {("127.0.0.1", 9006)} def test_update_should_replace_ttl(self): self.table.update(self.new_table) assert self.table.ttl == 300
def test_should_return_routing_table_on_valid_record(self): table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) assert table.routers == {('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003)} assert table.readers == {('127.0.0.1', 9004), ('127.0.0.1', 9005)} assert table.writers == {('127.0.0.1', 9006)} assert table.ttl == 300
def test_should_return_all_distinct_servers_in_routing_table(self): routing_table = { "ttl": 300, "servers": [ {"role": "ROUTE", "addresses": ["127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"]}, {"role": "READ", "addresses": ["127.0.0.1:9001", "127.0.0.1:9005"]}, {"role": "WRITE", "addresses": ["127.0.0.1:9002"]}, ], } table = RoutingTable.parse_routing_info([routing_table]) assert table.servers() == {('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003), ('127.0.0.1', 9005)}
def test_concurrent_refreshes_should_not_block_if_fresh(self): address = ("127.0.0.1", 9001) table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) with RoutingConnectionPool(connector, address) as pool: semaphore = Semaphore() class Refresher(Thread): refreshed = None def run(self): self.refreshed = pool.refresh_routing_table() class BlockingRefresher(Refresher): @classmethod def blocking_update(cls): pool.routing_table.update(table) semaphore.acquire() semaphore.release() return table def run(self): with patch.object(RoutingConnectionPool, "update_routing_table", side_effect=self.blocking_update): super(BlockingRefresher, self).run() first = BlockingRefresher() second = Refresher() assert not pool.routing_table.is_fresh() semaphore.acquire() first.start() second.start() sleep(1) assert not second.is_alive( ) # second call should return immediately without blocking second.join() semaphore.release() first.join() assert first.refreshed assert not second.refreshed assert pool.routing_table.is_fresh()
def test_should_fail_on_invalid_record(self): with self.assertRaises(ProtocolError): _ = RoutingTable.parse_routing_info([INVALID_ROUTING_RECORD])
def test_should_be_initially_stale(self): table = RoutingTable() assert not table.is_fresh()
def test_should_become_stale_if_no_readers(self): table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) table.readers.clear() assert not table.is_fresh(READ_ACCESS) assert table.is_fresh(WRITE_ACCESS)
def test_should_become_stale_on_expiry(self): table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) table.ttl = 0 assert not table.is_fresh()
def test_should_become_stale_if_no_writers(self): table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) table.writers.clear() assert not table.is_fresh()
def test_should_fail_on_multiple_records(self): with self.assertRaises(ProtocolError): _ = RoutingTable.parse_routing_info( [VALID_ROUTING_RECORD, VALID_ROUTING_RECORD])
def test_should_be_fresh_after_update(self): table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD]) assert table.is_fresh()
def test_should_fail_on_zero_records(self): with self.assertRaises(ProtocolError): _ = RoutingTable.parse_routing_info([])
def test_should_be_initially_stale(self): table = RoutingTable() assert not table.is_fresh(READ_ACCESS) assert not table.is_fresh(WRITE_ACCESS)
def test_should_parse_ip_address_and_port(self): parsed = RoutingTable.parse_address("127.0.0.1:7687") assert parsed == ("127.0.0.1", 7687)
def test_should_fail_on_non_numeric_port(self): with self.assertRaises(ValueError): _ = RoutingTable.parse_address("127.0.0.1:X")
def test_should_parse_host_name_and_port(self): parsed = RoutingTable.parse_address("localhost:7687") assert parsed == ("localhost", 7687)