public EventMessageProcessorFactory(
            EventDataSerializer serializer,
            IMessageHandler messageHandler,
            IMessageProcessingExceptionHandler <EventData> exceptionHandler,
            CancellationToken cancellationToken)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            if (messageHandler == null)
            {
                throw new ArgumentNullException(nameof(messageHandler));
            }

            if (exceptionHandler == null)
            {
                throw new ArgumentNullException(nameof(exceptionHandler));
            }

            _serializer        = serializer;
            _messageHandler    = messageHandler;
            _exceptionHandler  = exceptionHandler;
            _cancellationToken = cancellationToken;
        }
        public async Task Send_with_envelopes_sets_partition_key_correctly()
        {
            // Arrange
            var eventHubClient = EventHubClient.CreateFromConnectionString(_connectionString);
            var eventSender    = new EventMessageSender(eventHubClient);
            var serializer     = new EventDataSerializer();
            var sut            = new EventHubMessageBus(eventSender);

            string partitionKey = Guid.NewGuid().ToString();
            var    envelopes    = new Fixture()
                                  .Build <PartitionedMessage>()
                                  .With(message => message.PartitionKey, partitionKey)
                                  .CreateMany()
                                  .Select(message => new Envelope(message))
                                  .ToList();

            IEnumerable <PartitionReceiver> receivers = await GetReceivers(eventHubClient, _consumerGroupName);

            // Act
            await sut.Send(envelopes, CancellationToken.None);

            // Assert
            IEnumerable <EventData> received = await ReceiveAll(receivers);

            await eventHubClient.CloseAsync();

            IEnumerable <string> partitionKeys =
                from eventData in received
                select eventData.SystemProperties.PartitionKey;

            partitionKeys.Should().OnlyContain(x => x == partitionKey);
        }
        public async Task Send_sends_message_correctly()
        {
            var eventHubClient = EventHubClient.CreateFromConnectionString(_connectionString);
            var eventSender    = new EventMessageSender(eventHubClient);
            var serializer     = new EventDataSerializer();
            var sut            = new EventHubMessageBus(eventSender);
            var envelope       = new Envelope(
                messageId: Guid.NewGuid(),
                message: new Fixture().Create <Message>(),
                operationId: $"{Guid.NewGuid()}",
                correlationId: Guid.NewGuid(),
                contributor: $"{Guid.NewGuid()}");
            IEnumerable <PartitionReceiver> receivers = await GetReceivers(eventHubClient, _consumerGroupName);

            await sut.Send(envelope, CancellationToken.None);

            IEnumerable <EventData> received = await ReceiveAll(receivers);

            await eventHubClient.CloseAsync();

            received.Should().HaveCount(1);
            Envelope actual = serializer.Deserialize(received.Single());

            actual.Should().BeEquivalentTo(envelope);
        }
Beispiel #4
0
 public EventProcessor(
     EventMessageProcessor processor,
     IEventProcessingExceptionHandler exceptionHandler,
     CancellationToken cancellationToken)
 {
     _serializer        = new EventDataSerializer();
     _processor         = processor;
     _exceptionHandler  = exceptionHandler;
     _cancellationToken = cancellationToken;
 }
Beispiel #5
0
 internal EventMessageProcessor(
     EventDataSerializer serializer,
     IMessageHandler messageHandler,
     IMessageProcessingExceptionHandler <EventData> exceptionHandler,
     CancellationToken cancellationToken)
 {
     _serializer        = serializer;
     _messageHandler    = messageHandler;
     _exceptionHandler  = exceptionHandler;
     _cancellationToken = cancellationToken;
 }
        public EventHubMessageBus(
            EventMessageSender eventSender,
            EventDataSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            _eventSender = eventSender ?? throw new ArgumentNullException(nameof(eventSender));
        }
        public void TestInitialize()
        {
            if (eventHubClient == null)
            {
                Assert.Inconclusive(ConnectionParametersRequired);
            }

            fixture = new Fixture().Customize(new AutoMoqCustomization());
            fixture.Inject(eventHubClient);
            serializer = new EventDataSerializer();
            sut        = new EventHubMessageBus(eventHubClient, serializer);
        }
Beispiel #8
0
        public void Serialize_sets_MessageId_property_correctly(EventDataSerializer sut, Message message)
        {
            var envelope = new Envelope(message);

            EventData eventData = sut.Serialize(envelope);

            string propertyName = nameof(Envelope.MessageId);

            eventData.Properties.Keys.Should().Contain(propertyName);
            object actual = eventData.Properties[propertyName];

            actual.Should().Be(envelope.MessageId);
        }
Beispiel #9
0
        public void Deserialize_creates_new_MessageId_if_property_not_set(
            EventDataSerializer sut, Message message)
        {
            var       envelope  = new Envelope(message);
            EventData eventData = sut.Serialize(envelope);

            eventData.Properties.Remove("MessageId");

            Envelope actual = sut.Deserialize(eventData);

            actual.MessageId.Should().NotBeEmpty();
            sut.Deserialize(eventData).MessageId.Should().NotBe(actual.MessageId);
        }
Beispiel #10
0
        public void Serialize_serializes_message_correctly(Message message, JsonMessageSerializer serializer)
        {
            var sut      = new EventDataSerializer(serializer);
            var envelope = new Envelope(message);

            EventData eventData = sut.Serialize(envelope);

            ArraySegment <byte> body = eventData.Body;
            string value             = Encoding.UTF8.GetString(body.Array, body.Offset, body.Count);
            object actual            = serializer.Deserialize(value);

            actual.Should().BeOfType <Message>();
            actual.Should().BeEquivalentTo(message);
        }
Beispiel #11
0
        public void Deserialize_not_fails_even_if_Contributor_property_not_set(
            EventDataSerializer sut, Message message)
        {
            var       envelope  = new Envelope(message);
            EventData eventData = sut.Serialize(envelope);

            eventData.Properties.Remove("Contributor");

            Envelope actual = null;
            Action   action = () => actual = sut.Deserialize(eventData);

            action.Should().NotThrow();
            actual.Should().BeEquivalentTo(envelope, opts => opts.RespectingRuntimeTypes());
        }
Beispiel #12
0
        public void Deserialize_deserializes_envelope_correctly(
            EventDataSerializer sut,
            Guid messageId,
            Message message,
            string operationId,
            Guid correlationId,
            string contributor)
        {
            var       envelope  = new Envelope(messageId, message, operationId, correlationId, contributor);
            EventData eventData = sut.Serialize(envelope);

            Envelope actual = sut.Deserialize(eventData);

            actual.Should().BeEquivalentTo(envelope, opts => opts.RespectingRuntimeTypes());
        }
Beispiel #13
0
        public void Serialize_sets_Contributor_property_correctly(
            EventDataSerializer sut, Guid messageId, Message message, string contributor)
        {
            var envelope = new Envelope(
                messageId: Guid.NewGuid(),
                message,
                contributor: contributor);

            EventData eventData = sut.Serialize(envelope);

            string propertyName = nameof(Envelope.Contributor);

            eventData.Properties.Keys.Should().Contain(propertyName);
            object actual = eventData.Properties[propertyName];

            actual.Should().BeOfType <string>().Which.Should().Be(contributor);
        }
Beispiel #14
0
        public void Serialize_sets_CorrelationId_property_correctly(
            EventDataSerializer sut, Guid messageId, Message message, Guid correlationId)
        {
            var envelope = new Envelope(
                messageId: Guid.NewGuid(),
                message,
                correlationId: correlationId);

            EventData eventData = sut.Serialize(envelope);

            string propertyName = nameof(Envelope.CorrelationId);

            eventData.Properties.Keys.Should().Contain(propertyName);
            object actual = eventData.Properties[propertyName];

            actual.Should().Be(correlationId);
        }
Beispiel #15
0
        public EventHubMessageBus(
            EventHubClient eventHubClient,
            EventDataSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            if (eventHubClient == null)
            {
                throw new ArgumentNullException(nameof(eventHubClient));
            }

            _eventHubClient = eventHubClient;
            _serializer     = serializer;
        }
        public async Task Send_with_envelopes_sends_partitioned_messages_correctly()
        {
            // Arrange
            var eventHubClient = EventHubClient.CreateFromConnectionString(_connectionString);
            var eventSender    = new EventMessageSender(eventHubClient);
            var serializer     = new EventDataSerializer();
            var sut            = new EventHubMessageBus(eventSender);

            string partitionKey = Guid.NewGuid().ToString();
            var    envelopes    = new Fixture()
                                  .Build <PartitionedMessage>()
                                  .With(message => message.PartitionKey, partitionKey)
                                  .CreateMany()
                                  .Select(message => new Envelope(
                                              messageId: Guid.NewGuid(),
                                              message,
                                              operationId: $"{Guid.NewGuid()}",
                                              correlationId: Guid.NewGuid(),
                                              contributor: $"{Guid.NewGuid()}"))
                                  .ToList();

            IEnumerable <PartitionReceiver> receivers = await GetReceivers(eventHubClient, _consumerGroupName);

            // Act
            await sut.Send(envelopes, CancellationToken.None);

            // Assert
            IEnumerable <EventData> received = await ReceiveAll(receivers);

            await eventHubClient.CloseAsync();

            IEnumerable <Envelope> actual = from eventData in received
                                            select serializer.Deserialize(eventData);

            actual.Should().BeEquivalentTo(
                envelopes,
                opts =>
                opts.WithStrictOrdering()
                .RespectingRuntimeTypes());
        }
 public EventHubMessageBus(
     EventHubClient eventHubClient,
     EventDataSerializer serializer)
     : this(new EventMessageSender(eventHubClient), serializer)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EventMessageSender"/> class.
 /// </summary>
 /// <param name="eventHubClient">An <see cref="EventHubClient"/>.</param>
 public EventMessageSender(EventHubClient eventHubClient)
 {
     _serializer     = new EventDataSerializer();
     _eventHubClient = eventHubClient ?? throw new ArgumentNullException(nameof(eventHubClient));
 }
Beispiel #19
0
 public void TestInitialize()
 {
     fixture           = new Fixture().Customize(new AutoMoqCustomization());
     messageSerializer = new JsonMessageSerializer();
     sut = new EventDataSerializer(messageSerializer);
 }