def test_task_result_connection_failure(self): """Tests what happens after connection failure when sending task_result""" ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) ts.network = MagicMock() ts.final_conn_failure = Mock() ts.task_computer = Mock() # Always fail on listening ts.network.listen = MagicMock( side_effect=lambda listen_info, waiting_task_result: TCPNetwork.__call_failure_callback( listen_info.failure_callback, {'waiting_task_result': waiting_task_result} ) ) # Try sending mocked task_result wtr = MagicMock() wtr.owner_key_id = 'owner_key_id' kwargs = {'waiting_task_result': wtr} ts._add_pending_request(TASK_CONN_TYPES['task_result'], 'owner_id', 'owner_port', wtr.owner_key_id, kwargs) ts._sync_pending() ts.client.want_to_start_task_session.assert_called_once_with( wtr.owner_key_id, ts.node, ANY, # conn_id )
def test_initiate_nat_traversal(self): ccd = self._get_config_desc() node = Node() node.nat_type = FullCone ts = TaskServer(node, ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() ts._add_pending_request = Mock() initiate = ts._TaskServer__initiate_nat_traversal key_id = 'key_id' node_info = {} super_node_info = Mock() ans_conn_id = 'conn_id' initiate(key_id, node_info, None, ans_conn_id) self.assertFalse(ts._add_pending_request.called) initiate(key_id, node_info, super_node_info, ans_conn_id) ts._add_pending_request.assert_called_with(TASK_CONN_TYPES['nat_punch'], ANY, ANY, ANY, ANY) node.nat_type = None initiate(key_id, node_info, super_node_info, ans_conn_id) ts._add_pending_request.assert_called_with(TASK_CONN_TYPES['middleman'], ANY, ANY, ANY, ANY)
def test_forwarded_session_requests(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() key_id = str(uuid.uuid4()) conn_id = str(uuid.uuid4()) subtask_id = str(uuid.uuid4()) ts.add_forwarded_session_request(key_id, conn_id) self.assertEqual(len(ts.forwarded_session_requests), 1) ts.forwarded_session_requests[key_id]['time'] = 0 ts._sync_forwarded_session_requests() self.assertEqual(len(ts.forwarded_session_requests), 0) ts.add_forwarded_session_request(key_id, conn_id) ts.forwarded_session_requests[key_id] = None ts._sync_forwarded_session_requests() self.assertEqual(len(ts.forwarded_session_requests), 0) session = MagicMock() session.address = '127.0.0.1' session.port = 65535 ts.conn_established_for_type[TASK_CONN_TYPES['task_failure']]( session, conn_id, key_id, subtask_id, "None" ) self.assertEqual(ts.task_sessions[subtask_id], session)
def test_send_waiting_results(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() ts._mark_connected = Mock() ts.task_computer = Mock() ts.task_manager = Mock() ts.task_manager.check_timeouts.return_value = [] ts.task_keeper = Mock() ts.task_connections_helper = Mock() ts._add_pending_request = Mock() subtask_id = 'xxyyzz' wtr = Mock() ts.results_to_send[subtask_id] = wtr wtr.already_sending = True wtr.last_sending_trial = 0 wtr.delay_time = 0 wtr.subtask_id = subtask_id wtr.address = '127.0.0.1' wtr.port = 10000 ts.sync_network() self.assertFalse(ts._add_pending_request.called) wtr.last_sending_trial = 0 ts.retry_sending_task_result(subtask_id) ts.sync_network() self.assertTrue(ts._add_pending_request.called) ts._add_pending_request.called = False ts.task_sessions[subtask_id] = Mock() ts.sync_network() self.assertFalse(ts._add_pending_request.called) ts._add_pending_request.called = False ts.results_to_send = dict() wtf = wtr ts.failures_to_send[subtask_id] = wtf ts.sync_network() self.assertFalse(ts._add_pending_request.called) self.assertEqual(ts.failures_to_send, {}) ts._add_pending_request.called = False ts.task_sessions.pop(subtask_id) ts.failures_to_send[subtask_id] = wtf ts.sync_network() self.assertTrue(ts._add_pending_request.called) self.assertEqual(ts.failures_to_send, {})
def test_traverse_nat(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() ts.traverse_nat("ABC", "10.10.10.10", 1312, 310319041904, "DEF") self.assertEqual(ts.network.connect.call_args[0][0].socket_addresses[0].address, "10.10.10.10") self.assertEqual(ts.network.connect.call_args[0][0].socket_addresses[0].port, 1312)
def test_conn_final_failures(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() ts.final_conn_failure = Mock() ts.task_computer = Mock() method = ts._TaskServer__connection_for_resource_request_final_failure method('conn_id', 'key_id', 'subtask_id', Mock()) ts.task_computer.resource_request_rejected.assert_called_once_with('subtask_id', ANY) ts.remove_pending_conn = Mock() ts.remove_responses = Mock() method = ts._TaskServer__connection_for_result_rejected_final_failure method('conn_id', 'key_id', 'subtask_id') self.assertTrue(ts.remove_pending_conn.called) self.assertTrue(ts.remove_responses.called_) ts.remove_pending_conn.called = False ts.remove_responses.called = False method = ts._TaskServer__connection_for_task_result_final_failure wtr = Mock() method('conn_id', 'key_id', wtr) self.assertTrue(ts.remove_pending_conn.called) self.assertTrue(ts.remove_responses.called) self.assertFalse(wtr.alreadySending) self.assertTrue(wtr.lastSendingTrial) ts.remove_pending_conn.called = False ts.remove_responses.called = False method = ts._TaskServer__connection_for_task_failure_final_failure method('conn_id', 'key_id', 'subtask_id', 'err_msg') self.assertTrue(ts.remove_pending_conn.called) self.assertTrue(ts.remove_responses.called) self.assertTrue(ts.task_computer.session_timeout.called) ts.remove_pending_conn.called = False ts.remove_responses.called = False ts.task_computer.session_timeout.called = False method = ts._TaskServer__connection_for_start_session_final_failure method('conn_id', 'key_id', Mock(), Mock(), 'ans_conn_id') self.assertTrue(ts.remove_pending_conn.called) self.assertTrue(ts.remove_responses.called) self.assertTrue(ts.task_computer.session_timeout.called)
def test_add_task_session(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() session = Mock() subtask_id = 'xxyyzz' ts.add_task_session(subtask_id, session) self.assertIsNotNone(ts.task_sessions[subtask_id])
def test_conn_for_start_session_failure(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() ts.final_conn_failure = Mock() method = ts._TaskServer__connection_for_start_session_failure method('conn_id', 'key_id', Mock(), Mock(), 'ans_conn_id') ts.final_conn_failure.assert_called_with('conn_id')
def test_remove_task_session(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() conn_id = str(uuid.uuid4()) session = Mock() session.conn_id = conn_id ts.remove_task_session(session) ts.task_sessions['task'] = session ts.remove_task_session(session)
def test_respond_to(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() session = Mock() ts.respond_to('key_id', session, 'conn_id') self.assertTrue(session.dropped.called) session.dropped.called = False ts.response_list['conn_id'] = deque([lambda *_: lambda x: x]) ts.respond_to('key_id', session, 'conn_id') self.assertFalse(session.dropped.called)
def test_retry_sending_task_result(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() subtask_id = 'xxyyzz' wtr = Mock() wtr.already_sending = True ts.results_to_send[subtask_id] = wtr ts.retry_sending_task_result(subtask_id) self.assertFalse(wtr.already_sending)
def test_conn_for_task_failure_established(self): ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, Mock(), self.client, use_docker_machine_manager=False) self.ts = ts ts.network = Mock() session = Mock() session.address = '127.0.0.1' session.port = 40102 method = ts._TaskServer__connection_for_task_failure_established method(session, 'conn_id', 'key_id', 'subtask_id', 'err_msg') self.assertEqual(session.key_id, 'key_id') self.assertIn('subtask_id', ts.task_sessions) self.assertTrue(session.send_hello.called) session.send_task_failure.assert_called_once_with('subtask_id', 'err_msg')