public void CreateReceiverCreatesDefaultWhenNoOptionsArePassed() { var clientOptions = new EventHubClientOptions { Retry = new ExponentialRetry(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3), 5), DefaultTimeout = TimeSpan.FromHours(24) }; var expectedOptions = new EventReceiverOptions { Retry = clientOptions.Retry, DefaultMaximumReceiveWaitTime = clientOptions.DefaultTimeout }; var expectedPartition = "56767"; var connectionString = "Endpoint=value.com;SharedAccessKeyName=[value];SharedAccessKey=[value];EntityPath=[value]"; var mockClient = new ReadableOptionsMock(connectionString, clientOptions); mockClient.CreateReceiver(expectedPartition); var actualOptions = mockClient.ReceiverOptions; Assert.That(actualOptions, Is.Not.Null, "The receiver options should have been set."); Assert.That(actualOptions.BeginReceivingAt.Offset, Is.EqualTo(expectedOptions.BeginReceivingAt.Offset), "The beginning position to receive should match."); Assert.That(actualOptions.ConsumerGroup, Is.EqualTo(expectedOptions.ConsumerGroup), "The consumer groups should match."); Assert.That(actualOptions.ExclusiveReceiverPriority, Is.EqualTo(expectedOptions.ExclusiveReceiverPriority), "The exclusive priorities should match."); Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match."); Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match."); Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match."); }
public void CloneProducesACopy() { var options = new EventReceiverOptions { ConsumerGroup = "custom$consumer", BeginReceivingAt = EventPosition.FromOffset(65), ExclusiveReceiverPriority = 99, Retry = new ExponentialRetry(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(5), 6), DefaultMaximumReceiveWaitTime = TimeSpan.FromMinutes(65), Identifier = "an_event_receiver" }; var clone = options.Clone(); Assert.That(clone, Is.Not.Null, "The clone should not be null."); Assert.That(clone.ConsumerGroup, Is.EqualTo(options.ConsumerGroup), "The consumer group of the clone should match."); Assert.That(clone.BeginReceivingAt, Is.EqualTo(options.BeginReceivingAt), "The position to begin reading events of the clone should match."); Assert.That(clone.ExclusiveReceiverPriority, Is.EqualTo(options.ExclusiveReceiverPriority), "The exclusive priority of the clone should match."); Assert.That(clone.DefaultMaximumReceiveWaitTime, Is.EqualTo(options.DefaultMaximumReceiveWaitTime), "The default maximum wait time of the clone should match."); Assert.That(clone.Identifier, Is.EqualTo(options.Identifier), "The identifier of the clone should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)clone.Retry, (ExponentialRetry)options.Retry), "The retry of the clone should be considered equal."); Assert.That(clone.Retry, Is.Not.SameAs(options.Retry), "The retry of the clone should be a copy, not the same instance."); }
public void DefaultMaximumReceiveWaitTimeUsesNormalizesValueINotSpecified(int?noTimeoutValue) { var options = new EventReceiverOptions(); var timeoutValue = (noTimeoutValue.HasValue) ? TimeSpan.Zero : (TimeSpan?)null; options.DefaultMaximumReceiveWaitTime = timeoutValue; Assert.That(options.DefaultMaximumReceiveWaitTime, Is.EqualTo(timeoutValue), "The value supplied by the caller should be preserved."); Assert.That(options.MaximumReceiveWaitTimeOrDefault, Is.Null, "The maximum wait value should be normalized to null internally."); }
public void ConstructorSetsThePriority(long?priority) { var options = new EventReceiverOptions { ExclusiveReceiverPriority = priority }; var transportReceiver = new ObservableTransportReceiverMock(); var receiver = new EventReceiver(transportReceiver, "dummy", "0", options); Assert.That(receiver.ExclusiveReceiverPriority, Is.EqualTo(priority)); }
public void ConstructorSetsTheConsumerGroup() { var options = new EventReceiverOptions { ConsumerGroup = "SomeGroup" }; var transportReceiver = new ObservableTransportReceiverMock(); var receiver = new EventReceiver(transportReceiver, "dummy", "0", options); Assert.That(receiver.ConsumerGroup, Is.EqualTo(options.ConsumerGroup)); }
public void ConstructorSetsTheStartingPosition() { var options = new EventReceiverOptions { BeginReceivingAt = EventPosition.FromSequenceNumber(12345, true) }; var transportReceiver = new ObservableTransportReceiverMock(); var receiver = new EventReceiver(transportReceiver, "dummy", "0", options); Assert.That(receiver.StartingPosition, Is.EqualTo(options.BeginReceivingAt)); }
private static async Task CreateSenderAndReceiver() { Console.Write("Creating the Sender and Receivers... "); var partition = (await client.GetPartitionIdsAsync()).First(); var senderOptions = new EventSenderOptions { PartitionId = partition }; var receiverOptions = new EventReceiverOptions { BeginReceivingAt = EventPosition.NewEventsOnly }; sender = client.CreateSender(senderOptions); receiver = client.CreateReceiver(partition, receiverOptions); Console.WriteLine("\tdone"); }
public async Task ReceiveAsyncInvokesTheTransportReceiver() { var options = new EventReceiverOptions { DefaultMaximumReceiveWaitTime = TimeSpan.FromMilliseconds(8) }; var transportReceiver = new ObservableTransportReceiverMock(); var receiver = new EventReceiver(transportReceiver, "dummy", "0", options); var cancellation = new CancellationTokenSource(); var expectedMessageCount = 45; await receiver.ReceiveAsync(expectedMessageCount, null, cancellation.Token); (var actualMessageCount, var actualWaitTime) = transportReceiver.ReceiveCalledWithParameters; Assert.That(actualMessageCount, Is.EqualTo(expectedMessageCount), "The message counts should match."); Assert.That(actualWaitTime, Is.EqualTo(options.DefaultMaximumReceiveWaitTime), "The wait time should match."); }
/// <summary> /// Creates an event receiver responsible for reading <see cref="EventData" /> from a specific Event Hub partition, /// and as a member of a specific consumer group. /// /// A receiver may be exclusive, which asserts ownership over the partition for the consumer /// group to ensure that only one receiver from that group is reading the from the partition. /// These exclusive receivers are sometimes referred to as "Epoch Receivers." /// /// A receiver may also be non-exclusive, allowing multiple receivers from the same consumer /// group to be actively reading events from the partition. These non-exclusive receivers are /// sometimes referred to as "Non-epoch Receivers." /// /// Designating a receiver as exclusive may be specified in the <paramref name="receiverOptions" />. /// By default, receivers are created as non-exclusive. /// </summary> /// /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</param> /// <param name="receiverOptions">The set of options to apply when creating the receiver.</param> /// /// <returns>An event receiver configured in the requested manner.</returns> /// public override EventReceiver CreateReceiver(string partitionId, EventReceiverOptions receiverOptions) { TrackOne.PartitionReceiver CreateReceiverFactory() { var position = new TrackOne.EventPosition { IsInclusive = receiverOptions.BeginReceivingAt.IsInclusive, Offset = receiverOptions.BeginReceivingAt.Offset, SequenceNumber = receiverOptions.BeginReceivingAt.SequenceNumber, EnqueuedTimeUtc = receiverOptions.BeginReceivingAt.EnqueuedTimeUtc }; var trackOneOptions = new TrackOne.ReceiverOptions { Identifier = receiverOptions.Identifier }; PartitionReceiver receiver; if (receiverOptions.ExclusiveReceiverPriority.HasValue) { receiver = TrackOneClient.CreateEpochReceiver(receiverOptions.ConsumerGroup, partitionId, position, receiverOptions.ExclusiveReceiverPriority.Value, trackOneOptions); } else { receiver = TrackOneClient.CreateReceiver(receiverOptions.ConsumerGroup, partitionId, position, trackOneOptions); } (TimeSpan minBackoff, TimeSpan maxBackoff, int maxRetries) = ((ExponentialRetry)receiverOptions.Retry).GetProperties(); receiver.RetryPolicy = new RetryExponential(minBackoff, maxBackoff, maxRetries); return(receiver); } return(new EventReceiver ( new TrackOneEventReceiver(CreateReceiverFactory), TrackOneClient.EventHubName, partitionId, receiverOptions )); }
public void CreateReceiverInvokesTheTransportClient() { var transportClient = new ObservableTransportClientMock(); var client = new InjectableTransportClientMock(transportClient, "Endpoint=sb://not-real.servicebus.windows.net/;SharedAccessKeyName=DummyKey;SharedAccessKey=[not_real];EntityPath=fake"); var expectedOptions = new EventReceiverOptions { Retry = Retry.Default }; var expectedPartition = "2123"; client.CreateReceiver(expectedPartition, expectedOptions); (var actualPartition, var actualOptions) = transportClient.CreateReceiverCalledWith; Assert.That(actualPartition, Is.EqualTo(expectedPartition), "The partition should have been passed."); Assert.That(actualOptions, Is.Not.Null, "The receiver options should have been set."); Assert.That(actualOptions.BeginReceivingAt.Offset, Is.EqualTo(expectedOptions.BeginReceivingAt.Offset), "The beginning position to receive should match."); Assert.That(actualOptions.ConsumerGroup, Is.EqualTo(expectedOptions.ConsumerGroup), "The consumer groups should match."); Assert.That(actualOptions.ExclusiveReceiverPriority, Is.EqualTo(expectedOptions.ExclusiveReceiverPriority), "The exclusive priorities should match."); Assert.That(actualOptions.Identifier, Is.EqualTo(expectedOptions.Identifier), "The identifiers should match."); Assert.That(actualOptions.PrefetchCount, Is.EqualTo(expectedOptions.PrefetchCount), "The prefetch counts should match."); Assert.That(ExponentialRetry.HaveSameConfiguration((ExponentialRetry)actualOptions.Retry, (ExponentialRetry)expectedOptions.Retry), "The retries should match."); Assert.That(actualOptions.MaximumReceiveWaitTimeOrDefault, Is.EqualTo(expectedOptions.MaximumReceiveWaitTimeOrDefault), "The wait times should match."); }
public async Task ReceiverWithOptionsCanReceive() { await using (var scope = await EventHubScope.CreateAsync(4)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); var options = new EventReceiverOptions { ConsumerGroup = "$Default", BeginReceivingAt = EventPosition.NewEventsOnly }; await using (var client = new EventHubClient(connectionString)) { var partition = (await client.GetPartitionIdsAsync()).First(); await using (var receiver = client.CreateReceiver(partition, options)) { Assert.That(async() => await receiver.ReceiveAsync(1, TimeSpan.Zero), Throws.Nothing); } } } }
/// <summary> /// Creates an event receiver, ready to receive broadcasts from the key /// </summary> /// <param name="key">The event key to listen from</param> /// <param name="options">Option flags</param> public EventReceiver(EventKey <T> key, EventReceiverOptions options = EventReceiverOptions.None) : base(key, options) { }
/// <summary> /// Creates an event receiver, ready to receive broadcasts from the key /// </summary> /// <param name="key">The event key to listen from</param> /// <param name="attachedScript">The script from where this receiver is created, useful if we have the ClearEveryFrame option set</param> /// <param name="options">Option flags</param> public EventReceiver(EventKey key, ScriptComponent attachedScript, EventReceiverOptions options = EventReceiverOptions.None) : base(key, attachedScript, options) { }
public override EventReceiver CreateReceiver(string partitionId, EventReceiverOptions receiverOptions) { CreateReceiverCalledWith = (partitionId, receiverOptions); return(default(EventReceiver)); }
/// <summary> /// Creates an event receiver responsible for reading <see cref="EventData" /> from a specific Event Hub partition, /// and as a member of a specific consumer group. /// /// A receiver may be exclusive, which asserts ownership over the partition for the consumer /// group to ensure that only one receiver from that group is reading the from the partition. /// These exclusive receivers are sometimes referred to as "Epoch Receivers." /// /// A receiver may also be non-exclusive, allowing multiple receivers from the same consumer /// group to be actively reading events from the partition. These non-exclusive receivers are /// sometimes referred to as "Non-epoch Receivers." /// /// Designating a receiver as exclusive may be specified in the <paramref name="receiverOptions" />. /// By default, receivers are created as non-exclusive. /// </summary> /// /// <param name="partitionId">The identifier of the Event Hub partition from which events will be received.</param> /// <param name="receiverOptions">The set of options to apply when creating the receiver.</param> /// /// <returns>An event receiver configured in the requested manner.</returns> /// public abstract EventReceiver CreateReceiver(string partitionId, EventReceiverOptions receiverOptions);
public async Task ReceiveCanReadOneEventBatch() { await using (var scope = await EventHubScope.CreateAsync(4)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); var eventBatch = new[] { new EventData(Encoding.UTF8.GetBytes("One")), new EventData(Encoding.UTF8.GetBytes("Two")), new EventData(Encoding.UTF8.GetBytes("Three")) }; var receiverOptions = new EventReceiverOptions { BeginReceivingAt = EventPosition.NewEventsOnly }; await using (var client = new EventHubClient(connectionString)) { var partition = (await client.GetPartitionIdsAsync()).First(); await using (var sender = client.CreateSender(new EventSenderOptions { PartitionId = partition })) await using (var receiver = client.CreateReceiver(partition, receiverOptions)) { // Initiate an operation to force the receiver to connect and set its position at the // end of the event stream. Assert.That(async() => await receiver.ReceiveAsync(1, TimeSpan.Zero), Throws.Nothing); // Send the batch of events. await sender.SendAsync(eventBatch); // Recieve and validate 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. var receivedEvents = new List <EventData>(); var index = 0; while ((receivedEvents.Count < eventBatch.Length) && (++index < 3)) { receivedEvents.AddRange(await receiver.ReceiveAsync(eventBatch.Length + 10, TimeSpan.FromMilliseconds(25))); } index = 0; Assert.That(receivedEvents, Is.Not.Empty, "There should have been a set of events received."); foreach (var receivedEvent in receivedEvents) { Assert.That(receivedEvent.IsEquivalentTo(eventBatch[index]), Is.True, $"The received event at index: { index } did not match the sent batch."); ++index; } Assert.That(index, Is.EqualTo(eventBatch.Length), "The number of received events did not match the batch size."); } } } }