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 _react_to_set_task_session(self, msg): super_node_info = None if msg.super_node_info: super_node_info = Node.from_dict(msg.super_node_info) self.p2p_service.want_to_start_task_session( msg.key_id, Node.from_dict(msg.node_info), msg.conn_id, super_node_info)
def test_request(self): ccd = self._get_config_desc() ccd.min_price = 10 n = Node() ka = EllipticalKeysAuth(self.path) ts = TaskServer(n, ccd, ka, self.client, use_docker_machine_manager=False) ts.verify_header_sig = lambda x: True self.ts = ts ts.client.get_suggested_addr.return_value = "10.10.10.10" ts.client.get_suggested_conn_reverse.return_value = False ts.client.get_requesting_trust.return_value = 0.3 self.assertIsInstance(ts, TaskServer) self.assertIsNone(ts.request_task()) n2 = Node() n2.prv_addr = "10.10.10.10" n2.port = 10101 task_header = get_example_task_header() task_header["task_owner"] = n2 ts.add_task_header(task_header) self.assertEqual(ts.request_task(), "uvw") ts.remove_task_header("uvw") task_header["task_owner_port"] = 0 task_header["task_id"] = "uvw2" self.assertTrue(ts.add_task_header(task_header)) self.assertIsNotNone(ts.task_keeper.task_headers["uvw2"]) self.assertIsNone(ts.request_task()) self.assertIsNone(ts.task_keeper.task_headers.get("uvw2"))
def test_instantiation(self): task_def = self._get_core_task_definition() node = Node() # abstract class cannot be instantiated # pylint: disable=abstract-class-instantiated with self.assertRaises(TypeError): CoreTask(task_def, owner=Node(node_name="ABC")) class CoreTaskDeabstacted(CoreTask): def query_extra_data(self, *args, **kwargs): pass def short_extra_data_repr(self, extra_data): pass # ENVIRONMENT has to be set with self.assertRaises(TypeError): CoreTaskDeabstacted(task_def, node) class CoreTaskDeabstractedEnv(CoreTask): ENVIRONMENT_CLASS = MagicMock() def query_extra_data(self, *args, **kwargs): pass def short_extra_data_repr(self, extra_data): pass def query_extra_data_for_test_task(self): pass task = CoreTaskDeabstractedEnv(task_def, node) self.assertIsInstance(task, CoreTask)
def test_collect_network_info(self): """ Test configuring Node object """ node = Node() node.collect_network_info() assert is_ip_address(node.pub_addr) assert is_ip_address(node.prv_addr) for address in node.prv_addresses: assert is_ip_address(address)
def test_json(self) -> None: "Test serialization and deserialization" n = Node(node_name="Blabla", key="ABC") json_dict = n.to_dict() json_str = json.dumps(json_dict) print(json_str) deser_dict = json.loads(json_str) print(deser_dict) n_deser = Node.from_dict(deser_dict) self.assertEqual(n.__dict__, n_deser.__dict__)
def test_add_known_peer(self): key_id = EllipticalKeysAuth(self.path, "TESTPRIV", "TESTPUB").get_key_id() nominal_seeds = len(self.service.seeds) node = Node( 'super_node', key_id, pub_addr='1.2.3.4', prv_addr='1.2.3.4', pub_port=10000, prv_port=10000 ) node.prv_addresses = [node.prv_addr, '172.1.2.3'] assert Node.is_super_node(node) KnownHosts.delete().execute() len_start = len(KnownHosts.select()) # insert one self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_1 = KnownHosts.select() len_1 = len(select_1) last_conn_1 = select_1[0].last_connected assert len_1 > len_start # advance time time.sleep(0.1) # insert duplicate self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_2 = KnownHosts.select() len_2 = len(select_2) assert len_2 == len_1 assert select_2[0].last_connected > last_conn_1 assert len(self.service.seeds) > nominal_seeds # try to add more than max, we already have at least 1 pub_prefix = '2.2.3.' prv_prefix = '172.1.2.' for i in xrange(1, MAX_STORED_HOSTS + 6): i_str = str(i) pub = pub_prefix + i_str prv = prv_prefix + i_str n = Node( i_str, key_id + i_str, pub_addr=pub, prv_addr=prv, pub_port=10000, prv_port=10000 ) self.service.add_known_peer(n, pub, n.prv_port) assert len(KnownHosts.select()) == MAX_STORED_HOSTS assert len(self.service.seeds) == nominal_seeds
def test_add_known_peer(self): key_id = encode_hex(urandom(64))[2:] nominal_seeds = len(self.service.seeds) node = Node(node_name='super_node', key=str(key_id), pub_addr='1.2.3.4', prv_addr='1.2.3.4', pub_port=10000, prv_port=10000) node.prv_addresses = [node.prv_addr, '172.1.2.3'] assert Node.is_super_node(node) KnownHosts.delete().execute() len_start = len(KnownHosts.select()) # insert one self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_1 = KnownHosts.select() len_1 = len(select_1) last_conn_1 = select_1[0].last_connected assert len_1 > len_start # advance time time.sleep(0.1) # insert duplicate self.service.add_known_peer(node, node.pub_addr, node.pub_port) select_2 = KnownHosts.select() len_2 = len(select_2) assert len_2 == len_1 assert select_2[0].last_connected > last_conn_1 assert len(self.service.seeds) > nominal_seeds # try to add more than max, we already have at least 1 pub_prefix = '2.2.3.' prv_prefix = '172.1.2.' key_id_str = key_id for i in range(1, MAX_STORED_HOSTS + 6): i_str = str(i) pub = pub_prefix + i_str prv = prv_prefix + i_str n = Node(node_name=i_str, key=key_id_str + i_str, pub_addr=pub, prv_addr=prv, pub_port=10000, prv_port=10000) self.service.add_known_peer(n, pub, n.prv_port) assert len(KnownHosts.select()) == MAX_STORED_HOSTS assert len(self.service.seeds) == nominal_seeds
def test_comparison(self): k = EllipticalKeysAuth(self.path) e = urandom(20) a = EthAccountInfo(k.get_key_id(), 5111, "10.0.0.1", "test-test-test", Node(), e) b = EthAccountInfo(k.get_key_id(), 5111, "10.0.0.1", "test-test-test", Node(), e) self.assertEqual(a, b) n = Node(prv_addr="10.10.10.10", prv_port=1031, pub_addr="10.10.10.10", pub_port=1032) c = EthAccountInfo(k.get_key_id(), 5112, "10.0.0.2", "test-test2-test", n, e) self.assertEqual(a, c) k.generate_new(2) c.key_id = k.get_key_id() self.assertNotEqual(a, c)
def test_add_to_peer_keeper(self): node = Node() node.key = encode_hex(urandom(64))[2:] m_test2 = mock.MagicMock() m_test3 = mock.MagicMock() self.service.peers["TEST3"] = m_test3 self.service.peers["TEST2"] = m_test2 self.service.peer_keeper = mock.MagicMock() node2 = Node() node2.key = "TEST2" self.service.peer_keeper.add_peer = mock.MagicMock(return_value=node2) self.service.add_to_peer_keeper(node) m_test2.ping.assert_called_with(0) m_test3.ping.assert_not_called() for i in range(100): self.service.peers[str(i)] = mock.MagicMock() node2.key = "59" self.service.add_to_peer_keeper(node) self.service.peers["59"].ping.assert_called_with(0) for i in list(range(58)) + list(range(60, 100)): self.service.peers[str(i)].ping.assert_not_called() node2.key = None self.service.add_to_peer_keeper(node) for i in list(range(58)) + list(range(60, 100)): self.service.peers[str(i)].ping.assert_not_called() self.service.peers["59"].ping.assert_called_once_with(0) m_test2.ping.assert_called_once_with(0) m_test3.ping.assert_not_called() self.assertEqual(len(self.service.peers), 102)
def test_add_to_peer_keeper(self): node = Node() node.key = EllipticalKeysAuth("TEST").get_key_id() m_test2 = MagicMock() m_test3 = MagicMock() self.service.peers["TEST3"] = m_test3 self.service.peers["TEST2"] = m_test2 self.service.peer_keeper = MagicMock() node2 = Node() node2.key = "TEST2" self.service.peer_keeper.add_peer = MagicMock(return_value=node2) self.service.add_to_peer_keeper(node) m_test2.ping.assert_called_with(0) m_test3.ping.assert_not_called() for i in range(100): self.service.peers[str(i)] = MagicMock() node2.key = "59" self.service.add_to_peer_keeper(node) self.service.peers["59"].ping.assert_called_with(0) for i in range(58) + range(60, 100): self.service.peers[str(i)].ping.assert_not_called() node2.key = None self.service.add_to_peer_keeper(node) for i in range(58) + range(60, 100): self.service.peers[str(i)].ping.assert_not_called() self.service.peers["59"].ping.assert_called_once_with(0) m_test2.ping.assert_called_once_with(0) m_test3.ping.assert_not_called() self.assertEqual(len(self.service.peers), 102)
def test_get_tasks(self, _): count = 3 tm = TaskManager("ABC", Node(), Mock(), root_path=self.path) task_id, subtask_id = self.__build_tasks(tm, count) one_task = tm.get_task_dict(task_id) assert one_task assert isinstance(one_task, dict) assert len(one_task) all_tasks = tm.get_tasks_dict() assert all_tasks assert isinstance(all_tasks, list) assert len(all_tasks) == count assert all(isinstance(t, dict) for t in all_tasks) one_subtask = tm.get_subtask_dict(subtask_id) assert isinstance(one_subtask, dict) assert len(one_subtask) all_subtasks = tm.get_subtasks_dict(task_id) assert all_subtasks assert isinstance(all_subtasks, list) assert all(isinstance(t, dict) for t in all_subtasks)
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_change_config(self): ccd = self._get_config_desc() ccd.task_session_timeout = 40 ccd.min_price = 1.0 ccd.use_distributed_resource_management = 10 ccd.task_request_interval = 10 # ccd.use_waiting_ttl = True ccd.waiting_for_task_timeout = 19 ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) self.ts = ts ccd2 = self._get_config_desc() ccd2.task_session_timeout = 124 ccd2.min_price = 0.0057 ccd2.use_distributed_resource_management = 0 ccd2.task_request_interval = 31 # ccd2.use_waiting_ttl = False ccd2.waiting_for_task_timeout = 90 ts.change_config(ccd2) self.assertEqual(ts.config_desc, ccd2) self.assertEqual(ts.last_message_time_threshold, 124) self.assertEqual(ts.task_keeper.min_price, 0.0057) self.assertEqual(ts.task_manager.use_distributed_resources, False) self.assertEqual(ts.task_computer.task_request_frequency, 31) self.assertEqual(ts.task_computer.waiting_for_task_timeout, 90)
def setUp(self): super(TestRenderingTask, self).setUp() files = self.additional_dir_content([3]) task_definition = TaskDefinition() task_definition.max_price = 1000 task_definition.task_id = "xyz" task_definition.estimated_memory = 1024 task_definition.timeout = 3600 task_definition.subtask_timeout = 600 task_definition.main_scene_file = files[1] task_definition.resolution = [800, 600] task_definition.output_file = files[2] task_definition.output_format = ".png" task = RenderingTaskMock( main_program_file=files[0], task_definition=task_definition, total_tasks=100, root_path=self.path, owner=Node(node_name="ABC", pub_addr="10.10.10.10", pub_port=1023, key="keyid"), ) dm = DirManager(self.path) task.initialize(dm) self.task = task
def run_benchmark(self, benchmark, task_builder, env_id, success=None, error=None): logger.info('Running benchmark for %s', env_id) from golem.network.p2p.node import Node def success_callback(performance): logger.info('%s performance is %.2f', env_id, performance) Performance.update_or_create(env_id, performance) if success: success(performance) def error_callback(err: Union[str, Exception]): logger.error("Unable to run %s benchmark: %s", env_id, str(err)) if error: if isinstance(err, str): err = Exception(err) error(err) task_state = TaskDesc() task_state.status = TaskStatus.notStarted task_state.definition = benchmark.task_definition self._validate_task_state(task_state) builder = task_builder(Node(), task_state.definition, self.dir_manager) task = builder.build() br = BenchmarkRunner(task=task, root_path=self.dir_manager.root_path, success_callback=success_callback, error_callback=error_callback, benchmark=benchmark) br.run()
def __init__(self, src_code, task_definition, node_name, environment, resource_size=0, owner_address="", owner_port=0, owner_key_id="", max_pending_client_results=MAX_PENDING_CLIENT_RESULTS): """Create more specific task implementation """ self.task_definition = task_definition task_timeout = task_definition.full_task_timeout deadline = timeout_to_deadline(task_timeout) th = TaskHeader( node_name=node_name, task_id=task_definition.task_id, task_owner_address=owner_address, task_owner_port=owner_port, task_owner_key_id=owner_key_id, environment=environment, task_owner=Node(), deadline=deadline, subtask_timeout=task_definition.subtask_timeout, resource_size=resource_size, estimated_memory=task_definition.estimated_memory, max_price=task_definition.max_price, docker_images=task_definition.docker_images, ) Task.__init__(self, th, src_code) self.task_resources = list() self.total_tasks = 0 self.last_task = 0 self.num_tasks_received = 0 self.subtasks_given = {} self.num_failed_subtasks = 0 self.full_task_timeout = task_timeout self.counting_nodes = {} self.root_path = None self.stdout = { } # for each subtask keep info about stdout received from computing node self.stderr = { } # for each subtask keep info about stderr received from computing node self.results = { } # for each subtask keep info about files containing results self.res_files = {} self.tmp_dir = None self.verificator = self.VERIFICATOR_CLASS() self.max_pending_client_results = max_pending_client_results
def _get_blender_task(task_definition, total_tasks=6): return BlenderRenderTask( owner=Node(node_name="exmaple-node-name"), task_definition=task_definition, total_tasks=total_tasks, root_path=self.tempdir, )
def test_header_serialization(self): node = Node(node_name="test node", pub_addr="10.10.10.10", pub_port=1023) task_header = TaskHeader( task_id="xyz", environment="DEFAULT", task_owner=node) # ignore dynamic properties task_header.fixed_header.last_checking = 0 task_header_dict = task_header.to_dict() serialized = CBORSerializer.dumps(task_header_dict) deserialized = CBORSerializer.loads(serialized) task_header_from_dict = TaskHeader.from_dict(deserialized) # ignore dynamic properties task_header_from_dict.fixed_header.last_checking = 0 assert task_header_from_dict.to_dict() == task_header_dict assert isinstance(task_header_from_dict.task_owner, Node) task_header_bin = task_header.to_binary() bin_serialized = CBORSerializer.dumps(task_header_bin) bin_deserialized = CBORSerializer.loads(bin_serialized) assert bin_deserialized == task_header_bin
def test_results_no_payment_addr(self, trust, mock_addr, dump_mock): mock_addr.return_value = ("10.10.10.10", 1111, "Full NAT") # FIXME: This test is too heavy, it starts up whole Golem Client. ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) ts.task_manager.listen_address = "10.10.10.10" ts.task_manager.listen_port = 1111 ts.receive_subtask_computation_time("xxyyzz", 1031) self.ts = ts extra_data = Mock() extra_data.ctd = ComputeTaskDef() extra_data.ctd.task_id = "xyz" extra_data.ctd.subtask_id = "xxyyzz" extra_data.ctd.environment = "DEFAULT" extra_data.should_wait = False task_mock = get_mock_task("xyz", "xxyyzz") task_mock.query_extra_data.return_value = extra_data sync_wait(ts.task_manager.add_new_task(task_mock)) ts.task_manager.tasks_states["xyz"].status = ts.task_manager.activeStatus[0] subtask, wrong_task, wait = ts.task_manager.get_next_subtask( "DEF", "DEF", "xyz", 1000, 10, 5, 10, 2, "10.10.10.10") ts.receive_subtask_computation_time("xxyyzz", 1031) account_info = Mock() account_info.key_id = "key" account_info.eth_account = Mock() account_info.eth_account.address = None ts.accept_result("xxyyzz", account_info) self.assertEqual(ts.client.transaction_system.add_payment_info.call_count, 0)
def test_results(self, trust, mock_addr, dump_mock): mock_addr.return_value = ("10.10.10.10", 1111, "Full NAT") ccd = self._get_config_desc() ts = TaskServer(Node(), ccd, EllipticalKeysAuth(self.path), self.client, use_docker_machine_manager=False) self.ts = ts ts.task_manager.listen_port = 1111 ts.task_manager.listen_address = "10.10.10.10" ts.receive_subtask_computation_time("xxyyzz", 1031) extra_data = Mock() extra_data.ctd = ComputeTaskDef() extra_data.ctd.task_id = "xyz" extra_data.ctd.subtask_id = "xxyyzz" extra_data.ctd.environment = "DEFAULT" extra_data.should_wait = False task_mock = get_mock_task("xyz", "xxyyzz") task_mock.query_extra_data.return_value = extra_data sync_wait(ts.task_manager.add_new_task(task_mock)) ts.task_manager.tasks_states["xyz"].status = ts.task_manager.activeStatus[0] subtask, wrong_task, wait = ts.task_manager.get_next_subtask("DEF", "DEF", "xyz", 1000, 10, 5, 10, 2, "10.10.10.10") ts.receive_subtask_computation_time("xxyyzz", 1031) self.assertEqual(ts.task_manager.tasks_states["xyz"].subtask_states["xxyyzz"].computation_time, 1031) expected_value = ceil(1031 * 10 / 3600) self.assertEqual(ts.task_manager.tasks_states["xyz"].subtask_states["xxyyzz"].value, expected_value) account_info = Mock() account_info.key_id = "key" prev_calls = trust.COMPUTED.increase.call_count ts.accept_result("xxyyzz", account_info) ts.client.transaction_system.add_payment_info.assert_called_with("xyz", "xxyyzz", expected_value, account_info) self.assertGreater(trust.COMPUTED.increase.call_count, prev_calls)
def test_send_report_computed_task(self): ts = TaskSession(Mock()) ts.verified = True ts.task_server.get_node_name.return_value = "ABC" n = Node() wtr = WaitingTaskResult("xyz", "xxyyzz", "result", result_types["data"], 13190, 10, 0, "10.10.10.10", 30102, "key1", n) ts.send_report_computed_task(wtr, "10.10.10.10", 30102, "0x00", n) ms = ts.conn.send_message.call_args[0][0] self.assertIsInstance(ms, MessageReportComputedTask) self.assertEqual(ms.subtask_id, "xxyyzz") self.assertEqual(ms.result_type, 0) self.assertEqual(ms.computation_time, 13190) self.assertEqual(ms.node_name, "ABC") self.assertEqual(ms.address, "10.10.10.10") self.assertEqual(ms.port, 30102) self.assertEqual(ms.eth_account, "0x00") self.assertEqual(ms.extra_data, []) self.assertEqual(ms.node_info, n) ts2 = TaskSession(Mock()) ts2.verified = True ts2.key_id = "DEF" ts2.can_be_not_encrypted.append(ms.TYPE) ts2.can_be_unsigned.append(ms.TYPE) ts2.task_manager.subtask2task_mapping = {"xxyyzz": "xyz"} ts2.interpret(ms) ts2.task_server.receive_subtask_computation_time.assert_called_with( "xxyyzz", 13190) wtr.result_type = "UNKNOWN" with self.assertLogs(logger, level="ERROR"): ts.send_report_computed_task(wtr, "10.10.10.10", 30102, "0x00", n)
def test_run(self): dm = DockerTaskThread.docker_manager = DockerManager.install() dm.update_config(status_callback=mock.Mock(), done_callback=mock.Mock(), work_dir=self.new_path, in_background=True) benchmark = BlenderBenchmark() task_definition = benchmark.task_definition task_state = TaskDesc() task_state.status = TaskStatus.notStarted task_state.definition = task_definition dir_manager = DirManager(self.path) task = blenderrendertask.BlenderRenderTaskBuilder( Node(), task_definition, dir_manager).build() success = mock.MagicMock() error = mock.MagicMock() self.br = BenchmarkRunner(task, self.path, success, error, benchmark) self.br.run() if self.br.tt: self.br.tt.join() self.assertEqual(success.call_count, 1)
def _get_core_task(self): from golem.network.p2p.node import Node task_def = self._get_core_task_definition() class CoreTaskDeabstacted(CoreTask): ENVIRONMENT_CLASS = mock.MagicMock() def query_extra_data(self, perf_index, num_cores=0, node_id=None, node_name=None): pass def short_extra_data_repr(self, extra_data): pass def query_extra_data_for_test_task(self): pass task = CoreTaskDeabstacted( owner=Node( node_name="ABC", pub_addr="10.10.10.10", pub_port=123, key="key", ), task_definition=task_def, resource_size=1024 ) dm = DirManager(self.path) task.initialize(dm) return task
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_str(self): n = Node(node_name="Blabla", key="ABC") self.assertNotIn("at", str(n)) self.assertNotIn("at", "{}".format(n)) self.assertIn("Blabla", str(n)) self.assertIn("Blabla", "{}".format(n)) self.assertIn("ABC", str(n)) self.assertIn("ABC", "{}".format(n))
def test_update_public_info_invalid(self): node = Node( node_name="Node 1", key="key_1" ) assert node.pub_addr is None assert node.pub_port is None assert node.p2p_pub_port is None assert node.hyperdrive_pub_port is None node.update_public_info() assert node.pub_addr is None assert node.pub_port is None assert node.p2p_pub_port is None assert node.hyperdrive_pub_port is None
def test_get_subtasks_borders(self, _): count = 3 tm = TaskManager("ABC", Node(), Mock(), root_path=self.path) task_id, _ = self.__build_tasks(tm, count) borders = tm.get_subtasks_borders(task_id) assert len(borders) == count assert all(len(b) == 4 for b in borders.values())
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)