Example #1
0
def test_timeout2():
    test_time = timedelta(minutes=2)
    under_test = CommandStatus("job_id1", "command_id1", test_time)
    assert under_test.timeout == test_time
    test_time2 = timedelta(seconds=30)
    under_test.timeout = test_time2
    assert under_test.timeout == test_time2
def test_prune_old():
    under_test = CommandChannel("localhost:42/some_topic")
    now = datetime.now()
    test_worker = WorkerStatus("some_service_id")
    test_worker.state = WorkerState.IDLE
    under_test.map_of_workers["some_id"] = test_worker

    test_job = JobStatus("some_id")
    test_job.state = JobState.WRITING
    under_test.map_of_jobs["some_id"] = test_job

    test_command = CommandStatus("some_id", "some_other_id")
    test_command.state = CommandState.SUCCESS
    under_test.map_of_commands["some_id"] = test_command

    under_test.update_workers(now + timedelta(seconds=60))

    time_diff = timedelta(minutes=5)
    under_test.update_workers(now + DEAD_ENTITY_TIME_LIMIT - time_diff)
    assert len(under_test.map_of_commands) == 1
    assert len(under_test.map_of_jobs) == 1
    assert len(under_test.map_of_workers) == 1
    under_test.update_workers(now + DEAD_ENTITY_TIME_LIMIT + time_diff)
    assert len(under_test.map_of_commands) == 0
    assert len(under_test.map_of_jobs) == 0
    assert len(under_test.map_of_workers) == 0
Example #3
0
def test_prune_old():
    status_queue = Queue()
    under_test = InThreadStatusTracker(status_queue)
    now = datetime.now()
    test_worker = WorkerStatus("some_service_id")
    test_worker.state = WorkerState.IDLE
    under_test.known_workers["some_id"] = test_worker

    test_job = JobStatus("some_id")
    test_job.state = JobState.WRITING
    under_test.known_jobs["some_id"] = test_job

    test_command = CommandStatus("some_id", "some_other_id")
    test_command.state = CommandState.SUCCESS
    under_test.known_commands["some_id"] = test_command

    time_diff = timedelta(minutes=5)
    under_test.prune_dead_entities(now + DEAD_ENTITY_TIME_LIMIT - time_diff)
    assert len(under_test.known_commands) == 1
    assert len(under_test.known_jobs) == 1
    assert len(under_test.known_workers) == 1
    under_test.prune_dead_entities(now + DEAD_ENTITY_TIME_LIMIT + time_diff)
    assert len(under_test.known_commands) == 0
    assert len(under_test.known_jobs) == 0
    assert len(under_test.known_workers) == 0
Example #4
0
def test_set_state():
    under_test1 = CommandStatus("job_id1", "command_id1")
    last_update = under_test1.last_update
    test_state = CommandState.SUCCESS
    assert under_test1.state == CommandState.NO_COMMAND
    under_test1.state = test_state
    assert under_test1.state == test_state
    assert under_test1.last_update != last_update
Example #5
0
def test_set_message():
    under_test1 = CommandStatus("job_id1", "command_id1")
    last_update = under_test1.last_update
    test_msg = "some test message"
    assert under_test1.message == ""
    under_test1.message = test_msg
    assert under_test1.message == test_msg
    assert under_test1.last_update != last_update
Example #6
0
def test_is_done():
    mock_cmd_ch = Mock()
    job_id = "some_job_id"
    cmd_id = "some_command_id"
    stand_in_status = CommandStatus(job_id, cmd_id)
    stand_in_status.state = CommandState.SUCCESS
    mock_cmd_ch.get_command.return_value = stand_in_status
    under_test = CommandHandler(mock_cmd_ch, cmd_id)
    assert under_test.is_done()
    mock_cmd_ch.get_command.assert_called_once_with(cmd_id)
Example #7
0
def test_has_timed_out():
    mock_cmd_ch = Mock()
    job_id = "some_job_id"
    cmd_id = "some_command_id"
    stand_in_status = CommandStatus(job_id, cmd_id)
    stand_in_status.state = CommandState.TIMEOUT_RESPONSE
    mock_cmd_ch.get_command.return_value = stand_in_status
    under_test = CommandHandler(mock_cmd_ch, cmd_id)
    with pytest.raises(RuntimeError):
        under_test.is_done()
    mock_cmd_ch.get_command.assert_called_once_with(cmd_id)
Example #8
0
def test_is_not_done():
    mock_cmd_ch = Mock()
    job_id = "some_job_id"
    cmd_id = "some_command_id"
    stand_in_status = CommandStatus(job_id, cmd_id)
    stand_in_status.state = CommandState.ERROR
    mock_cmd_ch.get_command.return_value = stand_in_status
    under_test = CommandHandler(mock_cmd_ch, cmd_id)
    with pytest.raises(RuntimeError):
        under_test.is_done()
    assert mock_cmd_ch.get_command.call_count >= 1
Example #9
0
def test_get_error_with_id():
    mock_cmd_ch = Mock()
    job_id = "some_job_id"
    cmd_id = "some_command_id"
    message_string = "some message"
    stand_in_status = CommandStatus(job_id, cmd_id)
    stand_in_status.message = message_string
    mock_cmd_ch.get_command.return_value = stand_in_status
    under_test = CommandHandler(mock_cmd_ch, cmd_id)
    assert under_test.get_message() == message_string
    mock_cmd_ch.get_command.assert_called_once_with(cmd_id)
Example #10
0
def test_init():
    job_id = "some job id"
    command_id = "some command id"
    under_test1 = CommandStatus(job_id=job_id, command_id=command_id)
    assert under_test1.job_id == job_id
    assert under_test1.command_id == command_id
    assert under_test1.state == CommandState.NO_COMMAND
Example #11
0
def test_get_state_with_id():
    mock_cmd_ch = Mock()
    job_id = "some_job_id"
    cmd_id = "some_command_id"
    stand_in_status = CommandStatus(job_id, cmd_id)
    mock_cmd_ch.get_command.return_value = stand_in_status
    under_test = CommandHandler(mock_cmd_ch, cmd_id)
    assert under_test.get_state() == stand_in_status.state
    mock_cmd_ch.get_command.assert_called_once_with(cmd_id)
Example #12
0
def test_update_message():
    under_test1 = CommandStatus("job_id1", "command_id1")
    under_test2 = CommandStatus("job_id1", "command_id1")
    test_msg = "some test message"
    under_test1.message = test_msg
    assert under_test2.message == ""
    under_test2.update_status(under_test1)  # No exception
    assert under_test2.message == test_msg
Example #13
0
 def check_for_command_presence(self, job_id: str, command_id: str):
     """
     Check if a command identifier is known and add it to a list of known ones if it is not.
     :param job_id: The job identifier of the command that we are looking for. (Only used if we need to add the
     command.)
     :param command_id: The command identifier to look for.
     """
     if command_id not in self.known_commands:
         new_command = CommandStatus(job_id, command_id)
         self.known_commands[command_id] = new_command
 def add_command_id(self, job_id: str, command_id: str):
     """
     Add a command identifier to the list of known commands before it has been encountered on the command topic.
     :param job_id: The job identifier of the new command.
     :param command_id: The identifier of the new command.
     """
     if command_id not in self.map_of_commands:
         self.map_of_commands[command_id] = CommandStatus(
             job_id, command_id)
         self.map_of_commands[
             command_id].state = CommandState.WAITING_RESPONSE
def test_timed_out():
    under_test = CommandChannel("localhost:42/some_topic")
    now = datetime.now()
    test_worker = WorkerStatus("some_service_id")
    test_worker.state = WorkerState.UNKNOWN
    under_test.map_of_workers["some_id"] = test_worker

    test_job = JobStatus("some_id")
    test_job.state = JobState.WAITING
    under_test.map_of_jobs["some_id"] = test_job

    test_command = CommandStatus("some_id", "some_other_id")
    test_command.state = CommandState.WAITING_RESPONSE
    under_test.map_of_commands["some_id"] = test_command

    time_diff = timedelta(minutes=5)
    under_test.update_workers(now + time_diff)
    assert under_test.map_of_commands[
        "some_id"].state == CommandState.TIMEOUT_RESPONSE
    assert under_test.map_of_jobs["some_id"].state == JobState.TIMEOUT
    assert under_test.map_of_workers[
        "some_id"].state == WorkerState.UNAVAILABLE
def test_update_command_status():
    under_test = CommandChannel("localhost:42/some_topic")
    job_id = "some_other_id"
    command_id = "some_id"
    new_status = CommandStatus(job_id, command_id)
    assert len(under_test.list_commands()) == 0
    assert under_test.get_command(command_id) is None
    under_test.status_queue.put(new_status)
    under_test.update_workers()
    assert len(under_test.list_commands()) == 1
    assert under_test.get_command(command_id).command_id == command_id
    under_test.stop_thread()
    del under_test
Example #17
0
def test_eq_failure_2():
    under_test1 = CommandStatus("job_id1", "command_id1")
    under_test2 = CommandStatus("job_id1", "command_id2")
    assert under_test1 != under_test2
Example #18
0
def test_eq_exception():
    under_test1 = CommandStatus("job_id", "command_id")
    with pytest.raises(NotImplementedError):
        under_test1 == 1
Example #19
0
def test_update_success():
    under_test1 = CommandStatus("job_id1", "command_id1")
    under_test2 = CommandStatus("job_id1", "command_id1")
    under_test2.update_status(under_test1)  # No exception
Example #20
0
def test_timeout1():
    under_test = CommandStatus("job_id1", "command_id1")
    assert under_test.timeout == COMMAND_STATUS_TIMEOUT
Example #21
0
def test_eq_success():
    under_test1 = CommandStatus("job_id", "command_id")
    under_test2 = CommandStatus("job_id", "command_id")
    assert under_test1 == under_test2
Example #22
0
def test_update_failure():
    under_test1 = CommandStatus("job_id1", "command_id1")
    under_test2 = CommandStatus("job_id1", "command_id2")
    with pytest.raises(RuntimeError):
        under_test2.update_status(under_test1)