public void ReceiveAsyncDetectsAnEmbeddedAmqpErrorForOperationCanceled() { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var identifier = "cusTOM-1D"; var eventPosition = EventPosition.FromOffset(123); var trackLastEnqueued = false; var invalidateOnSteal = true; var ownerLevel = 123L; var tokenValue = "123ABC"; var retryPolicy = new BasicRetryPolicy(new EventHubsRetryOptions()); var embeddedException = new OperationCanceledException("", new AmqpException(new Error { Condition = AmqpError.ArgumentError })); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); mockCredential .Setup(credential => credential.GetTokenAsync(It.IsAny <TokenRequestContext>(), It.Is <CancellationToken>(value => value == cancellationSource.Token))) .Returns(new ValueTask <AccessToken>(new AccessToken(tokenValue, DateTimeOffset.MaxValue))); mockScope .Setup(scope => scope.OpenConsumerLinkAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <EventPosition>(), It.IsAny <TimeSpan>(), It.IsAny <TimeSpan>(), It.IsAny <uint>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Throws(embeddedException); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, identifier, eventPosition, trackLastEnqueued, invalidateOnSteal, ownerLevel, null, null, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); Assert.That(async() => await consumer.ReceiveAsync(100, null, cancellationSource.Token), Throws.InstanceOf <OperationCanceledException>()); mockScope .Verify(scope => scope.OpenConsumerLinkAsync( It.Is <string>(value => value == consumerGroup), It.Is <string>(value => value == partition), It.Is <EventPosition>(value => value == eventPosition), It.IsAny <TimeSpan>(), It.IsAny <TimeSpan>(), It.IsAny <uint>(), It.IsAny <long?>(), It.Is <long?>(value => value == ownerLevel), It.Is <bool>(value => value == trackLastEnqueued), It.Is <string>(value => value == identifier), It.IsAny <CancellationToken>()), Times.Once()); }
public void ReceiveAsyncAppliesTheRetryPolicyForAmqpErrors(EventHubsRetryOptions retryOptions) { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var eventPosition = EventPosition.FromOffset(123); var trackLastEnqueued = false; var invalidateOnSteal = true; var ownerLevel = 123L; var tokenValue = "123ABC"; var retryPolicy = new BasicRetryPolicy(retryOptions); var retriableException = AmqpError.CreateExceptionForError(new Error { Condition = AmqpError.ServerBusyError }, "dummy"); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); mockCredential .Setup(credential => credential.GetTokenAsync(It.IsAny <TokenRequestContext>(), It.Is <CancellationToken>(value => value == cancellationSource.Token))) .Returns(new ValueTask <AccessToken>(new AccessToken(tokenValue, DateTimeOffset.MaxValue))); mockScope .Setup(scope => scope.OpenConsumerLinkAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <EventPosition>(), It.IsAny <TimeSpan>(), It.IsAny <uint>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())) .Throws(retriableException); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, eventPosition, trackLastEnqueued, invalidateOnSteal, ownerLevel, null, null, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); Assert.That(async() => await consumer.ReceiveAsync(100, null, cancellationSource.Token), Throws.InstanceOf(retriableException.GetType())); mockScope .Verify(scope => scope.OpenConsumerLinkAsync( It.Is <string>(value => value == consumerGroup), It.Is <string>(value => value == partition), It.Is <EventPosition>(value => value == eventPosition), It.IsAny <TimeSpan>(), It.IsAny <uint>(), It.IsAny <long?>(), It.Is <long?>(value => value == ownerLevel), It.Is <bool>(value => value == trackLastEnqueued), It.IsAny <CancellationToken>()), Times.Exactly(1 + retryOptions.MaximumRetries)); }
public void ReceiveAsyncRespectsTheRetryPolicy(RetryOptions retryOptions) { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var eventPosition = EventPosition.FromOffset(123); var options = new EventHubConsumerClientOptions { Identifier = "OMG!" }; var tokenValue = "123ABC"; var retryPolicy = new BasicRetryPolicy(retryOptions); var retriableException = new EventHubsException(true, "Test"); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); mockCredential .Setup(credential => credential.GetTokenAsync(It.IsAny <TokenRequestContext>(), It.Is <CancellationToken>(value => value == cancellationSource.Token))) .Returns(new ValueTask <AccessToken>(new AccessToken(tokenValue, DateTimeOffset.MaxValue))); mockScope .Setup(scope => scope.OpenConsumerLinkAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <EventPosition>(), It.IsAny <EventHubConsumerClientOptions>(), It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>())) .Throws(retriableException); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, eventPosition, options, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); Assert.That(async() => await consumer.ReceiveAsync(100, null, cancellationSource.Token), Throws.InstanceOf(retriableException.GetType())); mockScope .Verify(scope => scope.OpenConsumerLinkAsync( It.Is <string>(value => value == consumerGroup), It.Is <string>(value => value == partition), It.Is <EventPosition>(value => value == eventPosition), It.Is <EventHubConsumerClientOptions>(value => value == options), It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>()), Times.Exactly(1 + retryOptions.MaximumRetries)); }
public void ReceiveAsyncValidatesTheMaximumMessageCount(int count) { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var eventPosition = EventPosition.FromOffset(123); var retryPolicy = new BasicRetryPolicy(new EventHubsRetryOptions()); var retriableException = new EventHubsException(true, "Test"); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, eventPosition, true, null, null, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); Assert.That(async() => await consumer.ReceiveAsync(count, null, cancellationSource.Token), Throws.InstanceOf <ArgumentException>()); }
public void ReceiveAsyncValidatesConnectionClosed() { var endpoint = new Uri("amqps://not.real.com"); var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var eventPosition = EventPosition.FromOffset(123); var options = new EventHubConsumerClientOptions(); var retryPolicy = new BasicRetryPolicy(new EventHubsRetryOptions()); var mockCredential = new EventHubTokenCredential(Mock.Of <TokenCredential>()); var scope = new AmqpConnectionScope(endpoint, endpoint, eventHub, mockCredential, EventHubsTransportType.AmqpTcp, null); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, eventPosition, true, false, null, null, null, scope, Mock.Of <AmqpMessageConverter>(), retryPolicy); scope.Dispose(); Assert.That(async() => await consumer.ReceiveAsync(100, null, CancellationToken.None), Throws.InstanceOf <EventHubsException>().And.Property(nameof(EventHubsException.Reason)).EqualTo(EventHubsException.FailureReason.ClientClosed)); }
public async Task ReceiveAsyncValidatesClosed() { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var eventPosition = EventPosition.FromOffset(123); var options = new EventHubConsumerClientOptions(); var retryPolicy = new BasicRetryPolicy(new EventHubsRetryOptions()); var retriableException = new EventHubsException(true, "Test"); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, eventPosition, true, false, null, null, null, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); await consumer.CloseAsync(cancellationSource.Token); Assert.That(async() => await consumer.ReceiveAsync(100, null, cancellationSource.Token), Throws.InstanceOf <EventHubsException>().And.Property(nameof(EventHubsException.Reason)).EqualTo(EventHubsException.FailureReason.ClientClosed)); }
public void ReceiveAsyncRespectsTheCancellationTokenIfSetWhenCalled() { var eventHub = "eventHubName"; var consumerGroup = "$DEFAULT"; var partition = "3"; var identifier = "cusTOM-1D"; var eventPosition = EventPosition.FromOffset(123); var retryPolicy = new BasicRetryPolicy(new EventHubsRetryOptions()); var retriableException = new EventHubsException(true, "Test"); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <TokenCredential>(); var mockScope = new Mock <AmqpConnectionScope>(); using var cancellationSource = new CancellationTokenSource(); cancellationSource.Cancel(); var consumer = new AmqpConsumer(eventHub, consumerGroup, partition, identifier, eventPosition, true, true, null, null, null, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy); Assert.That(async() => await consumer.ReceiveAsync(100, null, cancellationSource.Token), Throws.InstanceOf <TaskCanceledException>()); }