Exemplo n.º 1
0
def test_worker_processes_tasks_with_pre_and_post_process():
    """
    Test worker runs registered callbacks when processing a message
    """

    # Declare these so they can be checked as a side effect to the callbacks
    contexts = []

    def pre_process_with_side_effect(context):
        contexts.append(context)

    def post_process_with_side_effect(context):
        contexts.append(context)

    # When we have a registered pre_process and post_process callback
    register_event("pre_process", pre_process_with_side_effect)
    register_event("post_process", post_process_with_side_effect)

    # And we process a message
    internal_queue = _add_message_to_internal_queue(
        'tests.tasks.index_incrementer')
    worker = ProcessWorker(internal_queue, INTERVAL, parent_id=1)
    worker.process_message()

    pre_process_context = contexts[0]

    # We should run the callbacks with the right task contexts
    pre_process_context['task_name'].should.equal('index_incrementer')
    pre_process_context['args'].should.equal([])
    pre_process_context['kwargs'].should.equal({'message': 'Test message'})
    pre_process_context['full_task_path'].should.equal(
        'tests.tasks.index_incrementer')
    pre_process_context['queue_url'].should.equal(
        'https://queue.amazonaws.com/123456789012/tester')
    pre_process_context['timeout'].should.equal(30)

    assert 'fetch_time' in pre_process_context
    assert 'status' not in pre_process_context

    post_process_context = contexts[1]

    post_process_context['task_name'].should.equal('index_incrementer')
    post_process_context['args'].should.equal([])
    post_process_context['kwargs'].should.equal({'message': 'Test message'})
    post_process_context['full_task_path'].should.equal(
        'tests.tasks.index_incrementer')
    post_process_context['queue_url'].should.equal(
        'https://queue.amazonaws.com/123456789012/tester')
    post_process_context['timeout'].should.equal(30)
    post_process_context['status'].should.equal('success')

    assert 'fetch_time' in post_process_context
    assert 'exception' not in post_process_context

    # And the internal queue should be empty
    _check_internal_queue_is_empty(internal_queue)
Exemplo n.º 2
0
def test_register_different_events():
    def print_pre_process(context):
        print(context)

    def print_post_process(context):
        print(context)

    events.register_event("pre_process", print_pre_process)
    events.register_event("post_process", print_post_process)
    events.get_events().pre_process.should.equal([print_pre_process])
    events.get_events().post_process.should.equal([print_post_process])
Exemplo n.º 3
0
def test_register_multiple_same_events():
    def print_pre_process(context):
        print(context)

    def print_numbers(context):
        print(1 + 2)

    events.register_event("pre_process", print_pre_process)
    events.register_event("pre_process", print_numbers)
    events.get_events().pre_process.should.equal(
        [print_pre_process, print_numbers])
Exemplo n.º 4
0
def test_worker_processes_tasks_with_post_process_callback_exception():
    """
    Test simple worker runs registered callbacks when processing
    a message and it fails
    """

    queue_url, packed_message = _create_packed_message(
        'tests.tasks.exception_task')

    # Declare this so it can be checked as a side effect
    # to post_process_with_side_effect
    contexts = []

    def post_process_with_side_effect(context):
        contexts.append(context)

    # When we have a registered post_process callback
    register_event("post_process", post_process_with_side_effect)

    worker = SimpleProcessWorker(queue_url, INTERVAL, BATCHSIZE, parent_id=1)
    worker.process_message(packed_message)

    post_process_context = contexts[0]

    # We should run the callback with the task context
    post_process_context['task_name'].should.equal('exception_task')
    post_process_context['args'].should.equal([])
    post_process_context['kwargs'].should.equal({'message': 'Test message'})
    post_process_context['full_task_path'].should.equal(
        'tests.tasks.exception_task')
    post_process_context['queue_url'].should.equal(
        'https://queue.amazonaws.com/123456789012/tester')
    post_process_context['timeout'].should.equal(30)
    post_process_context['status'].should.equal('exception')

    assert 'fetch_time' in post_process_context
    assert 'receipt_handle' in post_process_context
    assert 'exception' in post_process_context