예제 #1
0
        public async Task Deserialize_deserializes_envelope_correctly()
        {
            var       correlationId = Guid.NewGuid();
            var       message       = fixture.Create <BlogPostCreated>();
            var       envelope      = new Envelope(correlationId, message);
            EventData eventData     = await sut.Serialize(envelope);

            Envelope actual = await sut.Deserialize(eventData);

            actual.ShouldBeEquivalentTo(
                envelope, opts => opts.RespectingRuntimeTypes());
        }
예제 #2
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);
        }
        public async Task Send_sends_message_correctly()
        {
            // Arrange
            var message       = fixture.Create <BlogPostCreated>();
            var correlationId = Guid.NewGuid();
            var envelope      = new Envelope(correlationId, message);

            List <EventHubReceiver> receivers = await GetReceivers();

            try
            {
                // Act
                await sut.Send(envelope, CancellationToken.None);

                // Assert
                var waitTime             = TimeSpan.FromSeconds(1);
                Task <EventData>[] tasks = receivers.Select(r => r.ReceiveAsync(waitTime)).ToArray();
                await Task.WhenAll(tasks);

                EventData eventData = tasks.Select(t => t.Result).FirstOrDefault(r => r != null);

                eventData.Should().NotBeNull();
                Envelope actual = await serializer.Deserialize(eventData);

                actual.ShouldBeEquivalentTo(envelope, opts => opts.RespectingRuntimeTypes());
            }
            finally
            {
                // Cleanup
                receivers.ForEach(r => r.Close());
            }
        }
        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);
        }
예제 #5
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());
        }
예제 #6
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());
        }
예제 #7
0
        private async Task ProcessEvent(EventData eventData)
        {
            Envelope envelope = null;

            try
            {
                envelope = _serializer.Deserialize(eventData);
                IDictionary <string, object> properties = eventData.Properties;
                await _processor.Process(envelope, properties, _cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                await HandleExceptionFaultTolerantly(eventData, envelope, exception).ConfigureAwait(false);

                if (exception is TaskCanceledException)
                {
                    throw;
                }
            }
        }
        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());
        }
예제 #9
0
        private async Task ProcessEvent(
            PartitionContext context, EventData eventData)
        {
            Envelope envelope = null;

            try
            {
                envelope = await _serializer.Deserialize(eventData).ConfigureAwait(false);

                await _messageHandler.Handle(envelope, _cancellationToken).ConfigureAwait(false);

                await context.CheckpointAsync(eventData).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                try
                {
                    var exceptionContext = envelope == null
                        ? new MessageProcessingExceptionContext <EventData>(eventData, exception)
                        : new MessageProcessingExceptionContext <EventData>(eventData, envelope, exception);

                    await _exceptionHandler.Handle(exceptionContext).ConfigureAwait(false);

                    if (exceptionContext.Handled)
                    {
                        return;
                    }
                }
                catch (Exception unhandleable)
                {
                    Trace.TraceError(unhandleable.ToString());
                }

                throw;
            }
        }