コード例 #1
0
def test_inmemory_messaging_backend():
    consumer_1 = Consumer(
        lambda message: print(f"Hello {message['message']}!"))
    consumer_2 = Consumer(
        lambda message: print(f"Hello {message['message']} 2!"))

    backend = InMemoryMessagingBackend()
    registry = SerializerRegistry(serializer_settings)
    link_synchronously("somequeue", [consumer_1, consumer_2], registry,
                       backend)

    registry = SerializerRegistry(serializer_settings)
    publisher = QueuePublisher(registry, backend=backend)
    publisher.publish("somequeue", {"message": "Mary"})
コード例 #2
0
ファイル: test_publishers.py プロジェクト: Rydra/melange
    def test_publish_a_message_to_a_queue(self):
        backend = a_backend()

        registry = SerializerRegistry(serializer_settings)
        sut = QueuePublisher(backend=backend, serializer_registry=registry)
        sut.publish("queue", {"some_content": "12345"})
        assert_that(backend.publish_to_queue, called().times(1))
コード例 #3
0
    def test_if_a_consumer_raises_an_exception_the_message_is_not_acknowledged(
            self):
        serializer = SerializerStub()

        serialized_event = serializer.serialize(BananaHappened("apple"))
        messages = [
            Message.create(serialized_event, None, serializer.identifier())
            for _ in range(2)
        ]
        backend = a_backend_with_messages(messages)

        consumer = ExceptionaleConsumer()

        registry = SerializerRegistry(serializer_settings)
        sut = SimpleMessageDispatcher(consumer,
                                      serializer_registry=registry,
                                      backend=backend)
        sut.consume_event("queue")
        assert_that(backend.acknowledge, never(called()))
コード例 #4
0
    def test_consume_on_queue_but_no_consumer_interested_in_the_messages(self):
        serializer = SerializerStub()

        serialized_event = serializer.serialize(BananaHappened("apple"))
        messages = [
            Message.create(serialized_event, None, serializer.identifier())
            for _ in range(2)
        ]
        backend = a_backend_with_messages(messages)

        consumer = ProxySpy(NoBananaConsumer())

        registry = SerializerRegistry(serializer_settings)
        sut = SimpleMessageDispatcher(consumer,
                                      serializer_registry=registry,
                                      backend=backend)
        sut.consume_event("queue")

        assert_that(consumer.process, never(called()))
        assert_that(backend.acknowledge, called().times(2))
コード例 #5
0
def test_async_consumer(request):
    # We'll use the ElasticMQ as backend since it works like a real SQS queue
    backend = LocalSQSBackend(host=os.environ.get("SQSHOST"),
                              port=os.environ.get("SQSPORT"))
    queue_name = "testqueue"

    # Initialize the queue
    queue = MessagingBackendFactory(backend).init_queue(queue_name)

    # Delete the queue upon finishing the execution of the test
    def teardown():
        backend.delete_queue(queue)

    request.addfinalizer(teardown)

    # Create a consumer that, upon receiving a message, will set
    # the variable "value set" to later assert that this value
    # has, indeed, been set by the consumer that is running on another thread
    state = State()

    def set_state(message: Dict) -> None:
        state.value_set = message["value"]

    consumer = Consumer(on_message=set_state)
    registry = SerializerRegistry(serializer_settings)
    handler = SimpleMessageDispatcher(consumer, registry, backend=backend)
    # Start the consumer loop thread to run the consumer loop in the background
    threading.Thread(target=lambda: handler.consume_loop(queue_name),
                     daemon=True).start()

    # Publish a message and...
    publisher = QueuePublisher(registry, backend)
    publisher.publish(queue_name, {"value": 1})

    # ...wait until the value is set
    probe = StateProbe(state)
    probe.wait()

    assert_that(state.value_set, is_(1))
コード例 #6
0
from melange.serializers import PickleSerializer, SerializerRegistry


class MyConsumer(SingleDispatchConsumer):
    @consumer
    def on_my_test_message_received(self, event: MyTestMessage) -> None:
        print(event.message)


if __name__ == "__main__":
    serializer_settings = {
        "serializers": {
            "pickle": PickleSerializer
        },
        "serializer_bindings": {
            DomainEvent: "pickle"
        },
    }

    serializer_registry = SerializerRegistry(serializer_settings)

    backend = LocalSQSBackend(host="localhost", port=9324)
    consumer = MyConsumer()
    message_dispatcher = SimpleMessageDispatcher(
        consumer,
        serializer_registry,
        backend=backend,
    )
    print("Consuming...")
    message_dispatcher.consume_loop("melangetutorial-queue")