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_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);
        }
        public async Task Send_sets_partition_key_correctly()
        {
            var eventHubClient         = EventHubClient.CreateFromConnectionString(_connectionString);
            var eventSender            = new EventMessageSender(eventHubClient);
            var sut                    = new EventHubMessageBus(eventSender);
            PartitionedMessage message = new Fixture().Create <PartitionedMessage>();
            IEnumerable <PartitionReceiver> receivers = await GetReceivers(eventHubClient, _consumerGroupName);

            await sut.Send(new Envelope(message), CancellationToken.None);

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

            await eventHubClient.CloseAsync();

            EventData eventData = received.Single();

            eventData.SystemProperties.PartitionKey.Should().Be(message.PartitionKey);
        }
        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());
        }