public async Task RetryDelegatingHandler_DoesNotRetryOnNotSupportedException()
        {
            // arrange
            int callCounter = 0;

            PipelineContext contextMock = Substitute.For <PipelineContext>();

            contextMock.ConnectionStatusChangesHandler = new ConnectionStatusChangesHandler(delegate(ConnectionStatus status, ConnectionStatusChangeReason reason) { });
            IDelegatingHandler innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var memoryStream = new NotSeekableStream(new byte[] { 1, 2, 3 });

            using var message = new Message(memoryStream);
            innerHandlerMock
            .SendEventAsync(Arg.Is(message), Arg.Any <CancellationToken>())
            .Returns(t =>
            {
                callCounter++;
                Message m     = t.Arg <Message>();
                Stream stream = m.GetBodyStream();
                byte[] buffer = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubException(TestExceptionMessage, isTransient: true);
            });

            var retryDelegatingHandler = new RetryDelegatingHandler(contextMock, innerHandlerMock);

            // act
            NotSupportedException exception = await retryDelegatingHandler
                                              .SendEventAsync(message, CancellationToken.None)
                                              .ExpectedAsync <NotSupportedException>()
                                              .ConfigureAwait(false);

            // assert
            callCounter.Should().Be(1);
        }
예제 #2
0
        public async Task Retry_MessageHasBeenReadTransientExceptionOccured_Throws()
        {
            int callCounter = 0;

            var contextMock      = Substitute.For <IPipelineContext>();
            var innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var memoryStream     = new NotSeekableStream(new byte[] { 1, 2, 3 });
            var message          = new Message(memoryStream);

            innerHandlerMock.SendEventAsync(Arg.Is(message), Arg.Any <CancellationToken>()).Returns(t =>
            {
                callCounter++;
                var m         = t.Arg <Message>();
                Stream stream = m.GetBodyStream();
                var buffer    = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubClientTransientException("");
            });

            var sut = new RetryDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandlerMock;

            var cancellationToken = new CancellationToken();
            await sut.SendEventAsync(message, cancellationToken).ExpectedAsync <IotHubClientTransientException>();

            Assert.AreEqual(callCounter, 1);
        }
        public async Task Retry_NonSeekableStream_NotSupportedException()
        {
            int callCounter = 0;

            var contextMock      = Substitute.For <IPipelineContext>();
            var innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var memoryStream     = new NotSeekableStream(new byte[] { 1, 2, 3 });
            var message          = new Message(memoryStream);

            innerHandlerMock.SendEventAsync(Arg.Is(message), Arg.Any <CancellationToken>()).Returns(t =>
            {
                callCounter++;
                var m         = t.Arg <Message>();
                Stream stream = m.GetBodyStream();
                var buffer    = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubException(TestExceptionMessage, isTransient: true);
            });

            var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

            var cancellationToken           = new CancellationToken();
            NotSupportedException exception = await sut.SendEventAsync(message, cancellationToken).ExpectedAsync <NotSupportedException>().ConfigureAwait(false);

            Assert.AreEqual(callCounter, 1);
        }
예제 #4
0
        public async Task Retry_OneMessageHasBeenReadTransientExceptionOccured_Throws()
        {
            int callCounter = 0;

            var innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var memoryStream     = new NotSeekableStream(new byte[] { 1, 2, 3 });
            var message          = new Message(memoryStream);
            var messages         = new[] { message };

            innerHandlerMock.SendEventAsync(Arg.Is(messages), Arg.Any <CancellationToken>()).Returns(t =>
            {
                callCounter++;
                Message m     = t.Arg <IEnumerable <Message> >().First();
                Stream stream = m.GetBodyStream();
                var buffer    = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubClientTransientException("");
            });

            var sut = new RetryDelegatingHandler(innerHandlerMock);
            var cancellationToken = new CancellationToken();
            await sut.SendEventAsync(messages, cancellationToken).ExpectedAsync <IotHubClientTransientException>();

            Assert.AreEqual(callCounter, 1);
        }
예제 #5
0
        public async Task Retry_MessageHasBeenReadTransientExceptionOccured_Throws()
        {
            int callCounter = 0;

            var innerHandlerMock = Substitute.For <IDelegatingHandler>();
            var memoryStream     = new NotSeekableStream(new byte[] { 1, 2, 3 });
            var message          = new Message(memoryStream);

            innerHandlerMock.SendEventAsync(Arg.Is(message)).Returns(t =>
            {
                callCounter++;
                var m         = t.Arg <Message>();
                Stream stream = m.BodyStream;
                var buffer    = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubClientTransientException("");
            });

            var sut = new RetryDelegatingHandler(innerHandlerMock);
            await sut.SendEventAsync(message).ExpectedAsync <IotHubClientTransientException>();

            Assert.AreEqual(callCounter, 1);
        }
        public async Task Retry_MessageHasBeenReadTransientExceptionOccured_Throws()
        {
            int callCounter = 0;

            var innerHandlerMock = Substitute.For<IDelegatingHandler>();
            var memoryStream = new NotSeekableStream(new byte[] {1,2,3});
            var message = new Message(memoryStream);
            innerHandlerMock.SendEventAsync(Arg.Is(message)).Returns(t =>
            {
                callCounter++;
                var m = t.Arg<Message>();
                Stream stream = m.GetBodyStream();
                var buffer = new byte[3];
                stream.Read(buffer, 0, 3);
                throw new IotHubClientTransientException("");
            });

            var sut = new RetryDelegatingHandler(innerHandlerMock);
            await sut.SendEventAsync(message).ExpectedAsync<IotHubClientTransientException>();

            Assert.AreEqual(callCounter, 1);
        }