public async Task TransportRouting_TryOpenFailedWithSupportedExceptionTwoTimes_Fail()
        {
            var contextMock           = Substitute.For <IPipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();
            int openCallCounter       = 0;

            innerHandler.OpenAsync(Arg.Any <CancellationToken>()).Returns(async ci =>
            {
                openCallCounter++;
                await Task.Yield();
                throw new TimeoutException();
            });
            contextMock.Get <ITransportSettings[]>().Returns(new[] { amqpTransportSettings, mqttTransportSettings });
            var sut = new ProtocolRoutingDelegatingHandler(contextMock, null);

            sut.ContinuationFactory = (ctx, inner) => innerHandler;
            var cancellationToken = new CancellationToken();

            await sut.OpenAsync(cancellationToken).ExpectedAsync <TimeoutException>().ConfigureAwait(false);

            innerHandler.Received(0).Dispose();

            await sut.OpenAsync(cancellationToken).ExpectedAsync <TimeoutException>().ConfigureAwait(false);

            innerHandler.Received(1).Dispose();

            Assert.AreEqual(2, openCallCounter);
        }
        public async Task TransportRouting_TryOpenFailedWithSupportedExceptionTwoTimes_Fail()
        {
            var contextMock           = Substitute.For <IPipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();

            innerHandler.CloseAsync().Returns(TaskConstants.Completed);
            int openCallCounter = 0;

            innerHandler.OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>()).Returns(async ci =>
            {
                openCallCounter++;
                await Task.Yield();
                throw new TimeoutException();
            });
            contextMock.Get <ITransportSettings[]>().Returns(new[] { amqpTransportSettings, mqttTransportSettings });
            var sut = new ProtocolRoutingDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(Arg.Is(false), cancellationToken).ExpectedAsync <IotHubCommunicationException>();

            await innerHandler.Received(2).CloseAsync();

            Assert.AreEqual(2, openCallCounter);
        }
        static async Task TransportRouting_TryOpenFailedWithSupportedExceptionFirstTimes_SuccessOnSecondTry(Func <Exception> exceptionFactory)
        {
            var contextMock           = Substitute.For <IPipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();

            innerHandler.CloseAsync().Returns(TaskConstants.Completed);
            int openCallCounter = 0;

            innerHandler.OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>()).Returns(async ci =>
            {
                openCallCounter++;
                await Task.Yield();
                if (openCallCounter == 1)
                {
                    throw exceptionFactory();
                }
            });
            contextMock.Get <ITransportSettings[]>().Returns(new[] { amqpTransportSettings, mqttTransportSettings });
            var sut = new ProtocolRoutingDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(Arg.Is(false), cancellationToken);

            await innerHandler.Received(1).CloseAsync();

            Assert.AreEqual(2, openCallCounter);
        }
示例#4
0
        public async Task TransportRouting_TryOpenFailedWithUnsupportedException_FailOnFirstTry()
        {
            var contextMock           = Substitute.For <IPipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();

            innerHandler.CloseAsync().Returns(TaskConstants.Completed);
            int openCallCounter = 0;

            innerHandler.OpenAsync(false, CancellationToken.None).ReturnsForAnyArgs(async ci =>
            {
                openCallCounter++;
                await Task.Yield();
                throw new InvalidOperationException();
            });
            contextMock.Get <ITransportSettings[]>().Returns(new[] { amqpTransportSettings, mqttTransportSettings });
            var sut = new ProtocolRoutingDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(Arg.Is(false), cancellationToken).ExpectedAsync <InvalidOperationException>().ConfigureAwait(false);

            await innerHandler.Received(1).CloseAsync().ConfigureAwait(false);

            Assert.AreEqual(1, openCallCounter);
        }
        public async Task TransportRouting_TryOpenWhenInnerHandlerNotUsable_Success()
        {
            var contextMock           = Substitute.For <PipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            int openCallCounter       = 0;

            var innerHandlers = new List <IDelegatingHandler>(2);

            IDelegatingHandler GetInnerHandler()
            {
                var innerHandler = Substitute.For <IDelegatingHandler>();

                innerHandler.IsUsable.Returns(true);
                innerHandler.OpenAsync(Arg.Any <CancellationToken>()).Returns(async ci =>
                {
                    openCallCounter++;
                    await Task.Yield();
                    innerHandler.IsUsable.Returns(false);
                });
                innerHandlers.Add(innerHandler);
                return(innerHandler);
            }

            contextMock.TransportSettingsArray = new[] { amqpTransportSettings, mqttTransportSettings };
            var sut = new ProtocolRoutingDelegatingHandler(contextMock, null);

            sut.ContinuationFactory = (ctx, inner) => GetInnerHandler();
            var cancellationToken = new CancellationToken();

            await sut.OpenAsync(cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(1, innerHandlers.Count);
            innerHandlers[0].Received(0).Dispose();

            await sut.OpenAsync(cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(2, innerHandlers.Count);
            innerHandlers[0].Received(1).Dispose();
            innerHandlers[1].Received(0).Dispose();

            Assert.AreEqual(2, openCallCounter);
        }
        public async Task TransportRouting_CancellationTokenCanceled_Open()
        {
            var transportSettings = Substitute.For <ITransportSettings>();
            var innerHandler      = Substitute.For <IDelegatingHandler>();

            innerHandler.OpenAsync(Arg.Any <CancellationToken>()).Returns(TaskHelpers.CompletedTask);
            var contextMock = Substitute.For <PipelineContext>();
            var sut         = new ProtocolRoutingDelegatingHandler(contextMock, null);

            var userDefinedTimeoutCancellationTokenSource = new CancellationTokenSource();

            userDefinedTimeoutCancellationTokenSource.Cancel();

            await sut.OpenAsync(userDefinedTimeoutCancellationTokenSource.Token).ExpectedAsync <OperationCanceledException>().ConfigureAwait(false);
        }
        public async Task TransportRouting_CancellationTokenCanceled_Open()
        {
            var transportSettings = Substitute.For <ITransportSettings>();
            var innerHandler      = Substitute.For <IDelegatingHandler>();

            innerHandler.OpenAsync(Arg.Any <bool>(), Arg.Any <CancellationToken>()).Returns(TaskConstants.Completed);
            var contextMock = Substitute.For <IPipelineContext>();
            var sut         = new ProtocolRoutingDelegatingHandler(contextMock);

            var userDefinedTimeoutCancellationTokenSource = new CancellationTokenSource();

            userDefinedTimeoutCancellationTokenSource.Cancel();
            await sut.OpenAsync(false, userDefinedTimeoutCancellationTokenSource.Token);

            await innerHandler.Received(0).OpenAsync(Arg.Any <bool>(), userDefinedTimeoutCancellationTokenSource.Token);
        }
        public async Task TransportRouting_FirstTrySucceed_Open()
        {
            var contextMock           = Substitute.For <IPipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();

            innerHandler.OpenAsync(false, CancellationToken.None).ReturnsForAnyArgs(TaskConstants.Completed);
            contextMock.Get <ITransportSettings[]>().Returns(new[] { amqpTransportSettings, mqttTransportSettings });
            var sut = new ProtocolRoutingDelegatingHandler(contextMock);

            sut.ContinuationFactory = c => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(false, cancellationToken);

            await innerHandler.Received(1).OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>());
        }
        public async Task TransportRouting_FirstTrySucceed_Open()
        {
            var contextMock           = Substitute.For <PipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();

            innerHandler.OpenAsync(CancellationToken.None).ReturnsForAnyArgs(TaskHelpers.CompletedTask);
            contextMock.TransportSettingsArray = new[] { amqpTransportSettings, mqttTransportSettings };
            var sut = new ProtocolRoutingDelegatingHandler(contextMock, null);

            sut.ContinuationFactory = (ctx, inner) => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(cancellationToken).ConfigureAwait(false);

            await innerHandler.Received(1).OpenAsync(Arg.Any <CancellationToken>()).ConfigureAwait(false);
        }
        public async Task TransportRouting_TryOpenFailedWithUnsupportedException_FailOnFirstTry()
        {
            var contextMock           = Substitute.For <PipelineContext>();
            var amqpTransportSettings = Substitute.For <ITransportSettings>();
            var mqttTransportSettings = Substitute.For <ITransportSettings>();
            var innerHandler          = Substitute.For <IDelegatingHandler>();
            int openCallCounter       = 0;

            innerHandler.OpenAsync(CancellationToken.None).ReturnsForAnyArgs(async ci =>
            {
                openCallCounter++;
                await Task.Yield();
                throw new InvalidOperationException();
            });
            contextMock.TransportSettingsArray = new[] { amqpTransportSettings, mqttTransportSettings };
            var sut = new ProtocolRoutingDelegatingHandler(contextMock, null);

            sut.ContinuationFactory = (ctx, inner) => innerHandler;
            var cancellationToken = new CancellationToken();
            await sut.OpenAsync(cancellationToken).ExpectedAsync <InvalidOperationException>().ConfigureAwait(false);

            Assert.AreEqual(1, openCallCounter);
        }