public void GetPartitionPropertiesAsyncDetectsAnEmbeddedAmqpErrorForOperationCanceled() { var eventHubName = "myName"; var partitionId = "Barney"; 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 <EventHubTokenCredential>(Mock.Of <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))); mockConverter .Setup(converter => converter.CreatePartitionPropertiesRequest(It.Is <string>(value => value == eventHubName), It.Is <string>(value => value == partitionId), It.Is <string>(value => value == tokenValue))) .Returns(default(AmqpMessage)); mockScope .Setup(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>())) .Throws(embeddedException); var client = new InjectableMockClient("my.eventhub.com", eventHubName, mockCredential.Object, new EventHubConnectionOptions(), mockScope.Object, mockConverter.Object); Assert.That(async() => await client.GetPartitionPropertiesAsync(partitionId, retryPolicy, cancellationSource.Token), Throws.InstanceOf <OperationCanceledException>()); mockScope.Verify(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>()), Times.Once()); }
public void GetPartitionPropertiesAsyncAppliesTheRetryPolicyForAmqpErrors(EventHubsRetryOptions retryOptions) { var eventHubName = "myName"; var partitionId = "Barney"; 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 <EventHubTokenCredential>(Mock.Of <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))); mockConverter .Setup(converter => converter.CreatePartitionPropertiesRequest(It.Is <string>(value => value == eventHubName), It.Is <string>(value => value == partitionId), It.Is <string>(value => value == tokenValue))) .Returns(default(AmqpMessage)); mockScope .Setup(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>())) .Throws(retriableException); var client = new InjectableMockClient("my.eventhub.com", eventHubName, mockCredential.Object, new EventHubConnectionOptions(), mockScope.Object, mockConverter.Object); Assert.That(async() => await client.GetPartitionPropertiesAsync(partitionId, retryPolicy, cancellationSource.Token), Throws.InstanceOf(retriableException.GetType())); mockScope.Verify(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>()), Times.Exactly(1 + retryOptions.MaximumRetries)); }
public void GetPropertiesAsyncConsidersOperationCanceledExceptionAsRetriable(EventHubsRetryOptions retryOptions) { var eventHubName = "myName"; var tokenValue = "123ABC"; var retryPolicy = new BasicRetryPolicy(retryOptions); var retriableException = new OperationCanceledException(); var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <EventHubTokenCredential>(Mock.Of <TokenCredential>(), "{namespace}.servicebus.windows.net"); 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))); mockConverter .Setup(converter => converter.CreateEventHubPropertiesRequest(It.Is <string>(value => value == eventHubName), It.Is <string>(value => value == tokenValue))) .Returns(default(AmqpMessage)); mockScope .Setup(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>())) .Throws(retriableException); var client = new InjectableMockClient("my.eventhub.com", eventHubName, mockCredential.Object, new EventHubConnectionOptions(), mockScope.Object, mockConverter.Object); Assert.That(async() => await client.GetPropertiesAsync(retryPolicy, cancellationSource.Token), Throws.InstanceOf(retriableException.GetType())); mockScope.Verify(scope => scope.OpenManagementLinkAsync(It.IsAny <TimeSpan>(), It.IsAny <CancellationToken>()), Times.Exactly(1 + retryOptions.MaximumRetries)); }
public void GetPartitionPropertiesAsyncCreatesTheRequest() { var eventHubName = "myName"; var partitionId = "Barney"; var tokenValue = "123ABC"; var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <EventHubTokenCredential>(Mock.Of <TokenCredential>()); 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))) .Verifiable(); mockConverter .Setup(converter => converter.CreatePartitionPropertiesRequest(It.Is <string>(value => value == eventHubName), It.Is <string>(value => value == partitionId), It.Is <string>(value => value == tokenValue))) .Returns(default(AmqpMessage)) .Callback(() => cancellationSource.Cancel()) .Verifiable(); // Because the AMQP library is not friendly to mocking, only the path up to conversion can be tested without external // dependencies. To ensure that execution stops after that point, the converter triggers cancellation that should take // place immediately following the conversion and result in a well-known exception. var client = new InjectableMockClient("my.eventhub.com", eventHubName, mockCredential.Object, new EventHubConnectionOptions(), null, mockConverter.Object); Assert.That(async() => await client.GetPartitionPropertiesAsync(partitionId, Mock.Of <EventHubsRetryPolicy>(), cancellationSource.Token), Throws.InstanceOf <TaskCanceledException>()); mockCredential.VerifyAll(); mockConverter.VerifyAll(); }
public void GetPropertiesAsyncCreatesTheRequest() { var eventHubName = "myName"; var tokenValue = "123ABC"; var mockConverter = new Mock <AmqpMessageConverter>(); var mockCredential = new Mock <EventHubTokenCredential>(Mock.Of <TokenCredential>()); using var cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(EventHubsTestEnvironment.Instance.TestExecutionTimeLimit); mockCredential .Setup(credential => credential.GetTokenAsync(It.IsAny <TokenRequestContext>(), It.Is <CancellationToken>(value => value == cancellationSource.Token))) .Returns(new ValueTask <AccessToken>(new AccessToken(tokenValue, DateTimeOffset.MaxValue))) .Verifiable(); mockConverter .Setup(converter => converter.CreateEventHubPropertiesRequest(It.Is <string>(value => value == eventHubName), It.Is <string>(value => value == tokenValue))) .Returns(default(AmqpMessage)) .Verifiable(); // Because the AMQP library is not friendly to mocking, only the path up to conversion can be tested without external // dependencies. To ensure that execution stops after that point, the mock retry policy specifies an immediate timeout. var client = new InjectableMockClient("my.eventhub.com", eventHubName, TimeSpan.FromDays(1), mockCredential.Object, new EventHubConnectionOptions(), null, mockConverter.Object); Assert.That(async() => await client.GetPropertiesAsync(Mock.Of <EventHubsRetryPolicy>(), cancellationSource.Token), Throws.InstanceOf <TimeoutException>()); mockCredential.VerifyAll(); mockConverter.VerifyAll(); }