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]
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
def test_manager_must_pass_stop_worker_exc(manager): @manager.task def alarm(): raise StopWorker() with pytest.raises(StopWorker): manager.process(make_task('alarm'))
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'
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
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
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
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()
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
def test_unknown_task(manager): manager.process(make_task('foo')) assert manager.pop(['unknown'], 1)['name'] == 'foo'