def test_should_publish_consume_from_store_queue_from_rabbitmq():

    spy = SpyEvents()

    def assert_consumer_event_store(event: Event) -> BoolResult:
        spy.append(event)
        return isSuccess

    event = EventUserCreatedMother.random()

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_event(event)

    bus = RabbitMqEventBusMother.default()
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.default()

    consumer.add_handler_on_store(assert_consumer_event_store)
    consumer.start()

    sleep(1.0)
    consumer.stop()
    configurer.clear()

    spy.assert_number_unique_events(1)
    spy.assert_first_event(event)
    spy.assert_count_by_event_id(event.event_id, 1)
def test_should_publish_event_only_on_store_queue_with_previous_rabbitmq_configuration(
):
    event = EventUserCreatedMother.random()
    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_event(event)

    bus = RabbitMqEventBusMother.default()
    bus.retry_publish_only_on_store_queue(event)

    configurer.clear()
Exemple #3
0
def test_should_publish_consume_with_event_handler_notify_when_fail_consumer_with_critical_error(
    given_any_petisco
):
    spy = SpyEvents()
    logger = FakeLogger()
    notifier = FakeNotifier()

    class MyCriticalError(CriticalError):
        pass

    @event_handler(logger=logger, notifier=notifier)
    def assert_consumer(event: Event) -> Result[bool, Error]:
        spy.append(event)
        return Failure(MyCriticalError(Exception()))

    event = EventUserCreatedMother.random()
    subscribers = [
        EventSubscriber(
            event_name=event.event_name,
            event_version=event.event_version,
            handlers=[assert_consumer],
        )
    ]

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_subscribers(subscribers)

    bus = RabbitMqEventBusMother.default()
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.without_retry()
    consumer.add_subscribers(subscribers)
    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer.clear()

    spy.assert_number_unique_events(1)
    spy.assert_first_event(event)
    spy.assert_count_by_event_id(event.event_id, 1)

    first_logging_message = logger.get_logging_messages()[0]
    assert first_logging_message == (
        DEBUG,
        LogMessageMother.get_event_handler(
            operation="assert_consumer",
            message={"event": event.event_name, "body": event.to_json()},
        ).to_dict(),
    )
    assert notifier.publish_called
    assert notifier.publish_times_called == 1
Exemple #4
0
def test_should_publish_consume_with_event_handler_when_fail_consumer():
    spy = SpyEvents()
    logger = FakeLogger()

    @event_handler(logger=logger)
    def assert_consumer(event: Event) -> Result[bool, Error]:
        spy.append(event)
        return isFailure

    event = EventUserCreatedMother.random()
    subscribers = [
        EventSubscriber(
            event_name=event.event_name,
            event_version=event.event_version,
            handlers=[assert_consumer],
        )
    ]

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_subscribers(subscribers)

    bus = RabbitMqEventBusMother.default()
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.with_max_retries(1)
    consumer.add_subscribers(subscribers)
    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer.clear()

    spy.assert_number_unique_events(1)
    spy.assert_first_event(event)
    spy.assert_count_by_event_id(event.event_id, 1)

    first_logging_message = logger.get_logging_messages()[0]

    assert first_logging_message == (
        DEBUG,
        LogMessageMother.get_event_handler(
            operation="assert_consumer",
            message={"event": event.event_name, "body": event.to_json()},
        ).to_dict(),
    )
def test_should_retry_publish_only_on_store_queue_not_affecting_default_event_queue(
):

    spy_consumer_default_queue = SpyEvents()
    spy_consumer_store = SpyEvents()

    def assert_consumer_default_queue(event: Event) -> BoolResult:
        spy_consumer_default_queue.append(event)
        return isSuccess

    def assert_consumer_store(event: Event) -> BoolResult:
        spy_consumer_store.append(event)
        return isSuccess

    event = EventUserCreatedMother.random()
    subscribers = [
        EventSubscriber(
            event_name=event.event_name,
            event_version=event.event_version,
            handlers=[assert_consumer_default_queue],
        )
    ]

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_subscribers(subscribers)

    bus = RabbitMqEventBusMother.default()
    bus.retry_publish_only_on_store_queue(event)

    consumer = RabbitMqEventConsumerMother.default()
    consumer.add_subscribers(subscribers)
    consumer.add_handler_on_store(assert_consumer_store)

    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer.clear()

    spy_consumer_default_queue.assert_number_unique_events(0)
    spy_consumer_default_queue.assert_count_by_event_id(event.event_id, 0)
    spy_consumer_store.assert_number_unique_events(1)
    spy_consumer_store.assert_first_event(event)
    spy_consumer_store.assert_count_by_event_id(event.event_id, 1)
Exemple #6
0
def test_should_publish_consume_and_retry_event_with_two_handlers_from_rabbitmq(
):

    spy_consumer_1 = SpyEvents()
    spy_consumer_2 = SpyEvents()

    def assert_consumer_1(event: Event) -> Result[bool, Error]:
        spy_consumer_1.append(event)
        return isSuccess

    def assert_consumer_2(event: Event) -> Result[bool, Error]:
        spy_consumer_2.append(event)
        return isSuccess

    event = EventUserCreatedMother.random()
    subscribers = [
        EventSubscriber(
            event_name=event.event_name,
            event_version=event.event_version,
            handlers=[assert_consumer_1, assert_consumer_2],
        )
    ]

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_subscribers(subscribers)

    bus = RabbitMqEventBusMother.default()
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.default()
    consumer.add_subscribers(subscribers)
    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer.clear()

    spy_consumer_1.assert_number_unique_events(1)
    spy_consumer_1.assert_first_event(event)
    spy_consumer_1.assert_count_by_event_id(event.event_id, 1)

    spy_consumer_2.assert_number_unique_events(1)
    spy_consumer_2.assert_first_event(event)
    spy_consumer_2.assert_count_by_event_id(event.event_id, 1)
Exemple #7
0
def test_should_publish_consume_with_event_handler_with_default_parameters_when_success_consumer(
    given_any_petisco
):
    spy = SpyEvents()

    @event_handler()
    def assert_consumer(event: Event) -> Result[bool, Error]:
        spy.append(event)
        return isSuccess

    event = EventUserCreatedMother.random()
    subscribers = [
        EventSubscriber(
            event_name=event.event_name,
            event_version=event.event_version,
            handlers=[assert_consumer],
        )
    ]

    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_subscribers(subscribers)

    bus = RabbitMqEventBusMother.default()
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.without_retry()
    consumer.add_subscribers(subscribers)
    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer.clear()

    spy.assert_number_unique_events(1)
    spy.assert_first_event(event)
    spy.assert_count_by_event_id(event.event_id, 1)
Exemple #8
0
def test_should_recover_from_connection_error_when_publish_an_event(
    make_user_created_event,
):
    connector = RabbitMqConnector()
    original_wait_seconds_retry = connector.wait_seconds_retry
    connector.wait_seconds_retry = 0.1

    configurer = RabbitMqEventConfigurerMother.default(connector)

    event = make_user_created_event()

    configurer.configure_event(event)

    bus = RabbitMqEventBusMother.default(connector)

    connection = connector.get_connection(DEFAULT_EXCHANGE_NAME)

    connection.close()

    bus.publish(event)

    connector.wait_seconds_retry = original_wait_seconds_retry

    configurer.clear()
def test_should_publish_consume_from_store_queue_from_rabbitmq_with_stop_and_start():

    spy = SpyEvents()

    def assert_consumer_event_store(event: Event) -> BoolResult:
        spy.append(event)
        return isSuccess

    bus = RabbitMqEventBusMother.default()

    first_event = EventUserCreatedMother.random()
    configurer = RabbitMqEventConfigurerMother.default()
    configurer.configure_event(first_event)

    # First Start & Stop
    bus.publish(first_event)
    first_consumer = RabbitMqEventConsumerMother.default()
    first_consumer.add_handler_on_store(assert_consumer_event_store)
    first_consumer.start()
    sleep(1.0)
    first_consumer.stop()

    # Second Start & Stop
    second_event = EventUserCreatedMother.random()
    second_consumer = RabbitMqEventConsumerMother.default()
    second_consumer.add_handler_on_store(assert_consumer_event_store)
    second_consumer.start()
    bus.publish(second_event)
    sleep(1.0)
    second_consumer.stop()

    configurer.clear()

    spy.assert_number_unique_events(2)
    spy.assert_count_by_event_id(first_event.event_id, 1)
    spy.assert_count_by_event_id(second_event.event_id, 1)