Example #1
0
        public void ExecuteAsync_ExistingHandlerForRequest_ExecutedRequestHandlerToProduceResponse()
        {
            var requestExecutor    = TestableRequestExecutor.Create();
            var requestHandlerMock = new Mock <IRequestHandler>();
            var requestId          = Guid.NewGuid();

            requestHandlerMock.SetupGet(r => r.RequestType).Returns("requestType");
            requestHandlerMock.SetupGet(r => r.ArgumentsType).Returns(typeof(string));
            requestExecutor.SerialiserMock.Setup(s => s.DeserialiseArguments("the args for the request", typeof(string))).Returns("the deserialised arguments");
            requestHandlerMock.Setup(r => r.HandleRequestAsync(It.IsAny <Guid>(), It.IsAny <object>())).ReturnsAsync(new Response
            {
                Id = requestId
            });
            requestExecutor.Handlers.Add(requestHandlerMock.Object);
            var cancelationTokenSource = new CancellationTokenSource();

            requestExecutor.ExecuteAsync(new Request
            {
                Id   = requestId,
                Type = "requestType",
                Args = "the args for the request"
            }, cancelationTokenSource.Token).Wait();
            var executedRequest = ((RequestExecutedChannelMessage)requestExecutor.DispatchMessagesSourceBlock.Receive()).RequestExecutedResult;

            Assert.Equal(requestId, executedRequest.Response.Id);
            Assert.False(executedRequest.IsFaulted);
            Assert.Null(executedRequest.Exception);
            requestHandlerMock.Verify(h => h.HandleRequestAsync(requestId, "the deserialised arguments"));
        }
Example #2
0
        public void ExecuteAsync_HandlerThrowsException_ProducesFaultedRequestExecutedResultWithException()
        {
            var requestExecutor        = TestableRequestExecutor.Create();
            var cancelationTokenSource = new CancellationTokenSource();
            var requestHandlerMock     = new Mock <IRequestHandler>();
            var requestId = Guid.NewGuid();

            requestHandlerMock.SetupGet(r => r.RequestType).Returns("requestType");
            requestHandlerMock
            .Setup(h => h.HandleRequestAsync(It.IsAny <Guid>(), It.IsAny <object>()))
            .ThrowsAsync(new InvalidOperationException("the exception thrown by the handler"));
            requestExecutor.Handlers.Add(requestHandlerMock.Object);

            requestExecutor.ExecuteAsync(new Request
            {
                Id   = Guid.NewGuid(),
                Type = "requestType"
            }, cancelationTokenSource.Token).Wait();
            var executedRequest = ((RequestExecutedChannelMessage)requestExecutor.DispatchMessagesSourceBlock.Receive()).RequestExecutedResult;

            Assert.True(executedRequest.IsFaulted);
            Assert.Null(executedRequest.Response);
            Assert.IsType <HandlerFailedException>(executedRequest.Exception);
            Assert.Equal("the exception thrown by the handler", executedRequest.Exception.InnerException.Message);
        }
Example #3
0
        public void ExecuteAsync_Cancelled_ThrowsTaskCancelledException()
        {
            var requestExecutor        = TestableRequestExecutor.Create();
            var cancelationTokenSource = new CancellationTokenSource();

            cancelationTokenSource.Cancel();

            Assert.Throws <TaskCanceledException>(() => requestExecutor.ExecuteAsync(new Request
            {
                Id   = Guid.NewGuid(),
                Type = "requestType"
            }, cancelationTokenSource.Token).GetAwaiter().GetResult());
        }
Example #4
0
        public void ExecuteAsync_NoHandlers_CreatesResponseWithNoRequestHandlerFoundException()
        {
            var requestExecutor        = TestableRequestExecutor.Create();
            var cancelationTokenSource = new CancellationTokenSource();

            requestExecutor.ExecuteAsync(new Request
            {
                Id   = Guid.NewGuid(),
                Type = "requestType"
            }, cancelationTokenSource.Token).Wait();
            var executedRequest = ((RequestExecutedChannelMessage)requestExecutor.DispatchMessagesSourceBlock.Receive()).RequestExecutedResult;

            Assert.True(executedRequest.IsFaulted);
            Assert.Null(executedRequest.Response);
            Assert.IsType <NoRequestHandlerFoundException>(executedRequest.Exception);
        }
Example #5
0
        public void ExecuteAsync_InvalidRequestArguments_ProducesFaultedRequestExecutedResultWithException()
        {
            var requestExecutor = TestableRequestExecutor.Create();

            requestExecutor.SerialiserMock.Setup(s => s.DeserialiseArguments(It.IsAny <string>(), It.IsAny <Type>())).Throws <SerialiserException>();
            var cancelationTokenSource = new CancellationTokenSource();
            var requestHandlerMock     = new Mock <IRequestHandler>();
            var requestId = Guid.NewGuid();

            requestHandlerMock.SetupGet(r => r.RequestType).Returns("requestType");
            requestExecutor.Handlers.Add(requestHandlerMock.Object);

            requestExecutor.ExecuteAsync(new Request
            {
                Id   = Guid.NewGuid(),
                Type = "requestType"
            }, cancelationTokenSource.Token).Wait();
            var executedRequest = ((RequestExecutedChannelMessage)requestExecutor.DispatchMessagesSourceBlock.Receive()).RequestExecutedResult;

            Assert.True(executedRequest.IsFaulted);
            Assert.Null(executedRequest.Response);
            Assert.IsType <HandlerFailedException>(executedRequest.Exception);
            Assert.IsType <SerialiserException>(executedRequest.Exception.InnerException);
        }