コード例 #1
0
        public void CloseRespectsTheCancellationToken()
        {
            var consumer = new AmqpEventHubConsumer("aHub", "$DEFAULT", "0", EventPosition.Earliest, new EventHubConsumerOptions(), Mock.Of <AmqpConnectionScope>(), Mock.Of <AmqpMessageConverter>(), Mock.Of <EventHubRetryPolicy>(), null);

            using var cancellationSource = new CancellationTokenSource();

            cancellationSource.Cancel();
            Assert.That(async() => await consumer.CloseAsync(cancellationSource.Token), Throws.InstanceOf <TaskCanceledException>(), "Cancellation should trigger the appropriate exception.");
            Assert.That(consumer.Closed, Is.False, "Cancellation should have interrupted closing and left the consumer in an open state.");
        }
コード例 #2
0
        public async Task CloseMarksTheConsumerAsClosed()
        {
            var consumer = new AmqpEventHubConsumer("aHub", "$DEFAULT", "0", EventPosition.Earliest, new EventHubConsumerOptions(), Mock.Of <AmqpConnectionScope>(), Mock.Of <AmqpMessageConverter>(), Mock.Of <EventHubRetryPolicy>(), null);

            Assert.That(consumer.Closed, Is.False, "The consumer should not be closed on creation");

            await consumer.CloseAsync(CancellationToken.None);

            Assert.That(consumer.Closed, Is.True, "The consumer should be marked as closed after closing");
        }
コード例 #3
0
        public void UpdateRetryPolicyUpdatesTheRetryPolicy()
        {
            var newPolicy = new BasicRetryPolicy(new RetryOptions {
                Delay = TimeSpan.FromMilliseconds(50)
            });
            var consumer = new AmqpEventHubConsumer("aHub", "$DEFAULT", "0", EventPosition.Earliest, new EventHubConsumerOptions(), Mock.Of <AmqpConnectionScope>(), Mock.Of <AmqpMessageConverter>(), Mock.Of <EventHubRetryPolicy>(), null);

            Assert.That(GetActiveRetryPolicy(consumer), Is.Not.SameAs(newPolicy), "The initial policy should be a unique instance");

            consumer.UpdateRetryPolicy(newPolicy);
            Assert.That(GetActiveRetryPolicy(consumer), Is.SameAs(newPolicy), "The updated policy should match");
        }
コード例 #4
0
        public void ReceiveAsyncRespectsTheRetryPolicy(RetryOptions retryOptions)
        {
            var eventHub      = "eventHubName";
            var consumerGroup = "$DEFAULT";
            var partition     = "3";
            var eventPosition = EventPosition.FromOffset(123);
            var options       = new EventHubConsumerOptions {
                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(Task.FromResult(new AccessToken(tokenValue, DateTimeOffset.MaxValue)));

            mockScope
            .Setup(scope => scope.OpenConsumerLinkAsync(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <EventPosition>(),
                       It.IsAny <EventHubConsumerOptions>(),
                       It.IsAny <TimeSpan>(),
                       It.IsAny <CancellationToken>()))
            .Throws(retriableException);

            var consumer = new AmqpEventHubConsumer(eventHub, consumerGroup, partition, eventPosition, options, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy, null);

            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 <EventHubConsumerOptions>(value => value == options),
                        It.IsAny <TimeSpan>(),
                        It.IsAny <CancellationToken>()),
                    Times.Exactly(1 + retryOptions.MaximumRetries));
        }
コード例 #5
0
        public void UpdateRetryPolicyUpdatesTheOperationTimeout()
        {
            var initialPolicy = new BasicRetryPolicy(new RetryOptions {
                TryTimeout = TimeSpan.FromSeconds(17)
            });
            var initialTimeout = initialPolicy.CalculateTryTimeout(0);
            var consumer       = new AmqpEventHubConsumer("aHub", "$DEFAULT", "0", EventPosition.Earliest, new EventHubConsumerOptions(), Mock.Of <AmqpConnectionScope>(), Mock.Of <AmqpMessageConverter>(), initialPolicy, null);

            Assert.That(GetTimeout(consumer), Is.EqualTo(initialTimeout), "The initial timeout should match");

            var newPolicy = new BasicRetryPolicy(new RetryOptions {
                TryTimeout = TimeSpan.FromMilliseconds(50)
            });
            TimeSpan newTimeout = newPolicy.CalculateTryTimeout(0);

            consumer.UpdateRetryPolicy(newPolicy);
            Assert.That(GetTimeout(consumer), Is.EqualTo(newTimeout), "The updated timeout should match");
        }
コード例 #6
0
        public void ReceiveAsyncValidatesTheMaximumMessageCount(int count)
        {
            var eventHub      = "eventHubName";
            var consumerGroup = "$DEFAULT";
            var partition     = "3";
            var eventPosition = EventPosition.FromOffset(123);
            var options       = new EventHubConsumerOptions {
                Identifier = "OMG!"
            };
            var retryPolicy        = new BasicRetryPolicy(new 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();

            var consumer = new AmqpEventHubConsumer(eventHub, consumerGroup, partition, eventPosition, options, mockScope.Object, Mock.Of <AmqpMessageConverter>(), retryPolicy, null);

            Assert.That(async() => await consumer.ReceiveAsync(count, null, cancellationSource.Token), Throws.InstanceOf <ArgumentException>());
        }
コード例 #7
0
 /// <summary>
 ///   Gets the active operation timeout for the given client, using the
 ///   private field.
 /// </summary>
 ///
 private static TimeSpan GetTimeout(AmqpEventHubConsumer target) =>
 (TimeSpan)
 typeof(AmqpEventHubConsumer)
 .GetField("_tryTimeout", BindingFlags.Instance | BindingFlags.NonPublic)
 .GetValue(target);
コード例 #8
0
 /// <summary>
 ///   Gets the active retry policy for the given client, using the
 ///   private field.
 /// </summary>
 ///
 private static EventHubRetryPolicy GetActiveRetryPolicy(AmqpEventHubConsumer target) =>
 (EventHubRetryPolicy)
 typeof(AmqpEventHubConsumer)
 .GetField("_retryPolicy", BindingFlags.Instance | BindingFlags.NonPublic)
 .GetValue(target);
コード例 #9
0
        public void UpdateRetryPolicyValidatesTheRetryPolicy()
        {
            var consumer = new AmqpEventHubConsumer("aHub", "$DEFAULT", "0", EventPosition.Earliest, new EventHubConsumerOptions(), Mock.Of <AmqpConnectionScope>(), Mock.Of <AmqpMessageConverter>(), Mock.Of <EventHubRetryPolicy>(), null);

            Assert.That(() => consumer.UpdateRetryPolicy(null), Throws.ArgumentNullException);
        }