コード例 #1
0
 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
コード例 #2
0
 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)}
コード例 #3
0
    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()
コード例 #4
0
 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()
コード例 #5
0
 def test_should_become_stale_on_expiry(self):
     table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
     table.ttl = 0
     assert not table.is_fresh()
コード例 #6
0
 def test_should_be_fresh_after_update(self):
     table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
     assert table.is_fresh()
コード例 #7
0
 def test_should_fail_on_multiple_records(self):
     with self.assertRaises(ProtocolError):
         _ = RoutingTable.parse_routing_info(
             [VALID_ROUTING_RECORD, VALID_ROUTING_RECORD])
コード例 #8
0
 def test_should_fail_on_zero_records(self):
     with self.assertRaises(ProtocolError):
         _ = RoutingTable.parse_routing_info([])
コード例 #9
0
 def test_should_fail_on_invalid_record(self):
     with self.assertRaises(ProtocolError):
         _ = RoutingTable.parse_routing_info([INVALID_ROUTING_RECORD])
コード例 #10
0
 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)