| 1 |
import io |
| 2 |
from datetime import datetime, timezone |
| 3 |
|
| 4 |
from celery.events import dumper |
| 5 |
|
| 6 |
|
| 7 |
def test_humanize_type(): |
| 8 |
assert dumper.humanize_type('worker-online') == 'started' |
| 9 |
assert dumper.humanize_type('worker-offline') == 'shutdown' |
| 10 |
assert dumper.humanize_type('worker-heartbeat') == 'heartbeat' |
| 11 |
|
| 12 |
|
| 13 |
def test_dumper_say(): |
| 14 |
buf = io.StringIO() |
| 15 |
d = dumper.Dumper(out=buf) |
| 16 |
d.say('hello world') |
| 17 |
assert 'hello world' in buf.getvalue() |
| 18 |
|
| 19 |
|
| 20 |
def test_format_task_event_output(): |
| 21 |
buf = io.StringIO() |
| 22 |
d = dumper.Dumper(out=buf) |
| 23 |
d.format_task_event( |
| 24 |
hostname='worker1', |
| 25 |
timestamp=datetime(2024, 1, 1, 12, 0, 0), |
| 26 |
type='task-succeeded', |
| 27 |
task='mytask(123) args=(1,) kwargs={}', |
| 28 |
event={'result': 'ok', 'foo': 'bar'} |
| 29 |
) |
| 30 |
output = buf.getvalue() |
| 31 |
assert 'worker1 [2024-01-01 12:00:00]' in output |
| 32 |
assert 'task succeeded' in output |
| 33 |
assert 'mytask(123) args=(1,) kwargs={}' in output |
| 34 |
assert 'result=ok' in output |
| 35 |
assert 'foo=bar' in output |
| 36 |
|
| 37 |
|
| 38 |
def test_on_event_task_received(): |
| 39 |
buf = io.StringIO() |
| 40 |
d = dumper.Dumper(out=buf) |
| 41 |
event = { |
| 42 |
'timestamp': datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc).timestamp(), |
| 43 |
'type': 'task-received', |
| 44 |
'hostname': 'worker1', |
| 45 |
'uuid': 'abc', |
| 46 |
'name': 'mytask', |
| 47 |
'args': '(1,)', |
| 48 |
'kwargs': '{}', |
| 49 |
} |
| 50 |
d.on_event(event.copy()) |
| 51 |
output = buf.getvalue() |
| 52 |
assert 'worker1 [2024-01-01 12:00:00+00:00]' in output |
| 53 |
assert 'task received' in output |
| 54 |
assert 'mytask(abc) args=(1,) kwargs={}' in output |
| 55 |
|
| 56 |
|
| 57 |
def test_on_event_non_task(): |
| 58 |
buf = io.StringIO() |
| 59 |
d = dumper.Dumper(out=buf) |
| 60 |
event = { |
| 61 |
'timestamp': datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc).timestamp(), |
| 62 |
'type': 'worker-online', |
| 63 |
'hostname': 'worker1', |
| 64 |
'foo': 'bar', |
| 65 |
} |
| 66 |
d.on_event(event.copy()) |
| 67 |
output = buf.getvalue() |
| 68 |
assert 'worker1 [2024-01-01 12:00:00+00:00]' in output |
| 69 |
assert 'started' in output |
| 70 |
assert 'foo=bar' in output |