Пример #1
0
        public void ManagesMultipleAmqpDataSegmentsByCopyingEagerly()
        {
            byte[] firstSegment  = new byte[] { 1, 2, 3 };
            byte[] secondSegment = new byte[] { 4, 5, 6 };

            var message = new AmqpMessageBody(MessageBody.FromDataSegments(new[]
            {
                new Data {
                    Value = new ArraySegment <byte>(firstSegment)
                }, new Data {
                    Value = new ArraySegment <byte>(secondSegment)
                }
            }));

            message.TryGetData(out var body);
            var firstSegmentBeforeConversion  = body.ElementAt(0);
            var secondSegmentBeforeConversion = body.ElementAt(1);

            ReadOnlyMemory <byte> fromReadOnlyMemorySegments = MessageBody.FromReadOnlyMemorySegments(body);
            ReadOnlyMemory <byte> convertedASecondTime       = MessageBody.FromReadOnlyMemorySegments(body);

            var firstSegmentAfterConversion  = body.ElementAt(0);
            var secondSegmentAfterConversion = body.ElementAt(1);

            Assert.IsFalse(firstSegment.Equals(firstSegmentBeforeConversion));
            Assert.IsFalse(secondSegment.Equals(secondSegmentBeforeConversion));

            Assert.IsFalse(firstSegment.Equals(firstSegmentAfterConversion));
            Assert.IsFalse(secondSegment.Equals(secondSegmentAfterConversion));

            Assert.AreEqual(new byte[] { 1, 2, 3, 4, 5, 6 }, fromReadOnlyMemorySegments.ToArray());
            Assert.IsTrue(fromReadOnlyMemorySegments.Equals(convertedASecondTime));
        }
Пример #2
0
        public void ManagesMultipleReadOnlyMemoriesByCopyingOnConversion()
        {
            ReadOnlyMemory <byte> firstSegment  = new byte[] { 1, 2, 3 };
            ReadOnlyMemory <byte> secondSegment = new byte[] { 4, 5, 6 };

            var message = new AmqpMessageBody(MessageBody.FromReadOnlyMemorySegments(new[] { firstSegment, secondSegment }));

            message.TryGetData(out var body);
            var firstSegmentBeforeConversion  = body.ElementAt(0);
            var secondSegmentBeforeConversion = body.ElementAt(1);

            ReadOnlyMemory <byte> fromReadOnlyMemorySegments = MessageBody.FromReadOnlyMemorySegments(body);
            ReadOnlyMemory <byte> convertedASecondTime       = MessageBody.FromReadOnlyMemorySegments(body);

            var firstSegmentAfterConversion  = body.ElementAt(0);
            var secondSegmentAfterConversion = body.ElementAt(1);

            Assert.IsTrue(firstSegment.Equals(firstSegmentBeforeConversion));
            Assert.IsTrue(secondSegment.Equals(secondSegmentBeforeConversion));

            Assert.IsFalse(firstSegment.Equals(firstSegmentAfterConversion));
            Assert.IsFalse(secondSegment.Equals(secondSegmentAfterConversion));

            Assert.AreEqual(new byte[] { 1, 2, 3, 4, 5, 6 }, fromReadOnlyMemorySegments.ToArray());
            Assert.IsTrue(fromReadOnlyMemorySegments.Equals(convertedASecondTime));
        }
Пример #3
0
        public void ManagesMultipleAmqpDataSegmentsByCopyingEagerly()
        {
            var firstSegment     = new byte[] { 1, 2, 3 };
            var secondSegment    = new byte[] { 4, 5, 6 };
            var allSegmentsArray = new byte[] { 1, 2, 3, 4, 5, 6 };

            var message = new AmqpMessageBody(MessageBody.FromDataSegments(new[]
            {
                new Data {
                    Value = new ArraySegment <byte>(firstSegment)
                },
                new Data {
                    Value = new ArraySegment <byte>(secondSegment)
                }
            }));

            message.TryGetData(out var body);

            var firstSegmentBeforeConversion  = body.ElementAt(0);
            var secondSegmentBeforeConversion = body.ElementAt(1);
            var fromReadOnlyMemorySegments    = (ReadOnlyMemory <byte>)MessageBody.FromReadOnlyMemorySegments(body);
            var convertedASecondTime          = (ReadOnlyMemory <byte>)MessageBody.FromReadOnlyMemorySegments(body);
            var firstSegmentAfterConversion   = body.ElementAt(0);
            var secondSegmentAfterConversion  = body.ElementAt(1);

            Assert.That(firstSegmentBeforeConversion, Is.Not.EqualTo(firstSegment), "The first segment should not match before conversion.");
            Assert.That(secondSegmentBeforeConversion, Is.Not.EqualTo(secondSegment), "The second segment should not match before conversion.");
            Assert.That(firstSegmentAfterConversion, Is.Not.EqualTo(firstSegment), "The first segment should not match after conversion.");
            Assert.That(secondSegmentAfterConversion, Is.Not.EqualTo(secondSegment), "The second segment should not match after conversion.");
            Assert.That(fromReadOnlyMemorySegments.ToArray(), Is.EquivalentTo(allSegmentsArray), "The unified segments should match.");
            Assert.That(convertedASecondTime, Is.EqualTo(fromReadOnlyMemorySegments), "The unified segments should match when converted a second time.");
        }
Пример #4
0
        public void ManagesSingleReadOnlyMemoryWithoutCopying()
        {
            var singleReadOnlyMemory = new ReadOnlyMemory <byte>(new byte[] { 1, 2, 3 });
            var message = new AmqpMessageBody(MessageBody.FromReadOnlyMemorySegment(singleReadOnlyMemory));

            message.TryGetData(out var body);
            var fromReadOnlyMemorySegments = (ReadOnlyMemory <byte>)MessageBody.FromReadOnlyMemorySegments(body);

            Assert.That(fromReadOnlyMemorySegments, Is.EqualTo(singleReadOnlyMemory));
        }
Пример #5
0
        public void ManagesSingleReadOnlyMemoryWithoutCopying()
        {
            ReadOnlyMemory <byte> singleReadOnlyMemory = new byte[] { 1, 2, 3 };

            var message = new AmqpMessageBody(MessageBody.FromReadOnlyMemorySegment(singleReadOnlyMemory));

            message.TryGetData(out var body);
            ReadOnlyMemory <byte> fromReadOnlyMemorySegments = MessageBody.FromReadOnlyMemorySegments(body);

            Assert.IsTrue(singleReadOnlyMemory.Equals(fromReadOnlyMemorySegments));
        }
Пример #6
0
        /// <summary>
        /// Creates a new message from the specified received message by copying the properties.
        /// </summary>
        /// <param name="receivedMessage">The received message to copy the data from.</param>
        public ServiceBusMessage(ServiceBusReceivedMessage receivedMessage)
        {
            Argument.AssertNotNull(receivedMessage, nameof(receivedMessage));

            AmqpMessageBody body = null;

            if (receivedMessage.AmqpMessage.Body.TryGetData(out IEnumerable <ReadOnlyMemory <byte> > dataBody))
            {
                body = AmqpMessageBody.FromData(MessageBody.FromReadOnlyMemorySegments(dataBody));
            }
            else if (receivedMessage.AmqpMessage.Body.TryGetValue(out object valueBody))
            {
                body = AmqpMessageBody.FromValue(valueBody);
            }
            else if (receivedMessage.AmqpMessage.Body.TryGetSequence(out IEnumerable <IList <object> > sequenceBody))
            {
                body = AmqpMessageBody.FromSequence(sequenceBody);
            }
            else
            {
                throw new NotSupportedException($"{receivedMessage.AmqpMessage.Body.BodyType} is not a supported message body type.");
            }

            AmqpMessage = new AmqpAnnotatedMessage(body);

            // copy properties
            AmqpMessageProperties properties         = AmqpMessage.Properties;
            AmqpMessageProperties receivedProperties = receivedMessage.AmqpMessage.Properties;

            properties.MessageId          = receivedProperties.MessageId;
            properties.UserId             = receivedProperties.UserId;
            properties.To                 = receivedProperties.To;
            properties.Subject            = receivedProperties.Subject;
            properties.ReplyTo            = receivedProperties.ReplyTo;
            properties.CorrelationId      = receivedProperties.CorrelationId;
            properties.ContentType        = receivedProperties.ContentType;
            properties.ContentEncoding    = receivedProperties.ContentEncoding;
            properties.AbsoluteExpiryTime = receivedProperties.AbsoluteExpiryTime;
            properties.CreationTime       = receivedProperties.CreationTime;
            properties.GroupId            = receivedProperties.GroupId;
            properties.GroupSequence      = receivedProperties.GroupSequence;
            properties.ReplyToGroupId     = receivedProperties.ReplyToGroupId;

            // copy header except for delivery count which should be set to null
            AmqpMessageHeader header         = AmqpMessage.Header;
            AmqpMessageHeader receivedHeader = receivedMessage.AmqpMessage.Header;

            header.DeliveryCount = null;
            header.Durable       = receivedHeader.Durable;
            header.Priority      = receivedHeader.Priority;
            header.TimeToLive    = receivedHeader.TimeToLive;
            header.FirstAcquirer = receivedHeader.FirstAcquirer;

            // copy message annotations except for broker set ones
            foreach (KeyValuePair <string, object> kvp in receivedMessage.AmqpMessage.MessageAnnotations)
            {
                if (kvp.Key == AmqpMessageConstants.LockedUntilName || kvp.Key == AmqpMessageConstants.SequenceNumberName ||
                    kvp.Key == AmqpMessageConstants.DeadLetterSourceName || kvp.Key == AmqpMessageConstants.EnqueueSequenceNumberName ||
                    kvp.Key == AmqpMessageConstants.EnqueuedTimeUtcName || kvp.Key == AmqpMessageConstants.MessageStateName)
                {
                    continue;
                }
                AmqpMessage.MessageAnnotations.Add(kvp.Key, kvp.Value);
            }

            // copy delivery annotations
            foreach (KeyValuePair <string, object> kvp in receivedMessage.AmqpMessage.DeliveryAnnotations)
            {
                AmqpMessage.DeliveryAnnotations.Add(kvp.Key, kvp.Value);
            }

            // copy footer
            foreach (KeyValuePair <string, object> kvp in receivedMessage.AmqpMessage.Footer)
            {
                AmqpMessage.Footer.Add(kvp.Key, kvp.Value);
            }

            // copy application properties except for broker set ones
            foreach (KeyValuePair <string, object> kvp in receivedMessage.AmqpMessage.ApplicationProperties)
            {
                if (kvp.Key == AmqpMessageConstants.DeadLetterReasonHeader || kvp.Key == AmqpMessageConstants.DeadLetterErrorDescriptionHeader)
                {
                    continue;
                }
                AmqpMessage.ApplicationProperties.Add(kvp.Key, kvp.Value);
            }
        }
Пример #7
0
 /// <summary>
 /// Creates a new message from the specified payload.
 /// </summary>
 /// <param name="body">The payload of the message represented as bytes.</param>
 internal ServiceBusReceivedMessage(ReadOnlyMemory <byte> body)
     : this(new AmqpAnnotatedMessage(new AmqpMessageBody(MessageBody.FromReadOnlyMemorySegments(new ReadOnlyMemory <byte>[] { body }))))
 {
 }