Пример #1
0
        public void CreateBatchFromEventsWithMultipleEventsAssignsThePartitionKeyToBodyMessages()
        {
            var partitionKey = "sOmE-kEY";
            var firstEvent   = new EventData(new byte[] { 0x11, 0x22, 0x33 });
            var secondEvent  = new EventData(new byte[] { 0x44, 0x55, 0x66 });
            var events       = new[] { firstEvent, secondEvent };
            var converter    = new AmqpMessageConverter();

            using var message = converter.CreateBatchFromEvents(events, partitionKey);
            Assert.That(message, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(message.DataBody, Is.Not.Null, "The batch envelope should a body.");

            var messageData = message.DataBody.ToList();

            Assert.That(messageData.Count, Is.EqualTo(events.Length), "The batch envelope should contain each batch event in the body.");

            for (var index = 0; index < events.Length; ++index)
            {
                var eventMessage = converter.CreateMessageFromEvent(events[index]);
                eventMessage.Batchable = true;
                eventMessage.MessageAnnotations.Map[AmqpAnnotation.PartitionKey] = partitionKey;

                using var memoryStream = new MemoryStream();
                using var eventStream  = eventMessage.ToStream();

                eventStream.CopyTo(memoryStream);
                var expected = memoryStream.ToArray();
                var actual   = ((ArraySegment <byte>)messageData[index].Value).ToArray();

                Assert.That(actual, Is.EqualTo(expected), $"The batch body for message { index } should match the serialized event.");
            }
        }
Пример #2
0
        public void CreateBatchFromEventsWithMultipleEventsPopulatesTheEnvelopeBody()
        {
            using var firstEventStream  = new MemoryStream(new byte[] { 0x37, 0x39 });
            using var secondEventStream = new MemoryStream(new byte[] { 0x73, 0x93 });

            var firstEvent = new EventData(new byte[] { 0x11, 0x22, 0x33 })
            {
                Properties = new Dictionary <string, object> {
                    { nameof(MemoryStream), firstEventStream }
                }
            };

            var secondEvent = new EventData(new byte[] { 0x44, 0x55, 0x66 })
            {
                Properties = new Dictionary <string, object> {
                    { nameof(MemoryStream), secondEventStream }
                }
            };

            var events    = new[] { firstEvent, secondEvent };
            var converter = new AmqpMessageConverter();

            using var message = converter.CreateBatchFromEvents(events, null);
            Assert.That(message, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(message.DataBody, Is.Not.Null, "The batch envelope should a body.");

            var messageData = message.DataBody.ToList();

            Assert.That(messageData.Count, Is.EqualTo(events.Length), "The batch envelope should contain each batch event in the body.");

            // Reset the position for the stream properties, so that they
            // can be read for translation again.

            firstEventStream.Position  = 0;
            secondEventStream.Position = 0;

            for (var index = 0; index < events.Length; ++index)
            {
                var eventMessage = converter.CreateMessageFromEvent(events[index]);
                eventMessage.Batchable = true;

                using var memoryStream = new MemoryStream();
                using var eventStream  = eventMessage.ToStream();

                eventStream.CopyTo(memoryStream);
                var expected = memoryStream.ToArray();
                var actual   = ((ArraySegment <byte>)messageData[index].Value).ToArray();

                Assert.That(actual, Is.EqualTo(expected), $"The batch body for message { index } should match the serialized event.");
            }
        }
Пример #3
0
        public void CreateBatchFromEventsWithOneMessagePopulatesEnvelopeProperties(string partitionKey)
        {
            var eventData = new EventData(new byte[] { 0x11, 0x22, 0x33 });
            var converter = new AmqpMessageConverter();

            using var message = converter.CreateBatchFromEvents(new[] { eventData }, partitionKey);
            Assert.That(message, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(message.Batchable, Is.True, "The batch envelope should be set to batchable.");
            Assert.That(message.MessageFormat, Is.EqualTo(AmqpConstants.AmqpBatchedMessageFormat), "The batch envelope should have a batchable format.");
            Assert.That(message.DataBody, Is.Not.Null, "The batch envelope should a body.");
            Assert.That(message.DataBody.ToList().Count, Is.EqualTo(1), "The batch envelope should contain a single event in the body.");
            Assert.That(message.MessageAnnotations.Map.TryGetValue(AmqpAnnotation.PartitionKey, out string partitionKeyAnnotation), Is.EqualTo(!String.IsNullOrEmpty(partitionKey)), "There should be an annotation if a partition key was present.");

            if (!String.IsNullOrEmpty(partitionKey))
            {
                Assert.That(partitionKeyAnnotation, Is.EqualTo(partitionKey), "The partition key annotation should match.");
            }
        }
Пример #4
0
        public void CreateBatchFromEventsValidatesTheSource()
        {
            var converter = new AmqpMessageConverter();

            Assert.That(() => converter.CreateBatchFromEvents(null, "anything"), Throws.ArgumentNullException);
        }