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);
        }
Esempio n. 2
0
        public async Task RetryDelegatingHandler_SendEventAsyncRetries()
        {
            // arrange
            int callCounter = 0;

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

            innerHandlerMock.SendEventAsync(Arg.Is(message), Arg.Any <CancellationToken>()).Returns(t =>
            {
                callCounter++;

                Message m     = t.Arg <Message>();
                Stream stream = m.GetBodyStream();
                if (callCounter == 1)
                {
                    throw new IotHubException(TestExceptionMessage, isTransient: true);
                }
                byte[] buffer = new byte[3];
                stream.Read(buffer, 0, 3);
                return(TaskHelpers.CompletedTask);;
            });

            var retryDelegatingHandler = new RetryDelegatingHandler(contextMock, innerHandlerMock);

            // act
            await retryDelegatingHandler.SendEventAsync(message, new CancellationToken()).ConfigureAwait(false);

            // assert
            Assert.AreEqual(2, callCounter);
        }