Exemplo n.º 1
0
def test_stateful_tasks(manager):
    @manager.task(queue='normal', init_state=lambda: [0])
    def task(ctx):
        ctx.state[0] += 1
        ctx.set_result(None)

    manager.process(make_task('task'))
    manager.process(make_task('task'))
    manager.process(make_task('task'))
    assert manager.states['task'] == [3]
Exemplo n.º 2
0
def test_stateful_tasks(manager):
    @manager.task(queue='normal', init_state=lambda: [0])
    def task(ctx):
        ctx.state[0] += 1
        ctx.set_result(None)

    manager.process(make_task('task'))
    manager.process(make_task('task'))
    manager.process(make_task('task'))
    assert manager.states['task'] == [3]
Exemplo n.º 3
0
def test_expired_task(manager):
    called = []

    @manager.task
    def foo():
        called.append(True)

    manager.process(make_task('foo', expire=10), now=15)
    assert not called

    manager.process(make_task('foo', expire=10), now=5)
    assert called
Exemplo n.º 4
0
def test_expired_task(manager):
    called = []

    @manager.task
    def foo():
        called.append(True)

    manager.process(make_task('foo', expire=10), now=15)
    assert not called

    manager.process(make_task('foo', expire=10), now=5)
    assert called
Exemplo n.º 5
0
def test_manager_must_pass_stop_worker_exc(manager):
    @manager.task
    def alarm():
        raise StopWorker()

    with pytest.raises(StopWorker):
        manager.process(make_task('alarm'))
Exemplo n.º 6
0
def test_manager_must_pass_stop_worker_exc(manager):
    @manager.task
    def alarm():
        raise StopWorker()

    with pytest.raises(StopWorker):
        manager.process(make_task('alarm'))
Exemplo n.º 7
0
def test_dead_task(manager):
    @manager.task
    def foo():
        raise Exception()

    manager.process(make_task('foo', dead='dead'))
    assert manager.pop(['dead'], 1)['name'] == 'foo'
Exemplo n.º 8
0
def test_worker_alarm(manager):
    called = []
    def handler(signal, frame):
        called.append(True)
    signal.signal(signal.SIGALRM, handler)

    @manager.task
    def foo(sleep):
        time.sleep(sleep)

    w = Worker(manager, task_timeout=1)
    w.process_one(make_task('foo', args=(0.1,)))
    assert not called

    w.process_one(make_task('foo', args=(1.1,)))
    assert called
Exemplo n.º 9
0
def test_dead_task(manager):
    @manager.task
    def foo():
        raise Exception()

    manager.process(make_task('foo', dead='dead'))
    assert manager.pop(['dead'], 1)['name'] == 'foo'
Exemplo n.º 10
0
def test_worker_alarm(manager):
    called = []

    def handler(signal, frame):
        called.append(True)

    signal.signal(signal.SIGALRM, handler)

    @manager.task
    def foo(sleep):
        time.sleep(sleep)

    w = Worker(manager, task_timeout=1)
    w.process_one(make_task('foo', args=(0.1, )))
    assert not called

    w.process_one(make_task('foo', args=(1.1, )))
    assert called
Exemplo n.º 11
0
def test_stateful_tasks_close(manager):
    class State(object):
        def close(self):
            self.closed = True

    @manager.task(queue='normal', init_state=State)
    def task(ctx):
        pass

    manager.process(make_task('task'))
    manager.close()
    assert manager.states['task'].closed
Exemplo n.º 12
0
def test_stateful_tasks_close(manager):
    class State(object):
        def close(self):
            self.closed = True

    @manager.task(queue='normal', init_state=State)
    def task(ctx):
        pass

    manager.process(make_task('task'))
    manager.close()
    assert manager.states['task'].closed
Exemplo n.º 13
0
def test_sync_manager(manager):
    manager.sync = True

    @manager.task
    def foo(a, b):
        foo.called = True
        return a + b

    assert foo.push(1, 2).ready().value == 3
    assert foo.called

    with pytest.raises(KeyError):
        manager.process(make_task('boo'))

    @manager.task
    def bad():
        raise ZeroDivisionError()

    with pytest.raises(ZeroDivisionError):
        bad.push()
Exemplo n.º 14
0
def test_sync_manager(manager):
    manager.sync = True

    @manager.task
    def foo(a, b):
        foo.called = True
        return a + b

    assert foo.push(1, 2).ready().value == 3
    assert foo.called

    with pytest.raises(KeyError):
        manager.process(make_task('boo'))

    @manager.task
    def bad():
        raise ZeroDivisionError()

    with pytest.raises(ZeroDivisionError):
        bad.push()
Exemplo n.º 15
0
def test_retry_task(manager):
    @manager.task
    def foo():
        raise Exception()

    manager.default_retry_delay = None
    t = make_task('foo', retry=True)
    t['queue'] = 'test'
    manager.process(t)
    assert manager.pop(['test'], 1)['name'] == 'foo'

    t['retry_delay'] = 10
    manager.process(t, now=20)
    assert not manager.pop(['test'], 1)
    manager.queue.reschedule(50)
    assert manager.pop(['test'], 1)['name'] == 'foo'

    t['retry_delay'] = None
    t['retry'] = 1
    manager.process(t, now=20)
    assert manager.pop(['test'], 1)['retry'] == 0
Exemplo n.º 16
0
def test_retry_task(manager):
    @manager.task
    def foo():
        raise Exception()

    manager.default_retry_delay = None
    t = make_task('foo', retry=True)
    t['queue'] = 'test'
    manager.process(t)
    assert manager.pop(['test'], 1)['name'] == 'foo'

    t['retry_delay'] = 10
    manager.process(t, now=20)
    assert not manager.pop(['test'], 1)
    manager.queue.reschedule(50)
    assert manager.pop(['test'], 1)['name'] == 'foo'

    t['retry_delay'] = None
    t['retry'] = 1
    manager.process(t, now=20)
    assert manager.pop(['test'], 1)['retry'] == 0
Exemplo n.º 17
0
def test_unknown_task(manager):
    manager.process(make_task('foo'))
    assert manager.pop(['unknown'], 1)['name'] == 'foo'
Exemplo n.º 18
0
def test_unknown_task(manager):
    manager.process(make_task('foo'))
    assert manager.pop(['unknown'], 1)['name'] == 'foo'