def test_queue_handler_can_spawn_and_start_properly(self): """ This function asserts the `queue_handler.spawn_and_start()` can be executed properly. """ test_handler = queue_handler.QueueHandler(self.config_path) try: test_handler.spawn_and_start() mock_queue_handler_execution_loop.assert_called_once_with( test_handler) finally: test_handler.thread.join()
def test_sleep_for_can_pause_for_at_least_given_duration(self): """ This function asserts the `queue_handler.sleep_for()` pauses the thread for at least a given duration. """ test_handler = queue_handler.QueueHandler(self.config_path) test_sleep_time = 1 start = timeit.default_timer() test_handler.sleep_for(test_sleep_time) stop = timeit.default_timer() elapsed = stop - start assert test_sleep_time <= elapsed <= test_sleep_time * 1.5
def test_prepare_workflows_returns_a_workflow_iterator_correctly(self): """ This function asserts the `queue_handler.prepare_workflows()` returns an expected iterator of the list of `Workflow` objects. """ test_handler = queue_handler.QueueHandler(self.config_path) test_iterator = test_handler.prepare_workflows( self.mock_workflow_metas) assert isinstance(test_iterator, map) expect_result = ['fake-id-1', 'fake-id-2', 'fake-id-3'] for idx, item in enumerate(test_iterator): assert item.id == expect_result[idx]
def test_retrieve_workflows_returns_none_for_connection_error( self, caplog): """ This function asserts the `queue_handler.retrieve_workflows()` works properly if it runs into connection errors when talking to the Cromwell. """ caplog.set_level(logging.ERROR) test_handler = queue_handler.QueueHandler(self.config_path) results = test_handler.retrieve_workflows( test_handler.cromwell_query_dict) error = caplog.text assert results is None assert 'Failed to retrieve workflows from Cromwell' in error
def test_retrieve_workflows_returns_none_for_400_response_code( self, caplog): """ This function asserts the `queue_handler.retrieve_workflows()` works properly when it gets 400 error code from the Cromwell. """ caplog.set_level(logging.WARNING) test_handler = queue_handler.QueueHandler(self.config_path) results = test_handler.retrieve_workflows( test_handler.cromwell_query_dict) warn = caplog.text assert results is None assert 'Failed to retrieve workflows from Cromwell' in warn
def test_queue_handler_join_can_handle_exception(self, caplog): """ This function asserts the `queue_handler.join()` handles the exception properly, meanwhile, insufficiently, this to some extent, tests the availability of `queue_handler.join()`, since it's just a wrapper around the `threading.Thread.join()`. """ caplog.set_level(logging.ERROR) test_handler = queue_handler.QueueHandler(self.config_path) assert test_handler.thread is None test_handler.join() error = caplog.text assert 'The thread of this queue handler is not in a running state.' in error
def test_enqueue_can_put_a_workflow_into_the_queue(self): """ This function asserts the `queue_handler.enqueue()` puts a workflow into the queue """ test_handler = queue_handler.QueueHandler(self.config_path) assert test_handler.workflow_queue.empty() is True mock_workflow = queue_handler.Workflow( workflow_id='fake_workflow_id', bundle_uuid='fake_bundle_uuid', bundle_version='fake_bundle_version', ) test_handler.enqueue(iter([mock_workflow])) assert test_handler.workflow_queue.empty() is False out = test_handler.workflow_queue.get() assert out.id == 'fake_workflow_id'
def test_retrieve_workflows_returns_query_results_successfully( self, caplog): """ This function asserts the `queue_handler.retrieve_workflows()` works properly when it gets 200 OK from the Cromwell. """ caplog.set_level(logging.INFO) test_handler = queue_handler.QueueHandler(self.config_path) results = test_handler.retrieve_workflows( test_handler.cromwell_query_dict) info = caplog.text assert isinstance(results, list) num_workflows = len(results) assert num_workflows > 0 assert 'Retrieved {0} workflows from Cromwell.'.format( num_workflows) in info
def test_set_queue_indeed_changes_the_reference_pointer_properly( self, caplog): """ This function asserts the `queue_handler.set_queue()` accepts a `queue.Queue` object and points the reference to the queue when it gets called. """ caplog.set_level(logging.INFO) test_handler = queue_handler.QueueHandler(self.config_path) initial_queue_id = id(test_handler.workflow_queue) another_queue = Queue(-1) another_queue_id = id(another_queue) test_handler.set_queue(another_queue) final_queue_id = id(test_handler.workflow_queue) assert initial_queue_id != final_queue_id assert final_queue_id == another_queue_id
def test_execution_event_goes_back_to_sleep_directly_when_it_fails_to_retrieve_workflows( self, caplog): """ This function asserts when the `queue_handler.execution_event()` fails to retrieve any workflow, it will go back to sleep directly. """ caplog.set_level(logging.INFO) test_handler = queue_handler.QueueHandler(self.config_path) test_handler.queue_update_interval = 1 start = timeit.default_timer() test_handler.execution_event() stop = timeit.default_timer() elapsed = stop - start info = caplog.text assert 'is warmed up and running.' in info assert ( 'Cannot fetch any workflow from Cromwell, go back to sleep and wait for next attempt.' in info) assert (test_handler.queue_update_interval <= elapsed <= test_handler.queue_update_interval * 1.5)
def test_assemble_workflow_can_work_on_workflow_metadata_properly(self): """ This function asserts the `queue_handler._assemble_workflow()` properly parses an object of workflow metadata and assemble it as a `Workflow` instance. """ test_metadata = { 'id': 'fake-id-1', 'name': 'fake-name-1', 'status': 'On Hold', 'submission': '2018-01-01T23:49:40.620Z', 'labels': { 'cromwell-workflow-id': 'cromwell-fake-id-1', 'bundle-uuid': 'fake-bundle-uuid-1', 'bundle-version': '2018-01-01T22:49:40.620Z', 'workflow-name': 'fake-name-1', }, } test_handler = queue_handler.QueueHandler(self.config_path) workflow = test_handler._assemble_workflow(test_metadata) assert isinstance(workflow, queue_handler.Workflow) assert workflow.id == 'fake-id-1' assert workflow.bundle_uuid == 'fake-bundle-uuid-1' assert workflow.bundle_version == '2018-01-01T22:49:40.620Z'