public async Task ErrorHandler_NoErrors_Success() { var innerHandler = Substitute.For<IDelegatingHandler>(); innerHandler.OpenAsync(Arg.Is(false)).Returns(TaskConstants.Completed); innerHandler.SendEventAsync(Arg.Any<Message>()).Returns(TaskConstants.Completed); var sut = new ErrorDelegatingHandler(() => innerHandler); //emulate Gatekeeper behaviour: it opens the channel for us await sut.OpenAsync(false); await sut.SendEventAsync(new Message(new byte[0])); await innerHandler.Received(1).OpenAsync(Arg.Is(false)); await innerHandler.Received(1).SendEventAsync(Arg.Any<Message>()); }
public async Task ErrorHandler_NoErrors_Success() { var contextMock = Substitute.For<IPipelineContext>(); var innerHandler = Substitute.For<IDelegatingHandler>(); innerHandler.OpenAsync(Arg.Any<CancellationToken>()).Returns(Task.CompletedTask); innerHandler.SendEventAsync(Arg.Any<Message>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask); var sut = new ErrorDelegatingHandler(contextMock, innerHandler); var cancellationToken = new CancellationToken(); await sut.OpenAsync(cancellationToken).ConfigureAwait(false); await sut.SendEventAsync(new Message(new byte[0]), cancellationToken).ConfigureAwait(false); await innerHandler.Received(1).OpenAsync(cancellationToken).ConfigureAwait(false); await innerHandler.Received(1).SendEventAsync(Arg.Any<Message>(), cancellationToken).ConfigureAwait(false); }
DeviceClient(IotHubConnectionString iotHubConnectionString, ITransportSettings[] transportSettings) { this.iotHubConnectionString = iotHubConnectionString; #if !WINDOWS_UWP var innerHandler = new RetryDelegatingHandler( new ErrorDelegatingHandler( () => new RoutingDelegatingHandler(this.CreateTransportHandler, iotHubConnectionString, transportSettings))); #else // UWP does not support retry yet. We need to make the underlying Message stream accessible internally on UWP // to be sure that either the stream has not been read or it is seekable to safely retry operation var innerHandler = new ErrorDelegatingHandler( () => new RoutingDelegatingHandler(this.CreateTransportHandler, iotHubConnectionString, transportSettings)); #endif this.InnerHandler = new GateKeeperDelegatingHandler(innerHandler); }
static async Task OperationAsync_ExceptionThrownAndThenSucceed_OperationSuccessfullyCompleted(Func <IDelegatingHandler, Task> mockSetup, Func <IDelegatingHandler, Task> act, Func <IDelegatingHandler, Task> assert, Type thrownExceptionType, Type expectedExceptionType, bool reopenExpected) { int ctorCallCounter = 0; var contextMock = Substitute.For <IPipelineContext>(); var innerHandler = Substitute.For <IDelegatingHandler>(); innerHandler.OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>()).Returns(TaskConstants.Completed); var sut = new ErrorDelegatingHandler(contextMock); sut.ContinuationFactory = c => { ctorCallCounter++; return(innerHandler); }; //initial OpenAsync to emulate Gatekeeper behaviour var cancellationToken = new CancellationToken(); await sut.OpenAsync(false, cancellationToken).ConfigureAwait(false); //set initial operation result that throws bool[] setup = { false }; mockSetup(innerHandler).Returns(ci => { if (setup[0]) { return(TaskConstants.Completed); } throw ExceptionFactory[thrownExceptionType](); }); //act await((Func <Task>)(() => act(sut))).ExpectedAsync(expectedExceptionType).ConfigureAwait(false); //override outcome setup[0] = true;//otherwise previosly setup call will happen and throw; mockSetup(innerHandler).Returns(TaskConstants.Completed); //act await act(sut).ConfigureAwait(false); //assert await innerHandler.Received(reopenExpected? 2 : 1).OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>()).ConfigureAwait(false); await assert(innerHandler).ConfigureAwait(false); Assert.AreEqual(reopenExpected ? 2 : 1, ctorCallCounter); }
public async Task ErrorHandler_NoErrors_Success() { var innerHandler = Substitute.For <IDelegatingHandler>(); innerHandler.OpenAsync(Arg.Is(false)).Returns(TaskConstants.Completed); innerHandler.SendEventAsync(Arg.Any <Message>()).Returns(TaskConstants.Completed); var sut = new ErrorDelegatingHandler(() => innerHandler); //emulate Gatekeeper behaviour: it opens the channel for us await sut.OpenAsync(false); await sut.SendEventAsync(new Message(new byte[0])); await innerHandler.Received(1).OpenAsync(Arg.Is(false)); await innerHandler.Received(1).SendEventAsync(Arg.Any <Message>()); }
static async Task OperationAsync_ExceptionThrownAndThenSucceed_OperationSuccessfullyCompleted( Func <IDelegatingHandler, Task <Message> > mockSetup, Func <IDelegatingHandler, Task <Message> > act, Func <IDelegatingHandler, Task <Message> > assert, Type thrownExceptionType, Type expectedExceptionType) { var contextMock = Substitute.For <IPipelineContext>(); var innerHandler = Substitute.For <IDelegatingHandler>(); var sut = new ErrorDelegatingHandler(contextMock, innerHandler); //initial OpenAsync to emulate Gatekeeper behaviour var cancellationToken = new CancellationToken(); innerHandler.OpenAsync(Arg.Any <CancellationToken>()).Returns(Task.CompletedTask); await sut.OpenAsync(cancellationToken).ConfigureAwait(false); //set initial operation result that throws bool[] setup = { false }; mockSetup(innerHandler).Returns(ci => { if (setup[0]) { return(Task.FromResult(new Message())); } throw ExceptionFactory[thrownExceptionType](); }); //act await((Func <Task>)(() => act(sut))).ExpectedAsync(expectedExceptionType).ConfigureAwait(false); //override outcome setup[0] = true;//otherwise previosly setup call will happen and throw; mockSetup(innerHandler).Returns(new Message()); //act await act(sut).ConfigureAwait(false); //assert await innerHandler.Received(1).OpenAsync(Arg.Any <CancellationToken>()).ConfigureAwait(false); await assert(innerHandler).ConfigureAwait(false); }
public async Task ErrorHandler_NoErrors_Success() { var contextMock = Substitute.For <IPipelineContext>(); var innerHandler = Substitute.For <IDelegatingHandler>(); innerHandler.OpenAsync(Arg.Is(false), Arg.Any <CancellationToken>()).Returns(TaskConstants.Completed); innerHandler.SendEventAsync(Arg.Any <Message>(), Arg.Any <CancellationToken>()).Returns(TaskConstants.Completed); var sut = new ErrorDelegatingHandler(contextMock); sut.ContinuationFactory = c => innerHandler; //emulate Gatekeeper behaviour: it opens the channel for us var cancellationToken = new CancellationToken(); await sut.OpenAsync(false, cancellationToken).ConfigureAwait(false); await sut.SendEventAsync(new Message(new byte[0]), cancellationToken).ConfigureAwait(false); await innerHandler.Received(1).OpenAsync(Arg.Is(false), cancellationToken).ConfigureAwait(false); await innerHandler.Received(1).SendEventAsync(Arg.Any <Message>(), cancellationToken).ConfigureAwait(false); }
static async Task OpenAsync_ExceptionThrownAndThenSucceed_SuccessfullyOpened(Func <IDelegatingHandler, Task> mockSetup, Func <IDelegatingHandler, Task> act, Func <IDelegatingHandler, Task> assert, Type thrownExceptionType, Type expectedExceptionType) { int ctorCallCounter = 0; var contextMock = Substitute.For <IPipelineContext>(); var innerHandler = Substitute.For <IDelegatingHandler>(); var sut = new ErrorDelegatingHandler(contextMock); sut.ContinuationFactory = c => { ctorCallCounter++; return(innerHandler); }; //set initial operation result that throws bool[] setup = { false }; mockSetup(innerHandler).Returns(ci => { if (setup[0]) { return(Task.FromResult(Guid.NewGuid())); } throw ExceptionFactory[thrownExceptionType](); }); //act await((Func <Task>)(() => act(sut))).ExpectedAsync(expectedExceptionType).ConfigureAwait(false); //override outcome setup[0] = true;//otherwise previosly setup call will happen and throw; mockSetup(innerHandler).Returns(TaskConstants.Completed); //act await act(sut).ConfigureAwait(false); //assert await assert(innerHandler).ConfigureAwait(false); Assert.AreEqual(2, ctorCallCounter); }
static async Task OpenAsync_ExceptionThrownAndThenSucceed_SuccessfullyOpened(Func<IDelegatingHandler, Task> mockSetup, Func<IDelegatingHandler, Task> act, Func<IDelegatingHandler, Task> assert, Type thrownExceptionType, Type expectedExceptionType) { int ctorCallCounter = 0; var innerHandler = Substitute.For<IDelegatingHandler>(); var sut = new ErrorDelegatingHandler(() => { ctorCallCounter++; return innerHandler; }); //set initial operation result that throws bool[] setup = { false }; mockSetup(innerHandler).Returns(ci => { if (setup[0]) { return Task.FromResult(Guid.NewGuid()); } throw ExceptionFactory[thrownExceptionType](); }); //act await ((Func<Task>)(() => act(sut))).ExpectedAsync(expectedExceptionType); //override outcome setup[0] = true;//otherwise previosly setup call will happen and throw; mockSetup(innerHandler).Returns(TaskConstants.Completed); //act await act(sut); //assert await assert(innerHandler); Assert.AreEqual(2, ctorCallCounter); }
static async Task OperationAsync_ExceptionThrownAndThenSucceed_OperationSuccessfullyCompleted(Func<IDelegatingHandler, Task> mockSetup, Func<IDelegatingHandler, Task> act, Func<IDelegatingHandler, Task> assert, Type thrownExceptionType, Type expectedExceptionType, bool reopenExpected) { int ctorCallCounter = 0; var innerHandler = Substitute.For<IDelegatingHandler>(); var sut = new ErrorDelegatingHandler(() => { ctorCallCounter++; return innerHandler; }); //initial OpenAsync to emulate Gatekeeper behaviour innerHandler.OpenAsync(Arg.Is(false)).Returns(TaskConstants.Completed); await sut.OpenAsync(false); //set initial operation result that throws bool[] setup = { false }; mockSetup(innerHandler).Returns(ci => { if (setup[0]) { return TaskConstants.Completed; } throw ExceptionFactory[thrownExceptionType](); }); //act await ((Func<Task>)(() => act(sut))).ExpectedAsync(expectedExceptionType); //override outcome setup[0] = true;//otherwise previosly setup call will happen and throw; mockSetup(innerHandler).Returns(TaskConstants.Completed); //act await act(sut); //assert await innerHandler.Received(reopenExpected ? 2 : 1).OpenAsync(Arg.Is(false)); await assert(innerHandler); Assert.AreEqual(reopenExpected ? 2 : 1, ctorCallCounter); }