Ejemplo n.º 1
0
def test_enqueue_call():
    """Enqueueing job onto queues."""

    connection = object()
    uuids = []

    class Protocol:
        @staticmethod
        @asyncio.coroutine
        def enqueue_job(redis,
                        queue,
                        id,
                        data,
                        description,
                        timeout,
                        created_at,
                        *,
                        result_ttl=unset,
                        dependency_id=unset,
                        at_front=False):
            assert redis is connection
            assert queue == 'example'
            assert isinstance(id, str)
            assert data == b'\x80\x04\x952\x00\x00\x00\x00\x00\x00\x00(\x8c\x12fixtures.say_hello\x94N\x8c\x04Nick\x94\x85\x94}\x94\x8c\x03foo\x94\x8c\x03bar\x94st\x94.'  # noqa
            assert description == "fixtures.say_hello('Nick', foo='bar')"
            assert timeout == 180
            assert created_at == utcformat(utcnow())
            assert result_ttl is unset
            assert dependency_id is unset
            assert at_front is False
            uuids.append(id)
            return JobStatus.QUEUED, utcnow()

    class TestQueue(Queue):
        protocol = Protocol()

    q = TestQueue(connection, 'example')

    job = yield from q.enqueue_call(say_hello,
                                    args=('Nick', ),
                                    kwargs={'foo': 'bar'})

    assert job.connection is connection
    assert job.id == uuids.pop(0)
    assert job.func == say_hello
    assert job.args == ('Nick', )
    assert job.kwargs == {'foo': 'bar'}
    assert job.description == "fixtures.say_hello('Nick', foo='bar')"
    assert job.timeout == 180
    assert job.result_ttl == None  # TODO: optional?
    assert job.origin == q.name
    assert helpers.strip_microseconds(
        job.created_at) == helpers.strip_microseconds(utcnow())
    assert helpers.strip_microseconds(
        job.enqueued_at) == helpers.strip_microseconds(utcnow())
    assert job.status == JobStatus.QUEUED
    assert job.dependency_id is None
Ejemplo n.º 2
0
def test_enqueue_call():
    """Enqueueing job onto queues."""

    connection = object()
    uuids = []

    class Protocol:
        @staticmethod
        @asyncio.coroutine
        def enqueue_job(redis, queue, id, data, description, timeout,
                        created_at, *, ttl=None, result_ttl=unset,
                        dependency_id=unset, at_front=False,
                        meta=None):
            assert redis is connection
            assert queue == 'example'
            assert isinstance(id, str)
            assert data == b'\x80\x04\x952\x00\x00\x00\x00\x00\x00\x00(\x8c\x12fixtures.say_hello\x94N\x8c\x04Nick\x94\x85\x94}\x94\x8c\x03foo\x94\x8c\x03bar\x94st\x94.' # noqa
            assert description == "fixtures.say_hello('Nick', foo='bar')"
            assert timeout == 180
            assert created_at == utcformat(utcnow())
            assert not ttl
            assert result_ttl is unset
            assert dependency_id is unset
            assert at_front is False
            assert not meta
            uuids.append(id)
            return JobStatus.QUEUED, utcnow()

    class TestQueue(Queue):
        protocol = Protocol()

    q = TestQueue(connection, 'example')

    job = yield from q.enqueue_call(say_hello, args=('Nick',), kwargs={'foo': 'bar'})

    assert job.connection is connection
    assert job.id == uuids.pop(0)
    assert job.func == say_hello
    assert job.args == ('Nick',)
    assert job.kwargs == {'foo': 'bar'}
    assert job.description == "fixtures.say_hello('Nick', foo='bar')"
    assert job.timeout == 180
    assert job.result_ttl is None  # TODO: optional?
    assert job.origin == q.name
    assert helpers.strip_microseconds(job.created_at) == helpers.strip_microseconds(utcnow())
    assert helpers.strip_microseconds(job.enqueued_at) == helpers.strip_microseconds(utcnow())
    assert job.status == JobStatus.QUEUED
    assert job.dependency_id is None
Ejemplo n.º 3
0
def test_work_fails(loop):
    """Failing jobs are put on the failed queue."""

    q = Queue()
    failed_q = get_failed_queue()

    # Preconditions
    assert not (yield from failed_q.count)
    assert not (yield from q.count)

    # Action
    job = yield from q.enqueue(div_by_zero)
    assert (yield from q.count) == 1

    # keep for later
    enqueued_at_date = strip_microseconds(job.enqueued_at)

    w = Worker([q])
    yield from w.work(burst=True, loop=loop)  # Should silently pass

    # Postconditions
    assert not (yield from q.count)
    assert (yield from failed_q.count) == 1
    assert not (yield from w.get_current_job_id())

    # Check the job
    job = yield from Job.fetch(job.id)
    assert job.origin == q.name

    # Should be the original enqueued_at date, not the date of enqueueing
    # to the failed queue
    assert job.enqueued_at == enqueued_at_date
    assert job.exc_info  # should contain exc_info
Ejemplo n.º 4
0
def test_persistence_of_typical_jobs(redis):
    """Storing typical jobs."""

    job = Job.create(func=some_calculation, args=(3, 4), kwargs=dict(z=2))
    yield from job.save()

    expected_date = strip_microseconds(job.created_at)
    stored_date = (yield from redis.hget(job.key, 'created_at')) \
        .decode('utf-8')
    assert stored_date == utcformat(expected_date)

    # ... and no other keys are stored
    assert sorted((yield from redis.hkeys(job.key))) \
        == [b'created_at', b'data', b'description']