Exemplo n.º 1
0
def test_RunnableQueue_duplicate_jobs(mocker):
    """
    Verify that duplicate jobs are not added to the queue.
    """
    mock_api_client = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    mock_session_maker = mocker.MagicMock(return_value=mock_session)

    dl_job = FileDownloadJob("mock", "mock", "mock")
    msg_dl_job = MessageDownloadJob("mock", "mock", "mock")
    queue = RunnableQueue(mock_api_client, mock_session_maker)
    debug_logger = mocker.patch("securedrop_client.queue.logger.debug")

    # Queue begins empty (0 entries).
    assert len(queue.queue.queue) == 0

    queue.add_job(dl_job)
    assert len(queue.queue.queue) == 1

    # Now add the same job again.
    queue.add_job(dl_job)
    assert len(queue.queue.queue) == 1

    log_msg = "Duplicate job {}, skipping".format(dl_job)
    debug_logger.call_args[1] == log_msg

    # Now add a _different_ job with the same arguments (same uuid).
    queue.add_job(msg_dl_job)
    assert len(queue.queue.queue) == 2

    # Ensure that using _re_add_job in the case of a timeout won't allow duplicate
    # jobs to be added.
    with queue.condition_add_or_remove_job:
        queue._re_add_job(msg_dl_job)
    assert len(queue.queue.queue) == 2
Exemplo n.º 2
0
def test_RunnableQueue_resubmitted_jobs(mocker):
    """
    Verify that jobs that fail due to timeout are resubmitted without modifying order_number.
    """
    mock_api_client = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    mock_session_maker = mocker.MagicMock(return_value=mock_session)

    job_cls_high_priority = factory.dummy_job_factory(mocker, "mock")
    job_cls_low_priority = factory.dummy_job_factory(mocker, "mock")
    queue = RunnableQueue(mock_api_client, mock_session_maker)
    queue.JOB_PRIORITIES = {job_cls_high_priority: 1, job_cls_low_priority: 2}

    job1 = job_cls_high_priority()
    job2 = job_cls_low_priority()
    job3 = job_cls_high_priority()
    job4 = job_cls_low_priority()

    queue.add_job(job1)
    queue.add_job(job2)
    queue.add_job(job3)
    queue.add_job(job4)

    # Expected order of execution is job1 -> job3 -> job2 -> job4
    assert queue.queue.get(block=True) == (1, job1)

    # Now resubmit job1 via put_nowait. It should execute prior to job2-4.
    with queue.condition_add_or_remove_job:
        queue._re_add_job(job1)
    assert queue.queue.get(block=True) == (1, job1)
    assert queue.queue.get(block=True) == (1, job3)
    assert queue.queue.get(block=True) == (2, job2)
    assert queue.queue.get(block=True) == (2, job4)