def test_address_accessible(self): config = Mock() config.use_ipv6 = False server = PendingConnectionsServer(config, Mock()) assert not server._is_address_accessible(None) sockv4 = SocketAddress('8.8.8.8', 40100) sockv6 = SocketAddress('2001:0db8:85a3:0000:0000:8a2e:abcd:efea', 40100) assert server._is_address_accessible(sockv4) assert not server._is_address_accessible(sockv6) server.use_ipv6 = True assert server._is_address_accessible(sockv4) assert server._is_address_accessible(sockv6)
def test_sync_pending(self): network = Network() server = PendingConnectionsServer(None, network) req_type = 0 final_failure_called = [False] node_info = Mock(key='1234', prv_addresses=["1.2.3.4"], pub_addr="1.2.3.4", pub_port=self.port) def final_failure(*args, **kwargs): final_failure_called[0] = True server.conn_established_for_type[req_type] = lambda x: x server.conn_failure_for_type[req_type] = server.final_conn_failure server.conn_final_failure_for_type[req_type] = final_failure server._is_address_accessible = Mock(return_value=True) server._add_pending_request(req_type, node_info, prv_port=node_info.prv_port, pub_port=node_info.pub_port, args={}) assert len(server.pending_connections) == 1 server._sync_pending() assert network.connected network.connected = False server.pending_connections = {} server._add_pending_request(req_type, self.node_info, prv_port=self.node_info.prv_port, pub_port=self.node_info.pub_port, args={}) assert len(server.pending_connections) == 1 pending_conn = next(iter(list(server.pending_connections.values()))) pending_conn.socket_addresses = [] server._sync_pending() assert not network.connected assert final_failure_called[0]
def test_pending_conn(self): network = Network() server = PendingConnectionsServer(None, network) req_type = 0 final_failure_called = [False] def final_failure(*args, **kwargs): final_failure_called[0] = True server.conn_established_for_type[req_type] = lambda x: x server.conn_failure_for_type[req_type] = server.final_conn_failure server.conn_final_failure_for_type[req_type] = final_failure server._is_address_accessible = Mock(return_value=True) server._add_pending_request(req_type, self.node_info, prv_port=self.node_info.prv_port, pub_port=self.node_info.pub_port, args={}) assert len(server.pending_connections) == 1 pending_conn = next(iter(list(server.pending_connections.values()))) final_failure_called[0] = False server.final_conn_failure(pending_conn.id) assert final_failure_called[0] server.verified_conn(pending_conn.id) assert len(server.pending_connections) == 0 final_failure_called[0] = False server.final_conn_failure(pending_conn.id) assert not final_failure_called[0] server._add_pending_request(req_type, self.node_info, prv_port=self.node_info.prv_port, pub_port=self.node_info.pub_port, args={}) pending_conn = next(iter(list(server.pending_connections.values()))) server._mark_connected(pending_conn.id, "10.10.10.1", self.port) assert pending_conn.status == PenConnStatus.Connected assert SocketAddress("10.10.10.1", self.port) == pending_conn.socket_addresses[0]