def test_event_multiple_listeners(ctx): from eventlet import sleep from melkman.messaging import EventBus event_bus = EventBus(ctx) try: got_events = dict() got_events["one"] = 0 def got_one(event): if event["foo"] == 12: got_events["one"] += 1 got_events["two"] = 0 def got_two(event): if event["foo"] == 12: got_events["two"] += 1 event_bus.add_listener("test", got_one) event_bus.add_listener("test", got_two) event_bus.send("test", {"foo": 12}) sleep(0.5) assert got_events["one"] == 1 assert got_events["two"] == 1 finally: event_bus.kill()
def test_defer_event(ctx): from datetime import datetime, timedelta from eventlet import sleep, spawn, with_timeout from eventlet.event import Event from melkman.messaging import EventBus from melkman.scheduler import defer_event from melkman.scheduler.worker import ScheduledMessageService CHAN = 'test_chan' sms = ScheduledMessageService(ctx) sched = spawn(sms.run) got_message = Event() def got_message_cb(*args, **kw): got_message.send(True) eb = EventBus(ctx) eb.add_listener(CHAN, got_message_cb) now = datetime.utcnow() wait = timedelta(seconds=2) defer_event(now + wait, CHAN, {'foo': 'bar'}, ctx) sleep(3) try: with_timeout(10, got_message.wait) assert got_message.ready() finally: eb.kill() sched.kill() sched.wait()
def test_event_send_receive(ctx): from eventlet import sleep from melkman.messaging import EventBus try: event_bus = EventBus(ctx) got_events = dict() got_events["count"] = 0 def got_one(event): if event["foo"] == 12: got_events["count"] += 1 event_bus.add_listener("test", got_one) event_bus.send("test", {"foo": 12}) sleep(0.5) assert got_events["count"] == 1 finally: event_bus.kill()