예제 #1
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
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
예제 #3
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
예제 #4
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)
예제 #5
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
예제 #6
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)
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