public async Task ProducerDoesNotSendToSpecificPartitionWhenPartitionIdIsNotSpecified(bool nullPartition)
        {
            var partitions = 10;

            await using (var scope = await EventHubScope.CreateAsync(partitions))
            {
                var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName);

                await using (var client = new EventHubClient(connectionString))
                {
                    var producerOptions = new EventHubProducerOptions {
                    };

                    if (nullPartition)
                    {
                        producerOptions.PartitionId = null;
                    }

                    await using (var producer = client.CreateProducer(producerOptions))
                    {
                        var batches      = 30;
                        var partitionIds = await client.GetPartitionIdsAsync();

                        var partitionsCount = 0;
                        var consumers       = new List <EventHubConsumer>();

                        try
                        {
                            for (var index = 0; index < partitions; index++)
                            {
                                consumers.Add(client.CreateConsumer(EventHubConsumer.DefaultConsumerGroup, partitionIds[index], EventPosition.Latest));

                                // Initiate an operation to force the consumer to connect and set its position at the
                                // end of the event stream.

                                await consumers[index].ReceiveAsync(1, TimeSpan.Zero);
                            }

                            // Send the batches of events.

                            for (var index = 0; index < batches; index++)
                            {
                                await producer.SendAsync(new EventData(Encoding.UTF8.GetBytes("It's not healthy to send so many messages")));
                            }

                            // Receive the events; because there is some non-determinism in the messaging flow, the
                            // sent events may not be immediately available.  Allow for a small number of attempts to receive, in order
                            // to account for availability delays.

                            foreach (var consumer in consumers)
                            {
                                var receivedEvents = new List <EventData>();
                                var index          = 0;

                                while (++index < ReceiveRetryLimit)
                                {
                                    receivedEvents.AddRange(await consumer.ReceiveAsync(batches + 10, TimeSpan.FromMilliseconds(25)));
                                }

                                if (receivedEvents.Count > 0)
                                {
                                    partitionsCount++;
                                }
                            }
                        }
                        finally
                        {
                            await Task.WhenAll(consumers.Select(consumer => consumer.CloseAsync()));
                        }

                        Assert.That(partitionsCount, Is.GreaterThan(1));
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 ///   Creates an Event Hub producer responsible for transmitting <see cref="EventData" /> to the
 ///   Event Hub, grouped together in batches.  Depending on the <paramref name="producerOptions"/>
 ///   specified, the producer may be created to allow event data to be automatically routed to an available
 ///   partition or specific to a partition.
 /// </summary>
 ///
 /// <param name="producerOptions">The set of options to apply when creating the producer.</param>
 /// <param name="defaultRetryPolicy">The default retry policy to use if no retry options were specified in the <paramref name="producerOptions" />.</param>
 ///
 /// <returns>An Event Hub producer configured in the requested manner.</returns>
 ///
 public override EventHubProducer CreateProducer(EventHubProducerOptions producerOptions,
                                                 EventHubRetryPolicy defaultRetryPolicy) => throw new NotImplementedException();
 /// <summary>
 ///   Creates an Event Hub producer responsible for transmitting <see cref="EventData" /> to the
 ///   Event Hub, grouped together in batches.  Depending on the <paramref name="producerOptions"/>
 ///   specified, the producer may be created to allow event data to be automatically routed to an available
 ///   partition or specific to a partition.
 /// </summary>
 ///
 /// <param name="producerOptions">The set of options to apply when creating the producer.</param>
 ///
 /// <returns>An Event Hub producer configured in the requested manner.</returns>
 ///
 public abstract EventHubProducer CreateProducer(EventHubProducerOptions producerOptions);
Beispiel #4
0
 /// <summary>
 ///   Creates an Event Hub producer responsible for transmitting <see cref="EventData" /> to the
 ///   Event Hub, grouped together in batches.  Depending on the <paramref name="producerOptions"/>
 ///   specified, the producer may be created to allow event data to be automatically routed to an available
 ///   partition or specific to a partition.
 /// </summary>
 ///
 /// <param name="producerOptions">The set of options to apply when creating the producer.</param>
 /// <param name="defaultRetryPolicy">The default retry policy to use if no retry options were specified in the <paramref name="producerOptions" />.</param>
 ///
 /// <returns>An Event Hub producer configured in the requested manner.</returns>
 ///
 public abstract EventHubProducer CreateProducer(EventHubProducerOptions producerOptions,
                                                 EventHubRetryPolicy defaultRetryPolicy);
 public override EventHubProducer CreateProducer(EventHubProducerOptions producerOptions = default)
 {
     CreateProducerCalledWithOptions = producerOptions;
     return(default(EventHubProducer));
 }