예제 #1
0
def test_deferred_in_database(ctx):
    from datetime import datetime, timedelta
    from carrot.messaging import Consumer
    from eventlet import sleep, spawn
    import logging
    from melk.util.nonce import nonce_str
    import sys

    from melkman.context import Context
    from melkman.scheduler import defer_amqp_message, cancel_deferred
    from melkman.scheduler.worker import ScheduledMessageService
    from melkman.scheduler.worker import DeferredAMQPMessage, view_deferred_messages_by_timestamp

    sms = ScheduledMessageService(ctx)
    sched = spawn(sms.run)
    
    m1 = {'hello_world': nonce_str()}
    when = no_micro(datetime.utcnow() + timedelta(hours=2))
    defer_amqp_message(when, m1, 'testq', 'testx', ctx)
    
    # give it a sec to write it out, then close it down.
    sleep(1)
    sched.kill()
    sched.wait()
    
    # check the database for the message we sent
    count = 0
    for r in view_deferred_messages_by_timestamp(ctx.db, include_docs=True):
        count += 1
        message = DeferredAMQPMessage.wrap(r.doc)
        for (k, v) in m1.items():
            assert message.message[k] == v
        assert message.timestamp == when
    assert count == 1
예제 #2
0
def test_deferred_send_receive(ctx):
    from datetime import datetime, timedelta
    from carrot.messaging import Consumer
    from eventlet import sleep, spawn, with_timeout
    from eventlet.event import Event
    from eventlet.support.greenlets import GreenletExit
    import logging
    from melk.util.nonce import nonce_str
    import sys

    from melkman.context import Context
    from melkman.scheduler import defer_amqp_message, cancel_deferred
    from melkman.scheduler.worker import ScheduledMessageService

    got_message = Event()
    def got_message_cb(*args, **kw):
        got_message.send(True)

    def do_consume():
        consumer = Consumer(ctx.broker, exchange='testx', queue='testq', 
                            routing_key='testq', exclusive=True, durable=False)
        consumer.register_callback(got_message_cb)
        try:
            consumer.wait(limit=1)
        except StopIteration:
            pass
        except GreenletExit:
            pass
        finally:
            consumer.close()
            

    cons = spawn(do_consume)

    sms = ScheduledMessageService(ctx)
    sched = spawn(sms.run)

    m1 = {'hello': 'world'}
    now = datetime.utcnow()
    wait = timedelta(seconds=2)
    defer_amqp_message(now + wait, m1, 'testq', 'testx', ctx)

    try:
        #sleep(1)
        with_timeout(10, got_message.wait)
        assert got_message.ready()
    finally:
        sched.kill()
        sched.wait()
        cons.kill()
        cons.wait()