def test_should_configure_two_services_without_subscribers():
    spy = SpyEvents()

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

    configurer_service_1 = RabbitMqEventConfigurerMother.with_service(
        "service1")
    configurer_service_1.configure()

    configurer_service_2 = RabbitMqEventConfigurerMother.with_service(
        "service2")
    configurer_service_2.configure()

    event = EventUserCreatedMother.random()
    bus = RabbitMqEventBusMother.with_service("service1")
    bus.publish(event)

    consumer = RabbitMqEventConsumerMother.default()
    consumer.add_handler_on_store(assert_store)
    consumer.start()

    sleep(1.0)

    consumer.stop()
    configurer_service_1.clear()
    configurer_service_2.clear()

    spy.assert_number_unique_events(1)
    spy.assert_first_event(event)
    spy.assert_last_event(event)
    spy.assert_count_by_event_id(event.event_id, 1)
Beispiel #2
0
def test_should_publish_consume_and_retry_event_from_rabbitmq_when_a_queue_is_configured_with_specific_parameters(
    max_retries_allowed, expected_number_event_consumed, simulated_results
):
    spy = SpyEvents()
    spy_specific = SpyEvents()

    def assert_consumer(event: Event) -> Result[bool, Error]:
        spy.append(event)
        result = simulated_results.pop(0)
        return result

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

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

    specific_queue_config = SpecificQueueConfig(
        wildcard="*assert_specific_consumer",
        specific_retry_ttl=50,
        specific_main_ttl=75,
    )
    queue_config = QueueConfigMother.with_specific_queue_config(
        specific_queue_config, default_retry_ttl=100, default_main_ttl=100
    )

    configurer = RabbitMqEventConfigurerMother.with_queue_config(queue_config)
    configurer.configure_subscribers(subscribers)

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

    consumer = RabbitMqEventConsumerMother.with_max_retries(max_retries_allowed)
    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_last_event(event)
    spy.assert_count_by_event_id(event.event_id, expected_number_event_consumed)
    spy_specific.assert_number_unique_events(1)
Beispiel #3
0
def test_should_publish_consume_and_retry_event_from_rabbitmq_when_fail_consumer(
        max_retries_allowed, expected_number_event_consumed,
        simulated_results):
    spy = SpyEvents()

    def assert_consumer(event: Event) -> Result[bool, Error]:
        spy.append(event)
        result = simulated_results.pop(0)
        return result

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

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

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

    consumer = RabbitMqEventConsumerMother.with_max_retries(
        max_retries_allowed)
    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_last_event(event)
    spy.assert_count_by_event_id(event.event_id,
                                 expected_number_event_consumed)