def test_body(size): channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe("MyTopic.Sub.Sub") sent = Message() sent.reply_to = subscription sent.topic = "MyTopic.Sub.Sub" sent.body = bytes(bytearray(range(256)) * int(size)) channel.publish(message=sent) received = channel.consume(timeout=1.0) assert repr(sent.body) == repr(received.body) assert sent.body == received.body channel.close()
def test_amqp_conversion(): sent = Message() sent.created_at = int(now() * 1000) / 1000.0 sent.reply_to = "reply_to" sent.subscription_id = "subscription_id" sent.content_type = ContentType.JSON sent.body = '{"field":"value"}'.encode('latin1') sent.topic = "MyTopic" sent.status = Status( code=StatusCode.FAILED_PRECONDITION, why="Bad Args...", ) sent.metadata = { 'x-b3-sampled': '1', 'x-b3-traceid': 'f047c6f208eb36ab', 'x-b3-flags': '0', 'x-b3-spanid': 'ef81a2f9c261473d', 'x-b3-parentspanid': '0000000000000000' } body = sent.body amqp_message = amqp.Message(channel=None, body=body, **WireV1.to_amqp_properties(sent)) amqp_message.delivery_info = { "routing_key": sent.topic, "consumer_tag": sent.subscription_id, } received = WireV1.from_amqp_message(amqp_message) print(sent.__str__(), received.__str__()) assert str(sent) == str(received) assert sent.created_at == received.created_at assert sent.reply_to == received.reply_to assert sent.subscription_id == received.subscription_id assert sent.content_type == received.content_type assert sent.body == received.body assert sent.status == received.status assert sent.topic == received.topic assert sent.correlation_id == received.correlation_id assert sent.timeout == received.timeout assert sent.metadata == received.metadata
def test_propagation(): topic = "span_test" channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe(topic) tracer = Tracer() with tracer.span(name="span_name") as span: span_id = span.span_id message = Message() message.body = "body".encode('latin1') message.inject_tracing(span) channel.publish(topic=topic, message=message) message2 = channel.consume(timeout=1.0) span_context = message2.extract_tracing() assert span_context.span_id == span_id assert span_context.trace_id == tracer.tracer.span_context.trace_id
def consume_ready(self, timeout=1.0): received_msgs = [] # wait for new message try: stated_at = now() while True: _timeout = max(0.0, stated_at + timeout - now()) msg = self._channel.consume(timeout=_timeout) if msg.status.ok() and msg.has_correlation_id(): cid = msg.correlation_id if cid in self._requests: received_msgs.append( (msg, self._requests[cid]["metadata"])) del self._requests[cid] except socket.timeout: pass # check for timeouted requests for cid in self._requests.keys(): timeouted_msg = self._requests[cid]["msg"] if timeouted_msg.deadline_exceeded(): msg = Message() msg.body = timeouted_msg.body msg.topic = timeouted_msg.topic msg.reply_to = self._subscription msg.timeout = timeouted_msg.timeout metadata = self._requests[cid]["metadata"] del self._requests[cid] self._log.debug("[Retring] metadata={}, cid={}", metadata, msg.correlation_id) self._publish(msg, metadata) if not self._can_request and len(self._requests) <= self._min_requests: self._can_request = True return received_msgs
from __future__ import print_function from is_wire.core import Channel, Subscription, Message # This program shows how to publish a message using # the Intelligent Space framework # Connect to the broker channel = Channel("amqp://10.10.2.23:30000") # Create the message message = Message() message.body = "Teste" # Print the message print ("Sent Message:") print (message.body) # Define topic name Topic= "Aula01" message.topic = Topic # Publish message under the topic channel.publish(message)