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 EventHubMessageBus(
            EventMessageSender eventSender,
            EventDataSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            _eventSender = eventSender ?? throw new ArgumentNullException(nameof(eventSender));
        }
        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());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EventHubMessageBus"/> class.
 /// </summary>
 /// <param name="eventSender">An <see cref="EventMessageSender"/>.</param>
 public EventHubMessageBus(EventMessageSender eventSender)
 {
     _eventSender = eventSender ?? throw new ArgumentNullException(nameof(eventSender));
 }