def test_get_ping_latencies_one_country(self, mock_get_ping_latencies): relays = [ OutboundPeerModel("34.227.149.148", 1609, node_id="0", attributes={"country": "China"}), OutboundPeerModel("35.198.90.230", 1609, node_id="1", attributes={"country": "China"}), OutboundPeerModel("52.221.211.38", 1609, node_id="2", attributes={"country": "China"}), OutboundPeerModel("34.245.23.125", 1609, node_id="3", attributes={"country": "EU"}), OutboundPeerModel("34.238.245.201", 1609, node_id="4", attributes={"country": "EU"}), ] mock_get_ping_latencies.return_value = [ NodeLatencyInfo(relays[4], 100), NodeLatencyInfo(relays[1], 101), NodeLatencyInfo(relays[3], 109), NodeLatencyInfo(relays[2], 120), NodeLatencyInfo(relays[0], 130), ] best_relays = network_latency.get_best_relays_by_ping_latency_one_per_country( relays, 1) self.assertEqual(1, len(best_relays)) self.assertEqual("1", best_relays[0].node_id)
def test_get_best_relay_sorted_relays(self): relays = [ OutboundPeerModel("34.227.149.148", 1609, node_id="0", attributes={"country": "China"}), OutboundPeerModel("35.198.90.230", 1609, node_id="1", attributes={"country": "China"}), OutboundPeerModel("52.221.211.38", 1609, node_id="2", attributes={"country": "China"}), OutboundPeerModel("34.245.23.125", 1609, node_id="3", attributes={"country": "China"}), OutboundPeerModel("34.238.245.201", 1609, node_id="4", attributes={"country": "China"}), ] sorted_relays_ping_latency = [ NodeLatencyInfo(relays[0], 100), NodeLatencyInfo(relays[1], 101), NodeLatencyInfo(relays[2], 109), NodeLatencyInfo(relays[3], 120), NodeLatencyInfo(relays[4], 130), ] best_relay = network_latency._get_best_relay_latencies_one_per_country( sorted_relays_ping_latency, relays, 1) self.assertEqual("0", best_relay[0].node.node_id)
def test_get_best_relay_sorted_relays(self): relays = [ OutboundPeerModel("34.227.149.148", node_id="0"), OutboundPeerModel("35.198.90.230", node_id="1"), OutboundPeerModel("52.221.211.38", node_id="2"), OutboundPeerModel("34.245.23.125", node_id="3"), OutboundPeerModel("34.238.245.201", node_id="4") ] sorted_relays_ping_latency = [ NodeLatencyInfo(relays[0], 100), NodeLatencyInfo(relays[1], 101), NodeLatencyInfo(relays[2], 109), NodeLatencyInfo(relays[3], 120), NodeLatencyInfo(relays[4], 130) ] best_relay = network_latency._get_best_relay( sorted_relays_ping_latency, relays) self.assertEqual("0", best_relay.node.node_id)
def test_get_ping_latencies_current_relay_within_threshold_of_fastest( self, mock_get_ping_latencies): current_relay = OutboundPeerModel("52.221.211.38", 1609, node_id="2", attributes={"country": "EU"}) fastest_relay = OutboundPeerModel("35.198.90.230", 1609, node_id="1", attributes={"country": "EU"}) first_recommended_relay = OutboundPeerModel( "34.227.149.148", 1609, node_id="0", attributes={"country": "EU"}) self.peer_relays = set() self.peer_relays.add(current_relay) relays = [ first_recommended_relay, fastest_relay, current_relay, OutboundPeerModel("34.245.23.125", 1609, node_id="3", attributes={"country": "China"}), OutboundPeerModel("34.238.245.201", 1609, node_id="4", attributes={"country": "China"}), ] mock_get_ping_latencies.return_value = [ NodeLatencyInfo(relays[4], 100), NodeLatencyInfo(fastest_relay, 8), NodeLatencyInfo(relays[3], 109), NodeLatencyInfo(current_relay, 10), NodeLatencyInfo(first_recommended_relay, 9), ] best_relays = network_latency.get_best_relays_by_ping_latency_one_per_country( relays, 2, self.peer_relays) self.assertEqual(2, len(best_relays)) self.assertEqual("2", best_relays[0].node_id) self.assertEqual("3", best_relays[1].node_id)
def test_get_best_relay_less_relays(self): relays = [ OutboundPeerModel("34.227.149.148", 1609, node_id="0", attributes={"country": "China"}) ] sorted_relays_ping_latency = [NodeLatencyInfo(relays[0], 100)] best_relays = network_latency._get_best_relay_latencies_one_per_country( sorted_relays_ping_latency, relays, 2) self.assertEqual(1, len(best_relays)) self.assertEqual("0", best_relays[0].node.node_id)
def setUp(self) -> None: self.gateway_node = MockGatewayNode( gateway_helpers.get_gateway_opts(8000, split_relays=True)) self.gateway_node.alarm_queue = AlarmQueue() self.gateway_node.requester = ThreadedRequestService( "mock_thread_service", self.gateway_node.alarm_queue, constants.THREADED_HTTP_POOL_SLEEP_INTERVAL_S) self.gateway_node.requester.start() self.outbound_peer_models = [ OutboundPeerModel("1.1.1.1", 1609, node_type=NodeType.RELAY_BLOCK), OutboundPeerModel("1.1.1.2", 1609, node_type=NodeType.RELAY_BLOCK), OutboundPeerModel("1.1.1.3", 1609, node_type=NodeType.RELAY_BLOCK), ] sdn_http_service.fetch_potential_relay_peers_by_network = MagicMock( side_effect=lambda *args: self.outbound_peer_models) sdn_http_service.submit_peer_connection_error_event = MagicMock() self.latencies = [10, 20, 30] ping_latency.get_ping_latencies = MagicMock(side_effect=lambda *args: [ NodeLatencyInfo(self.outbound_peer_models[0], self.latencies[0]), NodeLatencyInfo(self.outbound_peer_models[1], self.latencies[1]), NodeLatencyInfo(self.outbound_peer_models[2], self.latencies[2]), ]) self.gateway_node.enqueue_connection = MagicMock()
def test_get_best_relay_one_relay(self): relays = [OutboundPeerModel("34.227.149.148", node_id="0")] sorted_relays_ping_latency = [NodeLatencyInfo(relays[0], 100)] best_relay = network_latency._get_best_relay( sorted_relays_ping_latency, relays) self.assertEqual("0", best_relay.node.node_id)