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));
        }
Example #2
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.");
        }