Esempio n. 1
0
def test_handle_with_no_context():
    """
    Test that when no context is added the dispatcher behaves sanely.

    """
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph

    # remove the sqs_message_context from the graph so we can test the dispatcher
    # defaulting logic
    graph._registry.entry_points.pop("sqs_message_context")

    content = dict(bar="baz")
    result = graph.sqs_message_dispatcher.handle_message(
        message=SQSMessage(
            consumer=None,
            content=content,
            media_type=DerivedSchema.MEDIA_TYPE,
            message_id=MESSAGE_ID,
            receipt_handle=None,
        ),
        bound_handlers=daemon.bound_handlers,
    )

    assert_that(result, is_(equal_to(True)))
    assert_that(graph.sqs_message_dispatcher.sqs_message_context(content), is_(equal_to(dict())))
Esempio n. 2
0
def test_nack_with_visibility_timeout_via_exception():
    """
    Consumer raises Nack; calls nack with visibility timeout

    """
    visibility_timeout_seconds = 2
    graph = ExampleDaemon.create_for_testing().graph
    message = SQSMessage(
        consumer=graph.sqs_consumer,
        content=None,
        media_type=DerivedSchema.MEDIA_TYPE,
        message_id=MESSAGE_ID,
        receipt_handle=RECEIPT_HANDLE,
    )
    with patch.object(graph.sqs_consumer.backoff_policy,
                      "visibility_timeout_seconds",
                      visibility_timeout_seconds):
        try:
            with message:
                raise Nack(visibility_timeout_seconds)
        except Nack:
            pass

    graph.sqs_consumer.sqs_client.change_message_visibility.assert_called_with(
        QueueUrl="queue",
        ReceiptHandle=RECEIPT_HANDLE,
        VisibilityTimeout=visibility_timeout_seconds,
    )
Esempio n. 3
0
 def setup(self):
     self.graph = ExampleDaemon.create_for_testing().graph
     self.opaque = self.graph.opaque
     self.message = SQSMessage(
         consumer=self.graph.sqs_consumer,
         content=None,
         media_type=DerivedSchema.MEDIA_TYPE,
         message_id=MESSAGE_ID,
         receipt_handle=RECEIPT_HANDLE,
     )
     self.graph.sqs_consumer.sqs_client.reset_mock()
 def setup(self):
     self.graph = ExampleDaemon.create_for_testing().graph
     self.opaque = self.graph.opaque
     self.message = SQSMessage(
         consumer=self.graph.sqs_consumer,
         content=None,
         media_type=DerivedSchema.MEDIA_TYPE,
         message_id=MESSAGE_ID,
         receipt_handle=RECEIPT_HANDLE,
     )
     self.graph.sqs_consumer.sqs_client.reset_mock()
Esempio n. 5
0
 def setup(self):
     self.graph = ExampleDaemon.create_for_testing().graph
     self.message = SQSMessage(
         consumer=self.graph.sqs_consumer,
         content=dict(
             opaque_data=dict(foo="bar", ),
             uri=MESSAGE_URI,
         ),
         media_type=None,
         message_id=MESSAGE_ID,
         receipt_handle=None,
     )
 def setup(self):
     self.graph = ExampleDaemon.create_for_testing().graph
     self.message = SQSMessage(
         consumer=self.graph.sqs_consumer,
         content=dict(
             opaque_data=dict(
                 foo="bar",
             ),
             uri=MESSAGE_URI,
         ),
         media_type=None,
         message_id=MESSAGE_ID,
         receipt_handle=None,
     )
Esempio n. 7
0
def test_nack_without_visibility_timeout():
    """
    Consumer passes

    """
    graph = ExampleDaemon.create_for_testing().graph
    message = SQSMessage(
        consumer=graph.sqs_consumer,
        content=None,
        media_type=DerivedSchema.MEDIA_TYPE,
        message_id=MESSAGE_ID,
        receipt_handle=RECEIPT_HANDLE,
    )
    message.nack()
    graph.sqs_consumer.sqs_client.change_message_visibility.assert_not_called()
Esempio n. 8
0
def test_trace_message_dispatch():
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph
    dispatcher = graph.sqs_message_dispatcher
    content = dict(bar="baz",
                   uri="http://example.com",
                   opaque_data={
                       "X-Request-Id": "req-id-5678",
                       "x-dynatrace": "tag-5678",
                   })
    message = SQSMessage(
        approximate_receive_count=0,
        consumer=graph.sqs_consumer,
        content=content,
        media_type=DerivedSchema.MEDIA_TYPE,
        message_id="message-id-5678",
        receipt_handle=None,
    )
    graph.sqs_consumer.sqs_client.reset_mock()

    with patch.object(tracing, "oneagent") as oneagent, \
            patch.object(tracing, "ChannelType"), \
            patch.object(tracing, "Channel") as channel, \
            patch.object(tracing, "MessagingDestinationType") as messagingdestinationtype:

        result = dispatcher.handle_message(
            message=message,
            bound_handlers=daemon.bound_handlers,
        )
        assert_that(result.result,
                    equal_to(MessageHandlingResultType.SUCCEEDED))

        sdk = oneagent.get_sdk.return_value

        sdk.create_messaging_system_info.assert_called_once_with(
            "SQS", "queue", messagingdestinationtype.QUEUE,
            channel.return_value)
        sdk.trace_incoming_message_process.assert_called_once_with(
            sdk.create_messaging_system_info.return_value,
            str_tag="tag-5678",
        )
        tracer_manager = sdk.trace_incoming_message_process.return_value
        tracer_manager.__enter__.assert_called_once_with()
        tracer_obj = tracer_manager.__enter__.return_value
        tracer_obj.set_vendor_message_id.assert_called_once_with(
            "message-id-5678")
        tracer_obj.set_correlation_id.assert_called_once_with("req-id-5678")
        tracer_manager.__exit__.assert_called_once_with(None, None, None)
    def setup(self):
        self.daemon = ExampleDaemon.create_for_testing()
        self.graph = self.daemon.graph

        self.dispatcher = self.graph.sqs_message_dispatcher

        self.content = dict(bar="baz", uri="http://example.com")
        self.message = SQSMessage(
            approximate_receive_count=0,
            consumer=self.graph.sqs_consumer,
            content=self.content,
            media_type=DerivedSchema.MEDIA_TYPE,
            message_id=MESSAGE_ID,
            receipt_handle=None,
        )
        self.graph.sqs_consumer.sqs_client.reset_mock()
Esempio n. 10
0
    def setup(self):
        self.daemon = ExampleDaemon.create_for_testing()
        self.graph = self.daemon.graph

        self.dispatcher = self.graph.sqs_message_dispatcher

        self.content = dict(bar="baz", uri="http://example.com")
        self.message = SQSMessage(
            approximate_receive_count=0,
            consumer=self.graph.sqs_consumer,
            content=self.content,
            media_type=DerivedSchema.MEDIA_TYPE,
            message_id=MESSAGE_ID,
            receipt_handle=None,
        )
        self.graph.sqs_consumer.sqs_client.reset_mock()
Esempio n. 11
0
def test_ack():
    """
    Consumer delegates to SQS client.

    """
    graph = ExampleDaemon.create_for_testing().graph
    message = SQSMessage(
        consumer=graph.sqs_consumer,
        content=None,
        media_type=DerivedSchema.MEDIA_TYPE,
        message_id=MESSAGE_ID,
        receipt_handle=RECEIPT_HANDLE,
    )
    message.ack()
    graph.sqs_consumer.sqs_client.delete_message.assert_called_with(
        QueueUrl="queue",
        ReceiptHandle=RECEIPT_HANDLE,
    )
Esempio n. 12
0
def test_consume():
    """
    Consumer delegates to SQS client.

    """
    graph = ExampleDaemon.create_for_testing().graph
    # simulate the response structure
    graph.sqs_consumer.sqs_client.receive_message.return_value = dict(
        Messages=[
            dict(MessageId=MESSAGE_ID,
                 ReceiptHandle=RECEIPT_HANDLE,
                 MD5OfBody="7efaa8404863d47c51ed0e20b9014aec",
                 Body=dumps(
                     dict(Message=dumps(
                         dict(
                             data="data",
                             mediaType=DerivedSchema.MEDIA_TYPE,
                         )), ))),
        ])

    messages = graph.sqs_consumer.consume()

    # SQS should have been called
    graph.sqs_consumer.sqs_client.receive_message.assert_called_with(
        AttributeNames=[
            "ApproximateReceiveCount",
        ],
        QueueUrl="queue",
        MaxNumberOfMessages=10,
        WaitTimeSeconds=1,
    )

    # and response translated properly
    assert_that(messages, has_length(1))
    assert_that(messages[0].consumer, is_(equal_to(graph.sqs_consumer)))
    assert_that(messages[0].message_id, is_(equal_to(MESSAGE_ID)))
    assert_that(messages[0].receipt_handle, is_(equal_to(RECEIPT_HANDLE)))
    assert_that(
        messages[0].content,
        is_(equal_to(dict(
            data="data",
            media_type=DerivedSchema.MEDIA_TYPE,
        ))))
Esempio n. 13
0
def test_handle():
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph

    message = dict(
        opaque_data=dict(foo="bar"),
        uri=MESSAGE_URI,
    )
    with graph.opaque.initialize(graph.sqs_message_context,
                                 message,
                                 message_id=MESSAGE_ID):
        assert_that(
            graph.opaque.as_dict(),
            has_entries(
                dict(
                    foo="bar",
                    message_id=MESSAGE_ID,
                    uri=MESSAGE_URI,
                )))
Esempio n. 14
0
def test_dispatch_by_identity_convention():
    """
    Message dispatch can use this convention.

    """
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph

    media_type = deleted(Foo)

    assert_that(
        graph.pubsub_message_schema_registry.find(media_type).schema,
        is_(instance_of(IdentityMessageSchema)),
    )

    assert_that(
        graph.sqs_message_handler_registry.find(media_type, daemon.bound_handlers),
        is_(equal_to(noop_handler)),
    )
Esempio n. 15
0
def test_handle_with_skipping():
    """
    Test that skipping works

    """
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph

    content = dict(bar="baz")
    result = graph.sqs_message_dispatcher.handle_message(
        message=SQSMessage(
            consumer=None,
            content=content,
            media_type=created("bar"),
            message_id=MESSAGE_ID,
            receipt_handle=None,
        ),
        bound_handlers=daemon.bound_handlers,
    )
    assert_that(result, is_(equal_to(False)))
def test_consume():
    """
    Consumer delegates to SQS client.

    """
    graph = ExampleDaemon.create_for_testing().graph
    # simulate the response structure
    graph.sqs_consumer.sqs_client.receive_message.return_value = dict(Messages=[dict(
        MessageId=MESSAGE_ID,
        ReceiptHandle=RECEIPT_HANDLE,
        MD5OfBody="7efaa8404863d47c51ed0e20b9014aec",
        Body=dumps(dict(
            Message=dumps(dict(
                data="data",
                mediaType=DerivedSchema.MEDIA_TYPE,
            )),
        ))),
    ])

    messages = graph.sqs_consumer.consume()

    # SQS should have been called
    graph.sqs_consumer.sqs_client.receive_message.assert_called_with(
        AttributeNames=[
            "ApproximateReceiveCount",
        ],
        QueueUrl="queue",
        MaxNumberOfMessages=10,
        WaitTimeSeconds=1,
    )

    # and response translated properly
    assert_that(messages, has_length(1))
    assert_that(messages[0].consumer, is_(equal_to(graph.sqs_consumer)))
    assert_that(messages[0].message_id, is_(equal_to(MESSAGE_ID)))
    assert_that(messages[0].receipt_handle, is_(equal_to(RECEIPT_HANDLE)))
    assert_that(messages[0].content, is_(equal_to(dict(
        data="data",
        media_type=DerivedSchema.MEDIA_TYPE,
    ))))
Esempio n. 17
0
def test_handle():
    """
    Test that the dispatcher handles a message and assigns context.

    """
    daemon = ExampleDaemon.create_for_testing()
    graph = daemon.graph

    content = dict(bar="baz")
    sqs_message_context = Mock(return_value=dict())
    with graph.opaque.initialize(sqs_message_context, content):
        result = graph.sqs_message_dispatcher.handle_message(
            message=SQSMessage(
                consumer=None,
                content=content,
                media_type=DerivedSchema.MEDIA_TYPE,
                message_id=MESSAGE_ID,
                receipt_handle=None,
            ),
            bound_handlers=daemon.bound_handlers,
        )

    assert_that(result, is_(equal_to(True)))
    sqs_message_context.assert_called_once_with(content)
Esempio n. 18
0
 def setup(self):
     self.daemon = ExampleDaemon.create_for_testing()
     self.graph = self.daemon.graph
Esempio n. 19
0
def create_daemon():
    return ExampleDaemon.create_for_testing().graph
 def setup(self):
     self.daemon = ExampleDaemon.create_for_testing()
     self.graph = self.daemon.graph
Esempio n. 21
0
 def setup(self):
     self.daemon = ExampleDaemon.create_for_testing()
     self.graph = self.daemon.graph
     self.registry = self.graph.sqs_message_handler_registry