| 1 |
import itertools |
| 2 |
import time |
| 3 |
|
| 4 |
import pytest |
| 5 |
from billiard.einfo import ExceptionInfo |
| 6 |
|
| 7 |
pytest.importorskip('multiprocessing') |
| 8 |
|
| 9 |
|
| 10 |
def do_something(i): |
| 11 |
return i * i |
| 12 |
|
| 13 |
|
| 14 |
def long_something(): |
| 15 |
time.sleep(1) |
| 16 |
|
| 17 |
|
| 18 |
def raise_something(i): |
| 19 |
try: |
| 20 |
raise KeyError('FOO EXCEPTION') |
| 21 |
except KeyError: |
| 22 |
return ExceptionInfo() |
| 23 |
|
| 24 |
|
| 25 |
class test_TaskPool: |
| 26 |
|
| 27 |
def setup_method(self): |
| 28 |
from celery.concurrency.prefork import TaskPool |
| 29 |
self.TaskPool = TaskPool |
| 30 |
|
| 31 |
def test_attrs(self): |
| 32 |
p = self.TaskPool(2) |
| 33 |
assert p.limit == 2 |
| 34 |
assert p._pool is None |
| 35 |
|
| 36 |
def x_apply(self): |
| 37 |
p = self.TaskPool(2) |
| 38 |
p.start() |
| 39 |
scratchpad = {} |
| 40 |
proc_counter = itertools.count() |
| 41 |
|
| 42 |
def mycallback(ret_value): |
| 43 |
process = next(proc_counter) |
| 44 |
scratchpad[process] = {} |
| 45 |
scratchpad[process]['ret_value'] = ret_value |
| 46 |
|
| 47 |
myerrback = mycallback |
| 48 |
|
| 49 |
res = p.apply_async(do_something, args=[10], callback=mycallback) |
| 50 |
res2 = p.apply_async(raise_something, args=[10], errback=myerrback) |
| 51 |
res3 = p.apply_async(do_something, args=[20], callback=mycallback) |
| 52 |
|
| 53 |
assert res.get() == 100 |
| 54 |
time.sleep(0.5) |
| 55 |
assert scratchpad.get(0)['ret_value'] == 100 |
| 56 |
|
| 57 |
assert isinstance(res2.get(), ExceptionInfo) |
| 58 |
assert scratchpad.get(1) |
| 59 |
time.sleep(1) |
| 60 |
assert isinstance(scratchpad[1]['ret_value'], ExceptionInfo) |
| 61 |
assert scratchpad[1]['ret_value'].exception.args == ('FOO EXCEPTION',) |
| 62 |
|
| 63 |
assert res3.get() == 400 |
| 64 |
time.sleep(0.5) |
| 65 |
assert scratchpad.get(2)['ret_value'] == 400 |
| 66 |
|
| 67 |
res3 = p.apply_async(do_something, args=[30], callback=mycallback) |
| 68 |
|
| 69 |
assert res3.get() == 900 |
| 70 |
time.sleep(0.5) |
| 71 |
assert scratchpad.get(3)['ret_value'] == 900 |
| 72 |
p.stop() |