Exemple #1
0
        public void CreateBatchFromMessagesWithMultipleEventsPopulatesTheEnvelopeBody()
        {
            using var firstEventStream  = new MemoryStream(new byte[] { 0x37, 0x39 });
            using var secondEventStream = new MemoryStream(new byte[] { 0x73, 0x93 });

            var converter = new AmqpMessageConverter();

            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 }
                }
            };

            using var firstMessage  = converter.CreateMessageFromEvent(firstEvent);
            using var secondMessage = converter.CreateMessageFromEvent(secondEvent);
            var source = new[] { firstMessage, secondMessage };

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

            var messageData = batchEnvelope.DataBody.ToList();

            Assert.That(messageData.Count, Is.EqualTo(source.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 < source.Length; ++index)
            {
                var eventMessage = source[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.");
            }
        }
Exemple #2
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.");
            }
        }
Exemple #3
0
        public void CreateBatchFromMessagesWithOneEventUsesItForTheEnvelope()
        {
            var converter = new AmqpMessageConverter();
            var body      = new byte[] { 0x11, 0x22, 0x33 };
            var property  = 65;

            var eventData = new EventData(body)
            {
                Properties = new Dictionary <string, object> {
                    { nameof(property), property }
                }
            };

            using var source        = converter.CreateMessageFromEvent(eventData);
            using var batchEnvelope = converter.CreateBatchFromMessages(new[] { source }, "Something");
            Assert.That(batchEnvelope, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(batchEnvelope.DataBody, Is.Not.Null, "The batch envelope should a body.");

            var messageData = batchEnvelope.DataBody.ToList();

            Assert.That(messageData.Count, Is.EqualTo(1), "The batch envelopeshould a single data body.");
            Assert.That(messageData[0].Value, Is.EqualTo(eventData.Body.ToArray()), "The batch envelope data should match the event body.");

            Assert.That(batchEnvelope.ApplicationProperties.Map.TryGetValue(nameof(property), out object propertyValue), Is.True, "The application property should exist in the batch.");
            Assert.That(propertyValue, Is.EqualTo(property), "The application property value should match.");
        }
Exemple #4
0
        public void CreateBatchFromMessagesWithMultipleEventsMessagePopulatesEnvelopeProperties(string partitionKey)
        {
            var converter = new AmqpMessageConverter();

            using var first  = converter.CreateMessageFromEvent(new EventData(new byte[] { 0x11, 0x22, 0x33 }));
            using var second = converter.CreateMessageFromEvent(new EventData(new byte[] { 0x44, 0x55, 0x66 }));
            var source = new[] { first, second };

            using var batchEnvelope = converter.CreateBatchFromMessages(source, partitionKey);
            Assert.That(batchEnvelope, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(batchEnvelope.Batchable, Is.True, "The batch envelope should be marked as batchable.");
            Assert.That(batchEnvelope.MessageFormat, Is.EqualTo(AmqpConstants.AmqpBatchedMessageFormat), "The batch envelope should be marked with a batchable format.");
            Assert.That(batchEnvelope.DataBody, Is.Not.Null, "The batch envelope should a body.");
            Assert.That(batchEnvelope.DataBody.ToList().Count, Is.EqualTo(source.Length), "The batch envelope should contain each batch event in the body.");
            Assert.That(batchEnvelope.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.");
            }
        }
Exemple #5
0
        public void CreateMessageFromEventPopulatesTheBody()
        {
            var body      = new byte[] { 0x11, 0x22, 0x33 };
            var eventData = new EventData(body);
            var converter = new AmqpMessageConverter();

            using var message = converter.CreateMessageFromEvent(eventData);
            Assert.That(message, Is.Not.Null, "The AMQP message should have been created.");
            Assert.That(message.DataBody, Is.Not.Null, "The AMQP message should a body.");

            var messageData = message.DataBody.ToList();

            Assert.That(messageData.Count, Is.EqualTo(1), "The AMQP message should a single data body.");
            Assert.That(messageData[0].Value, Is.EqualTo(eventData.Body.ToArray()), "The AMQP message data should match the event body.");
        }
Exemple #6
0
        public void CreateMessageFromEventProperlySetsThePartitionKeyAnnotation(string partitionKey)
        {
            var body      = new byte[] { 0x11, 0x22, 0x33 };
            var eventData = new EventData(body);
            var converter = new AmqpMessageConverter();

            using var message = converter.CreateMessageFromEvent(eventData, partitionKey);
            Assert.That(message, Is.Not.Null, "The AMQP message should have been created.");
            Assert.That(message.MessageAnnotations.Map.TryGetValue(AmqpAnnotation.PartitionKey, out object annotationPartionKey), Is.EqualTo(!String.IsNullOrEmpty(partitionKey)), "The partition key annotation was not correctly set.");

            if (!String.IsNullOrEmpty(partitionKey))
            {
                Assert.That(annotationPartionKey, Is.EqualTo(partitionKey), "The partition key annotation should match.");
            }
        }
        public void AnEventCanBeTranslatedToItself()
        {
            var sourceEvent = new EventData(new byte[] { 0x11, 0x22, 0x33 })
            {
                Properties = new Dictionary <string, object> {
                    { "Test", 1234 }
                }
            };

            var converter = new AmqpMessageConverter();

            using var message = converter.CreateMessageFromEvent(sourceEvent);
            var eventData = converter.CreateEventFromMessage(message);

            Assert.That(message, Is.Not.Null, "The AMQP message should have been created.");
            Assert.That(eventData, Is.Not.Null, "The translated event should have been created.");
            Assert.That(eventData.IsEquivalentTo(sourceEvent), "The translated event should match the source event.");
        }
Exemple #8
0
        public void CCreateBatchFromMessagesWithOneMessagePopulatesEnvelopeProperties(string partitionKey)
        {
            var eventData = new EventData(new byte[] { 0x11, 0x22, 0x33 });
            var converter = new AmqpMessageConverter();

            using var source        = converter.CreateMessageFromEvent(eventData);
            using var batchEnvelope = converter.CreateBatchFromMessages(new[] { source }, partitionKey);
            Assert.That(batchEnvelope, Is.Not.Null, "The batch envelope should have been created.");
            Assert.That(batchEnvelope.Batchable, Is.True, "The batch envelope should be set to batchable.");
            Assert.That(batchEnvelope.MessageFormat, Is.EqualTo(AmqpConstants.AmqpBatchedMessageFormat), "The batch envelope should have a batchable format.");
            Assert.That(batchEnvelope.DataBody, Is.Not.Null, "The batch envelope should a body.");
            Assert.That(batchEnvelope.DataBody.ToList().Count, Is.EqualTo(1), "The batch envelope should contain a single event in the body.");
            Assert.That(batchEnvelope.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.");
            }
        }
Exemple #9
0
        public void EventDataBatchRespectsTheTryAddCallback()
        {
            var eventLimit = 3;
            var converter  = new AmqpMessageConverter();
            var store      = new List <EventData>();
            var messages   = new List <AmqpMessage>();
            var batch      = EventHubsModelFactory.EventDataBatch(5, store, tryAddCallback: _ => store.Count < eventLimit);

            while (store.Count < eventLimit)
            {
                var eventData = new EventData(new BinaryData("Test"));
                Assert.That(() => batch.TryAdd(eventData), Is.True, $"The batch contains { store.Count } events; adding another should be permitted.");

                messages.Add(converter.CreateMessageFromEvent(eventData));
            }

            Assert.That(store.Count, Is.EqualTo(eventLimit), "The batch should be at its limit.");
            Assert.That(() => batch.TryAdd(new EventData(new BinaryData("Too many"))), Is.False, "The batch is full; it should not be possible to add a new event.");
            Assert.That(() => batch.TryAdd(new EventData(new BinaryData("Too many"))), Is.False, "The batch is full; a second attempt to add a new event should not succeed.");

            Assert.That(store.Count, Is.EqualTo(eventLimit), "The batch should be at its limit after the failed TryAdd attempts.");
            Assert.That(batch.AsReadOnlyCollection <AmqpMessage>().Count, Is.EqualTo(eventLimit), "The messages produced by the batch should match the limit.");
        }
Exemple #10
0
        public void CreateMessageFromEventAllowsNoPartitionKey(string partitionKey)
        {
            var converter = new AmqpMessageConverter();

            Assert.That(() => converter.CreateMessageFromEvent(new EventData(new byte[] { 0x11 }), partitionKey), Throws.Nothing);
        }
Exemple #11
0
        public void CreateMessageFromEventValidatesTheSource()
        {
            var converter = new AmqpMessageConverter();

            Assert.That(() => converter.CreateMessageFromEvent(null), Throws.ArgumentNullException);
        }