Beispiel #1
0
def test_auto_delete():
    name = 'test-auto-delete'
    delete_queue(name)
    subscriber = Subscriber()
    subscriber.register(nop, name, ['test.auto_delete'], auto_delete=True)
    publish_domain_event('test.auto_delete', {})
    subscriber.start_consuming(timeout=1.0)
    assert not check_queue_exists(name)
def test_dummy_mode():
    def handle_event(event):
        handle_event.message = event.data['message']

    handle_event.message = None
    name = 'test-dummy'
    subscriber = Subscriber(connection_settings=None)
    subscriber.register(handle_event, name, ['test.publish-dummy'])
    publish_domain_event('test.publish-dummy', {}, connection_settings=None)
    subscriber.start_consuming(timeout=1.0)
    assert not check_queue_exists(name)
Beispiel #3
0
def dead_letter_message():
    name = 'test-replay'
    delete_queue(name)
    subscriber = Subscriber()
    subscriber.register(raise_error, name, ['test.replay'], dead_letter=True)
    data = dict(message=str(uuid.uuid4())[:4])
    publish_domain_event('test.replay', data)
    subscriber.start_consuming(timeout=0.5)
    yield data
    transport = Subscriber()
    transport.channel.queue_delete(queue='test-replay')
    transport.channel.queue_delete(queue='test-replay-dl')
def test_publish():
    def handle_event(event):
        handle_event.message = event.data['message']

    handle_event.message = None
    name = 'test-publish'
    subscriber = Subscriber()
    subscriber.register(handle_event, name, ['test.publish'])
    data = dict(message=str(uuid.uuid4())[:4])
    publish_domain_event('test.publish', data)
    subscriber.start_consuming(timeout=1.0)
    assert handle_event.message == data['message']
    assert get_queue_size(name) == 0
Beispiel #5
0
def test_dead_letter():
    name = 'test-dead-letter'
    delete_queue(name)
    subscriber = Subscriber()
    subscriber.register(raise_error, name, ['test.dl'], dead_letter=True)
    data = dict(message=str(uuid.uuid4())[:4])
    publish_domain_event('test.dl', data)
    subscriber.start_consuming(timeout=1.0)
    header, event = get_message_from_queue('test-dead-letter-dl')
    assert event.data == data
    transport = Subscriber()
    transport.channel.queue_delete(queue='test-dead-letter')
    transport.channel.queue_delete(queue='test-dead-letter-dl')
Beispiel #6
0
def test_retry():
    def raise_retry(event):
        raise_retry.received += 1
        raise Retry(0.1)
    raise_retry.received = 0

    name = 'test-retry'
    delete_queue(name)
    subscriber = Subscriber()
    subscriber.register(raise_retry, name, ['test.retry'], max_retries=3)
    data = dict(message=str(uuid.uuid4())[:4])
    publish_domain_event('test.retry', data)
    subscriber.start_consuming(timeout=1.0)
    assert raise_retry.received == 4
    assert get_queue_size(name) == 0
Beispiel #7
0
def test_multiple_listeneres():
    def one(event):
        one.received += 1
    one.received = 0

    def two(event):
        two.received += 1
    two.received = 0

    subscriber = Subscriber()
    subscriber.register(one, 'test-listener-one', ['test.one'])
    subscriber.register(two, 'test-listener-two', ['test.two'])
    publish_domain_event('test.one', {})
    publish_domain_event('test.two', {})
    subscriber.start_consuming(timeout=1.0)
    assert one.received == 1
    assert two.received == 1
Beispiel #8
0
def test_retry_delays():
    # Events with shorter retry delays should be processed before the ones with
    # longer delays.
    def raise_retry(event):
        raise_retry.received.append(event.data['delay'])
        raise Retry(event.data['delay'])
    raise_retry.received = []

    name = 'test-retry-delay'
    delete_queue(name)
    subscriber = Subscriber()
    subscriber.register(raise_retry, name, ['test.retry-delay'], max_retries=1)
    publish_domain_event('test.retry-delay', {'delay': 0.5})
    publish_domain_event('test.retry-delay', {'delay': 0.1})
    subscriber.start_consuming(timeout=1.0)
    assert raise_retry.received == [0.5, 0.1, 0.1, 0.5]
    assert get_queue_size(name) == 0
Beispiel #9
0
def test_long_running_consumer():
    # Test a job that takes longer than the heartbeat of the connection.
    # Make sure the consumer finishes processing and acknowledges the event.
    name = 'test-long-job'
    delete_queue(name)

    def slow_nop(event):
        sleep(3.0)
        slow_nop.finished += 1

    slow_nop.finished = 0
    subscriber = Subscriber(connection_settings=settings.BROKER+'?heartbeat=1')
    subscriber.register(slow_nop, name, ['test.long_job'])
    publish_domain_event('test.long_job', {})
    subscriber.start_consuming(timeout=5.0)
    assert get_queue_size(name) == 0
    assert slow_nop.finished == 1
 def test_fire_event(self, mock):
     """Use basic API to fire event"""
     event = publish_domain_event('test.test', {})
     assert mock.called
     json_data, routing_key = mock.call_args[0]
     event_data = json.loads(json_data)
     assert routing_key == "test.test"
     assert event_data['data'] == {}
     assert event_data['uuid_string'] == event.uuid_string
 def test_event_loading(self, mock):
     event = publish_domain_event('test.test', {})
     assert mock.called
     json_data, routing_key = mock.call_args[0]
     new_event = DomainEvent.from_json(json_data)
     assert new_event == event
def main():
    event = publish_domain_event('test_domain.event_has_happened', data={'myinfo': 'foo'})
    print(" [x] Sent {}".format(event))