def test_termination(self):
        script = "import time\ntime.sleep(20)"

        task_server = Mock()
        task_server.config_desc = ClientConfigDescriptor()
        task_server.client.datadir = self.test_dir
        task_server.benchmark_manager = Mock()
        task_server.benchmark_manager.benchmarks_needed.return_value = False
        task_server.client.get_node_name.return_value = "test_node"
        task_server.get_task_computer_root.return_value = \
            task_server.client.datadir
        task_computer = TaskComputer(task_server,
                                     use_docker_manager=False)
        image = DockerImage("golemfactory/base", tag="1.2")

        with self.assertRaises(AttributeError):
            dir_mapping = DockerTaskThread.generate_dir_mapping(
                self.resources_dir, self.output_dir)
            DockerTaskThread("subtask_id", None,
                             script, None,
                             dir_mapping, timeout=30)

        def test():
            dir_mapping = DockerTaskThread.generate_dir_mapping(
                self.resources_dir, self.output_dir)
            tt = DockerTaskThread("subtask_id", [image],
                                  script, None,
                                  "test task thread", dir_mapping, timeout=30)
            task_computer.counting_thread = tt
            task_computer.counting_task = True
            tt.setDaemon(True)
            tt.start()
            time.sleep(1)

        started = time.time()
        parent_thread = Thread(target=test)
        parent_thread.start()
        time.sleep(1)

        ct = task_computer.counting_thread

        while ct and ct.is_alive():
            task_computer.run()

            if time.time() - started > 15:
                self.fail("Job timed out")
            else:
                ct = task_computer.counting_thread

            time.sleep(1)
Exemple #2
0
    def test_thread(self):
        ts = mock.MagicMock()
        ts.config_desc = ClientConfigDescriptor()
        ts.benchmark_manager.benchmarks_needed.return_value = False
        ts.get_task_computer_root.return_value = self.new_path

        tc = TaskComputer(ts, use_docker_manager=False)
        tc.counting_task = True
        tc.waiting_for_task = None

        tt = self._new_task_thread(tc)
        sync_wait(tt.start())

        self.assertGreater(tt.end_time - tt.start_time, 0)
        self.assertLess(tt.end_time - tt.start_time, 20)
Exemple #3
0
 def test_thread(self):
     files_ = self.additional_dir_content([0, [1], [1], [1], [1]])
     ts = mock.MagicMock()
     ts.config_desc = config_desc()
     tc = TaskComputer("ABC", ts, use_docker_machine_manager=False)
     tc.counting_task = True
     tc.waiting_for_task = None
     tt = PyTaskThread(
         tc, "xxyyzz", self.path,
         "cnt=0\nfor i in range(1000000):\n\tcnt += 1\noutput=cnt", {},
         "hello thread", os.path.dirname(files_[0]),
         os.path.dirname(files_[1]), 20)
     tt.run()
     self.assertGreater(tt.end_time - tt.start_time, 0)
     self.assertLess(tt.end_time - tt.start_time, 20)
     self.assertTrue(tc.counting_task)
Exemple #4
0
    def test_directories(self, *_):
        c = self.client

        self.assertIsInstance(c.get_datadir(), unicode)
        self.assertIsInstance(c.get_dir_manager(), Mock)

        c.task_server = TaskServer.__new__(TaskServer)
        c.task_server.client = self.client
        c.task_server.task_manager = TaskManager.__new__(TaskManager)
        c.task_server.task_manager.root_path = self.path
        c.task_server.task_computer = TaskComputer.__new__(TaskComputer)
        c.task_server.task_computer.dir_manager = DirManager(self.tempdir)
        c.task_server.task_computer.current_computations = []

        c.resource_server = ResourceServer.__new__(ResourceServer)
        c.resource_server.dir_manager = c.task_server.task_computer.dir_manager

        self.assertIsInstance(c.get_dir_manager(), DirManager)

        res_dirs = c.get_res_dirs()

        self.assertIsInstance(res_dirs, dict)
        self.assertTrue(len(res_dirs) == 3)

        for key, value in res_dirs.iteritems():
            self.assertIsInstance(key, unicode)
            self.assertIsInstance(value, unicode)
            self.assertTrue(self.path in value)

        res_dir_sizes = c.get_res_dirs_sizes()

        for key, value in res_dir_sizes.iteritems():
            self.assertIsInstance(key, unicode)
            self.assertIsInstance(value, unicode)
            self.assertTrue(key in res_dirs)
Exemple #5
0
    def test_task_computed(self):
        """golem.monitor signal"""
        from golem.monitor.model.nodemetadatamodel import NodeMetadataModel
        from golem.monitor.monitor import SystemMonitor
        from golem.monitorconfig import MONITOR_CONFIG
        monitor = SystemMonitor(
            NodeMetadataModel("CLIID", "SESSID", "hackix", "3.1337", "Descr",
                              config_desc()), MONITOR_CONFIG)
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()
        task = TaskComputer("ABC",
                            task_server,
                            use_docker_machine_manager=False)

        task_thread = mock.MagicMock()
        task_thread.start_time = time.time()
        duration = random.randint(1, 100)
        task_thread.end_time = task_thread.start_time + duration

        def prepare():
            subtask = mock.MagicMock()
            subtask_id = random.randint(3000, 4000)
            task.assigned_subtasks[subtask_id] = subtask
            task_thread.subtask_id = subtask_id

        def check(expected):
            with mock.patch(
                    'golem.monitor.monitor.SenderThread.send') as mock_send:
                task.task_computed(task_thread)
                self.assertEquals(mock_send.call_count, 1)
                result = mock_send.call_args[0][0].dict_repr()
                for key in ('cliid', 'sessid', 'timestamp'):
                    del result[key]
                expected_d = {
                    'type': 'ComputationTime',
                    'success': expected,
                    'value': duration,
                }
                self.assertEquals(expected_d, result)

        # error case
        prepare()
        task_thread.error = True
        check(False)

        # success case
        prepare()
        task_thread.error = False
        task_thread.error_msg = None
        task_thread.result = {
            'data': 'oh senora!!!',
            'result_type': 'Cadencia da Vila'
        }
        check(True)

        # default case (error)
        prepare()
        task_thread.result = None
        check(False)
Exemple #6
0
    def test_resource_failure(self):
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()
        tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)

        task_id = 'xyz'
        subtask_id = 'xxyyzz'

        tc.task_resource_failure(task_id, 'reason')
        assert not task_server.send_task_failed.called

        tc.task_to_subtask_mapping[task_id] = subtask_id
        tc.assigned_subtasks[subtask_id] = mock.Mock()

        tc.task_resource_failure(task_id, 'reason')
        assert task_server.send_task_failed.called

        tc.resource_request_rejected(subtask_id, 'reason')
Exemple #7
0
    def test_resource_failure(self):
        task_server = self.task_server

        tc = TaskComputer(task_server, use_docker_manager=False)

        task_id = 'xyz'
        subtask_id = 'xxyyzz'

        tc.task_resource_failure(task_id, 'reason')
        assert not task_server.send_task_failed.called

        tc.assigned_subtask = ComputeTaskDef(
            task_id=task_id,
            subtask_id=subtask_id,
        )

        tc.task_resource_failure(task_id, 'reason')
        assert task_server.send_task_failed.called
Exemple #8
0
    def test_computation(self):
        p2p_node = P2PNode()
        ctd = ComputeTaskDef()
        ctd['task_id'] = "xyz"
        ctd['subtask_id'] = "xxyyzz"
        ctd['src_code'] = \
            "cnt=0\n" \
            "for i in range(10000):\n" \
            "\tcnt += 1\n" \
            "output={'data': cnt, 'result_type': 0}"
        ctd['extra_data'] = {}
        ctd['short_description'] = "add cnt"
        ctd['deadline'] = timeout_to_deadline(10)

        task_server = self.task_server
        task_server.task_keeper.task_headers = {
            ctd['subtask_id']:
            mock.Mock(subtask_timeout=5, deadline=timeout_to_deadline(5)),
            ctd['task_id']:
            mock.Mock(subtask_timeout=5, deadline=timeout_to_deadline(20))
        }

        mock_finished = mock.Mock()
        tc = TaskComputer(task_server,
                          use_docker_manager=False,
                          finished_cb=mock_finished)

        self.assertEqual(tc.assigned_subtask, None)
        tc.task_given(ctd)
        self.assertEqual(tc.assigned_subtask, ctd)
        self.assertLessEqual(tc.assigned_subtask['deadline'],
                             timeout_to_deadline(10))
        tc.task_server.request_resource.assert_called_with("xyz", "xxyyzz")

        assert tc.task_resource_collected("xyz")
        tc.task_server.unpack_delta.assert_called_with(
            tc.dir_manager.get_task_resource_dir("xyz"), None, "xyz")
        assert tc.counting_thread is None
        assert tc.assigned_subtask is None
        task_server.send_task_failed.assert_called_with(
            "xxyyzz", "xyz", "Host direct task not supported")

        tc.support_direct_computation = True
        tc.task_given(ctd)
        assert tc.task_resource_collected("xyz")
        assert not tc.waiting_for_task
        assert tc.counting_thread is not None
        self.assertGreater(tc.counting_thread.time_to_compute, 9)
        self.assertLessEqual(tc.counting_thread.time_to_compute, 10)
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()
        self.__wait_for_tasks(tc)

        prev_task_failed_count = task_server.send_task_failed.call_count
        self.assertIsNone(tc.counting_task)
        self.assertIsNone(tc.counting_thread)
        self.assertIsNone(tc.assigned_subtask)
        assert task_server.send_task_failed.call_count == prev_task_failed_count
        self.assertTrue(task_server.send_results.called)
        args = task_server.send_results.call_args[0]
        self.assertEqual(args[0], "xxyyzz")
        self.assertEqual(args[1], "xyz")
        self.assertEqual(args[2]["data"], 10000)
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()

        ctd['subtask_id'] = "aabbcc"
        ctd['src_code'] = "raise Exception('some exception')"
        ctd['deadline'] = timeout_to_deadline(5)
        tc.task_given(ctd)
        self.assertEqual(tc.assigned_subtask, ctd)
        self.assertLessEqual(tc.assigned_subtask['deadline'],
                             timeout_to_deadline(5))
        tc.task_server.request_resource.assert_called_with("xyz", "aabbcc")
        self.assertTrue(tc.task_resource_collected("xyz"))
        self.__wait_for_tasks(tc)

        self.assertIsNone(tc.counting_task)
        self.assertIsNone(tc.counting_thread)
        self.assertIsNone(tc.assigned_subtask)
        task_server.send_task_failed.assert_called_with(
            "aabbcc", "xyz", 'some exception')
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()

        ctd['subtask_id'] = "aabbcc2"
        ctd['src_code'] = "print('Hello world')"
        ctd['deadline'] = timeout_to_deadline(5)
        tc.task_given(ctd)
        self.assertTrue(tc.task_resource_collected("xyz"))
        self.__wait_for_tasks(tc)

        task_server.send_task_failed.assert_called_with(
            "aabbcc2", "xyz", "Wrong result format")
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()

        task_server.task_keeper.task_headers["xyz"].deadline = \
            timeout_to_deadline(20)
        ctd['subtask_id'] = "aabbcc3"
        ctd['src_code'] = "output={'data': 0, 'result_type': 0}"
        ctd['deadline'] = timeout_to_deadline(40)
        tc.task_given(ctd)
        self.assertTrue(tc.task_resource_collected("xyz"))
        self.assertIsNotNone(tc.counting_thread)
        self.assertGreater(tc.counting_thread.time_to_compute, 10)
        self.assertLessEqual(tc.counting_thread.time_to_compute, 20)
        self.__wait_for_tasks(tc)

        ctd['subtask_id'] = "xxyyzz2"
        ctd['deadline'] = timeout_to_deadline(1)
        tc.task_given(ctd)
        self.assertTrue(tc.task_resource_collected("xyz"))
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()
        tt = tc.counting_thread
        tc.task_computed(tc.counting_thread)
        self.assertIsNone(tc.counting_thread)
        mock_finished.assert_called_once_with()
        mock_finished.reset_mock()
        task_server.send_task_failed.assert_called_with(
            "xxyyzz2", "xyz", "Wrong result format")
        tt.end_comp()
        time.sleep(0.5)
        if tt.is_alive():
            tt.join(timeout=5)
Exemple #9
0
    def test_run(self):
        task_server = self.task_server
        task_server.config_desc.task_request_interval = 0.5
        task_server.config_desc.accept_tasks = True
        task_server.get_task_computer_root.return_value = self.path
        tc = TaskComputer(task_server, use_docker_manager=False)
        self.assertIsNone(tc.counting_task)
        self.assertIsNone(tc.counting_thread)
        self.assertIsNone(tc.waiting_for_task)
        tc.last_task_request = 0
        tc.run()
        task_server.request_task.assert_called_with()
        task_server.request_task = mock.MagicMock()
        task_server.config_desc.accept_tasks = False
        tc2 = TaskComputer(task_server, use_docker_manager=False)
        tc2.counting_task = None
        tc2.counting_thread = None
        tc2.waiting_for_task = None
        tc2.last_task_request = 0

        tc2.run()
        task_server.request_task.assert_not_called()

        tc2.runnable = True
        tc2.compute_tasks = True
        tc2.waiting_for_task = False
        tc2.counting_task = None

        tc2.last_task_request = 0
        tc2.counting_thread = None

        tc2.run()

        assert task_server.request_task.called

        task_server.request_task.called = False

        tc2.waiting_for_task = 'xxyyzz'
        tc2.use_waiting_ttl = True
        tc2.last_checking = 10**10

        tc2.run()
        tc2.session_timeout()
Exemple #10
0
    def test_task_computed(self):
        """golem.monitor signal"""
        from golem.monitor.model.nodemetadatamodel import NodeMetadataModel
        from golem.monitor.monitor import SystemMonitor
        from golem.monitorconfig import MONITOR_CONFIG
        #  hold reference to avoid GC of monitor
        client_mock = mock.MagicMock()
        client_mock.cliid = 'CLIID'
        client_mock.sessid = 'SESSID'
        client_mock.config_desc = ClientConfigDescriptor()
        os_info = OSInfo('linux', 'Linux', '1', '1.2.3')
        monitor = SystemMonitor(  # noqa pylint: disable=unused-variable
            NodeMetadataModel(client_mock, os_info, "3.1337"), MONITOR_CONFIG)
        task_server = mock.MagicMock()
        task_server.config_desc = ClientConfigDescriptor()
        task_server.benchmark_manager.benchmarks_needed.return_value = False
        task_server.get_task_computer_root.return_value = self.new_path

        task = TaskComputer(task_server, use_docker_manager=False)

        task_thread = mock.MagicMock()
        task_thread.start_time = time.time()
        duration = random.randint(1, 100)
        task_thread.end_time = task_thread.start_time + duration

        def prepare():
            subtask = mock.MagicMock()
            subtask_id = random.randint(3000, 4000)
            subtask['subtask_id'] = subtask_id
            task_server\
                .task_keeper.task_headers[subtask_id].subtask_timeout = duration

            task.assigned_subtask = subtask
            task_thread.subtask_id = subtask_id

        def check(expected):
            with mock.patch('golem.monitor.monitor.SenderThread.send') \
                    as mock_send:
                task.task_computed(task_thread)
                self.assertEqual(mock_send.call_count, 1)
                result = mock_send.call_args[0][0].dict_repr()
                for key in ('cliid', 'sessid', 'timestamp'):
                    del result[key]
                expected_d = {
                    'type': 'ComputationTime',
                    'success': expected,
                    'value': duration,
                }
                self.assertEqual(expected_d, result)

        # error case
        prepare()
        task_thread.error = True
        check(False)

        # success case
        prepare()
        task_thread.error = False
        task_thread.error_msg = None
        task_thread.result = {'data': 'oh senora!!!'}
        check(True)

        # default case (error)
        prepare()
        task_thread.result = None
        check(False)
Exemple #11
0
 def test_init(self):
     task_server = self.task_server
     tc = TaskComputer(task_server, use_docker_manager=False)
     self.assertIsInstance(tc, TaskComputer)
Exemple #12
0
    def test_event_listeners(self):
        client = mock.Mock()
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()
        tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)

        tc.lock_config(True)
        tc.lock_config(False)

        listener = ClientTaskComputerEventListener(client)
        tc.register_listener(listener)

        tc.lock_config(True)
        client.lock_config.assert_called_with(True)

        tc.lock_config(False)
        client.lock_config.assert_called_with(False)
Exemple #13
0
    def test_event_listeners(self):
        client = mock.Mock()
        task_server = self.task_server

        tc = TaskComputer(task_server, use_docker_manager=False)

        tc.lock_config(True)
        tc.lock_config(False)

        listener = ClientTaskComputerEventListener(client)
        tc.register_listener(listener)

        tc.lock_config(True)
        client.lock_config.assert_called_with(True)

        tc.lock_config(False)
        client.lock_config.assert_called_with(False)
Exemple #14
0
 def test_request_rejected(self):
     task_server = self.task_server
     tc = TaskComputer(task_server, use_docker_manager=False)
     with self.assertLogs(logger, level="INFO"):
         tc.task_request_rejected("xyz", "my rejection reason")
Exemple #15
0
    def test_change_config(self):
        task_server = self.task_server

        tc = TaskComputer(task_server, use_docker_manager=False)
        tc.docker_manager = mock.Mock(spec=DockerManager, hypervisor=None)

        tc.use_docker_manager = False
        tc.change_config(mock.Mock(), in_background=False)
        assert not tc.docker_manager.update_config.called

        tc.use_docker_manager = True

        def _update_config(status_callback, *_, **__):
            status_callback()

        tc.docker_manager.update_config = _update_config

        tc.counting_task = True
        tc.change_config(mock.Mock(), in_background=False)

        # pylint: disable=unused-argument
        def _update_config_2(status_callback, done_callback, *_, **__):
            done_callback(False)

        tc.docker_manager.update_config = _update_config_2

        tc.counting_task = None
        tc.change_config(mock.Mock(), in_background=False)
Exemple #16
0
    def test_change_config(self):
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()

        tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)
        tc.docker_manager = mock.Mock()

        tc.use_docker_machine_manager = False
        tc.change_config(mock.Mock(), in_background=False)
        assert not tc.docker_manager.update_config.called

        tc.use_docker_machine_manager = True
        tc.docker_manager.update_config = lambda x, y, z: x()

        tc.counting_task = True
        tc.change_config(mock.Mock(), in_background=False)

        tc.docker_manager.update_config = lambda x, y, z: y()

        tc.counting_task = False
        tc.change_config(mock.Mock(), in_background=False)
Exemple #17
0
    def test_run(self):
        task_server = mock.MagicMock()
        task_server.config_desc = config_desc()
        task_server.config_desc.task_request_interval = 0.5
        task_server.config_desc.use_waiting_for_task_timeout = True
        task_server.config_desc.waiting_for_task_timeout = 1
        task_server.config_desc.accept_tasks = True
        task_server.get_task_computer_root.return_value = self.path
        tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)
        self.assertFalse(tc.counting_task)
        self.assertEqual(len(tc.current_computations), 0)
        self.assertIsNone(tc.waiting_for_task)
        tc.last_task_request = 0
        tc.run()
        task_server.request_task.assert_called_with()
        task_server.request_task = mock.MagicMock()
        task_server.config_desc.accept_tasks = False
        tc2 = TaskComputer("DEF",
                           task_server,
                           use_docker_machine_manager=False)
        tc2.counting_task = False
        tc2.current_computations = []
        tc2.waiting_for_task = None
        tc2.last_task_request = 0

        tc2.run()
        task_server.request_task.assert_not_called()

        tc2.runnable = True
        tc2.compute_tasks = True
        tc2.waiting_for_task = False
        tc2.counting_task = False

        tc2.last_task_request = 0
        tc2.current_computations = []

        tc2.run()

        assert task_server.request_task.called

        task_server.request_task.called = False

        tc2.waiting_for_task = 'xxyyzz'
        tc2.use_waiting_ttl = True
        tc2.last_checking = 10**10

        tc2.run()
        tc2.session_timeout()
Exemple #18
0
    def test_publish_events(self, send, log, *_):
        self.client = Client(datadir=self.path,
                             transaction_system=False,
                             connect_to_known_hosts=False,
                             use_docker_machine_manager=False,
                             use_monitor=False)
        c = self.client

        def get_balance(*_):
            d = Deferred()
            d.callback((1, 2, 3))
            return d

        c.task_server = Mock()
        c.task_server.task_computer = TaskComputer.__new__(TaskComputer)
        c.task_server.task_computer.stats = dict()

        c.get_balance = get_balance
        c.get_task_count = lambda *_: 0
        c.get_supported_task_count = lambda *_: 0
        c.connection_status = lambda *_: 'test'

        c.config_desc.node_snapshot_interval = 1
        c.config_desc.network_check_interval = 1

        c._publish = Mock()

        past_time = time.time() - 10**10
        future_time = time.time() + 10**10

        c.last_nss_time = future_time
        c.last_net_check_time = future_time
        c.last_balance_time = future_time
        c.last_tasks_time = future_time

        c._Client__publish_events()

        assert not send.called
        assert not log.debug.called
        assert not c._publish.called

        c.last_nss_time = past_time
        c.last_net_check_time = past_time
        c.last_balance_time = past_time
        c.last_tasks_time = past_time

        c._Client__publish_events()

        assert not log.debug.called
        assert send.call_count == 2
        assert c._publish.call_count == 3

        def raise_exc(*_):
            raise Exception('Test exception')

        c.get_balance = raise_exc
        c._publish = Mock()
        send.call_count = 0

        c.last_nss_time = past_time
        c.last_net_check_time = past_time
        c.last_balance_time = past_time
        c.last_tasks_time = past_time

        c._Client__publish_events()

        assert log.debug.called
        assert send.call_count == 2
        assert c._publish.call_count == 2
Exemple #19
0
    def test_computation(self):
        task_server = mock.MagicMock()
        task_server.get_task_computer_root.return_value = self.path
        task_server.config_desc = config_desc()
        tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)

        ctd = ComputeTaskDef()
        ctd.task_id = "xyz"
        ctd.subtask_id = "xxyyzz"
        ctd.return_address = "10.10.10.10"
        ctd.return_port = 10203
        ctd.key_id = "key"
        ctd.task_owner = "owner"
        ctd.src_code = "cnt=0\nfor i in range(10000):\n\tcnt += 1\noutput={'data': cnt, 'result_type': 0}"
        ctd.extra_data = {}
        ctd.short_description = "add cnt"
        ctd.deadline = timeout_to_deadline(10)
        self.assertEqual(len(tc.assigned_subtasks), 0)
        tc.task_given(ctd)
        self.assertEqual(tc.assigned_subtasks["xxyyzz"], ctd)
        self.assertLessEqual(tc.assigned_subtasks["xxyyzz"].deadline,
                             timeout_to_deadline(10))
        self.assertEqual(tc.task_to_subtask_mapping["xyz"], "xxyyzz")
        tc.task_server.request_resource.assert_called_with(
            "xyz", tc.resource_manager.get_resource_header("xyz"),
            "10.10.10.10", 10203, "key", "owner")
        assert tc.task_resource_collected("xyz")
        tc.task_server.unpack_delta.assert_called_with(
            tc.dir_manager.get_task_resource_dir("xyz"), None, "xyz")
        assert len(tc.current_computations) == 0
        assert tc.assigned_subtasks.get("xxyyzz") is None
        task_server.send_task_failed.assert_called_with(
            "xxyyzz", "xyz", "Host direct task not supported", "10.10.10.10",
            10203, "key", "owner", "ABC")

        tc.support_direct_computation = True
        tc.task_given(ctd)
        assert tc.task_resource_collected("xyz")
        assert not tc.waiting_for_task
        assert len(tc.current_computations) == 1
        self.__wait_for_tasks(tc)

        prev_task_failed_count = task_server.send_task_failed.call_count
        self.assertFalse(tc.counting_task)
        self.assertEqual(len(tc.current_computations), 0)
        self.assertIsNone(tc.assigned_subtasks.get("xxyyzz"))
        assert task_server.send_task_failed.call_count == prev_task_failed_count
        self.assertTrue(task_server.send_results.called)
        args = task_server.send_results.call_args[0]
        self.assertEqual(args[0], "xxyyzz")
        self.assertEqual(args[1], "xyz")
        self.assertEqual(args[2]["data"], 10000)
        self.assertGreater(args[3], 0)
        self.assertLess(args[3], 10)
        self.assertEqual(args[4], "10.10.10.10")
        self.assertEqual(args[5], 10203)
        self.assertEqual(args[6], "key")
        self.assertEqual(args[7], "owner")
        self.assertEqual(args[8], "ABC")

        ctd.subtask_id = "aabbcc"
        ctd.src_code = "raise Exception('some exception')"
        ctd.deadline = timeout_to_deadline(5)
        tc.task_given(ctd)
        self.assertEqual(tc.assigned_subtasks["aabbcc"], ctd)
        self.assertLessEqual(tc.assigned_subtasks["aabbcc"].deadline,
                             timeout_to_deadline(5))
        self.assertEqual(tc.task_to_subtask_mapping["xyz"], "aabbcc")
        tc.task_server.request_resource.assert_called_with(
            "xyz", tc.resource_manager.get_resource_header("xyz"),
            "10.10.10.10", 10203, "key", "owner")
        self.assertTrue(tc.task_resource_collected("xyz"))
        self.__wait_for_tasks(tc)

        self.assertFalse(tc.counting_task)
        self.assertEqual(len(tc.current_computations), 0)
        self.assertIsNone(tc.assigned_subtasks.get("aabbcc"))
        task_server.send_task_failed.assert_called_with(
            "aabbcc", "xyz", 'some exception', "10.10.10.10", 10203, "key",
            "owner", "ABC")

        ctd.subtask_id = "aabbcc2"
        ctd.src_code = "print 'Hello world'"
        ctd.timeout = timeout_to_deadline(5)
        tc.task_given(ctd)
        self.assertTrue(tc.task_resource_collected("xyz"))
        self.__wait_for_tasks(tc)

        task_server.send_task_failed.assert_called_with(
            "aabbcc2", "xyz", "Wrong result format", "10.10.10.10", 10203,
            "key", "owner", "ABC")

        ctd.subtask_id = "xxyyzz2"
        ctd.timeout = timeout_to_deadline(1)
        tc.task_given(ctd)
        self.assertTrue(tc.task_resource_collected("xyz"))
        tt = tc.current_computations[0]
        tc.task_computed(tc.current_computations[0])
        self.assertEqual(len(tc.current_computations), 0)
        task_server.send_task_failed.assert_called_with(
            "xxyyzz2", "xyz", "Wrong result format", "10.10.10.10", 10203,
            "key", "owner", "ABC")
        tt.end_comp()
        time.sleep(0.5)
        if tt.is_alive():
            tt.join(timeout=5)
    def test_termination(self):
        script = "import time\ntime.sleep(20)"

        task_server = Mock()
        task_server.config_desc = ClientConfigDescriptor()
        task_server.config_desc.estimated_blender_performance = 2000.0
        task_server.config_desc.estimated_lux_performance = 2000.0
        task_server.client.datadir = self.test_dir
        task_server.client.get_node_name.return_value = "test_node"
        task_server.get_task_computer_root.return_value = task_server.client.datadir
        task_computer = TaskComputer("node",
                                     task_server,
                                     use_docker_machine_manager=False)
        image = DockerImage("golemfactory/base", tag="1.2")

        with self.assertRaises(AttributeError):
            DockerTaskThread(task_computer,
                             "subtask_id",
                             None,
                             self.work_dir,
                             script,
                             None,
                             "test task thread",
                             self.resources_dir,
                             self.output_dir,
                             timeout=30)

        def test():
            tt = DockerTaskThread(task_computer,
                                  "subtask_id", [image],
                                  self.work_dir,
                                  script,
                                  None,
                                  "test task thread",
                                  self.resources_dir,
                                  self.output_dir,
                                  timeout=30)
            task_computer.current_computations.append(tt)
            task_computer.counting_task = True
            tt.setDaemon(True)
            tt.start()
            time.sleep(1)

        started = time.time()
        parent_thread = Thread(target=test)
        parent_thread.start()
        time.sleep(1)

        ct = task_computer.current_computations[0]

        while ct and ct.is_alive():
            task_computer.run()

            if time.time() - started > 15:
                self.fail("Job timed out")
            elif task_computer.current_computations:
                ct = task_computer.current_computations[0]
            else:
                ct = None

            time.sleep(1)
Exemple #21
0
 def test_init(self):
     task_server = mock.MagicMock()
     task_server.get_task_computer_root.return_value = self.path
     task_server.config_desc = config_desc()
     tc = TaskComputer("ABC", task_server, use_docker_machine_manager=False)
     self.assertIsInstance(tc, TaskComputer)