public async Task ShouldInvokeRegisteredQueryHandlerInContainerWhenDispatchedMultipleTimes()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomething, string> >(() => queryHandler, Lifestyle.Singleton);
                container.Register <IQueryAsyncHandler <QuerySomethingWithNonReferenceTypeResult, int> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);

                const string data1 = "Test message 1.";
                const string data2 = "Test message 2.";
                const int    data3 = 1;

                var dispatcher = new QueryDispatcher(resolver);
                var result1    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data1));
                var result2    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data2));
                var result3    = dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(data3));

                await Task.WhenAll(result1, result2, result3);

                (await result1).Should().Be(data1);
                (await result2).Should().Be(data2);
                (await result3).Should().Be(data3);
            }
            public async Task Should_Invoke_Registered_Query_Handler_In_Container_When_Dispatched_Multiple_Times()
            {
                var queryHandler = new TestQueryHandler(_testOutputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomething, string> >(() => queryHandler, Lifestyle.Singleton);
                container.Register <IQueryAsyncHandler <QuerySomethingWithNonReferenceTypeResult, int> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);

                const string data1 = "Test message 1.";
                const string data2 = "Test message 2.";
                const int    data3 = 1;

                var dispatcher = new QueryDispatcher(resolver);
                var result1    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data1));
                var result2    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data2));
                var result3    = dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(data3));

                await Task.WhenAll(result1, result2, result3);

                Assert.Equal(data1, await result1);
                Assert.Equal(data2, await result2);
                Assert.Equal(data3, await result3);
            }
            public Task Should_Throw_When_Cancelled()
            {
                return(Assert.ThrowsAnyAsync <OperationCanceledException>(async() =>
                {
                    var queryHandler = new TestQueryHandler(_testOutputHelper);
                    var registration = new QueryHandlerRegistration();
                    registration.Register(() => (IQueryAsyncHandler <QuerySomethingWithDelay, string>)queryHandler);

                    var cts = new CancellationTokenSource();

                    var dispatcher = new QueryDispatcher(registration);
                    Task task = dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(new QuerySomethingWithDelay("This will be cancelled", 3000), cts.Token);

                    cts.Cancel();

                    try
                    {
                        await task;
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
            public async Task ShouldInvokeRegisteredQueryHandlerWithCompositeResolver()
            {
                var commandHandler = new TestQueryHandler(_outputHelper);
                var container      = new Container();

                container.Register <IQueryAsyncHandler <QuerySomethingWithNonReferenceTypeResult, int> >(() => commandHandler, Lifestyle.Singleton);
                container.Register <IQueryHandler <QuerySomething, string> >(() => commandHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var containerAsyncHandlerResolver = new ContainerQueryAsyncHandlerResolver(containerAdapter);
                var containerHandlerResolver      = new ContainerQueryHandlerResolver(containerAdapter);

                Func <Exception, bool> exceptionHandler = (ex) =>
                {
                    var exception = ex as NoQueryHandlerResolvedException;
                    if (exception != null)
                    {
                        _outputHelper.WriteLine($"Ignoring encountered exception while trying to resolve query handler for {exception.QueryType.Name}...");

                        // Notify as handled if no command handler is resolved from other resolvers.
                        return(true);
                    }

                    return(false);
                };

                var compositeResolver = new CompositeQueryHandlerResolver(new List <IQueryHandlerResolver>()
                {
                    containerAsyncHandlerResolver,
                    containerHandlerResolver
                }, exceptionHandler);                                    // Pass in an exception handler.

                var dispatcher = new QueryDispatcher(compositeResolver); // Composite resolver

                int result1 = await dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(1973));

                string result2 = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething("1973"));

                result1.Should().Be(1973);
                result2.Should().Be("1973");
            }
            public async Task Should_Allow_Registered_Query_Handlers_With_Non_Reference_Type_Query_Results()
            {
                var queryHandler = new TestQueryHandler(_testOutputHelper);
                var registration = new QueryHandlerRegistration();

                registration.Register(() => (IQueryHandler <QuerySomethingWithNonReferenceTypeResult, int>)queryHandler);

                var dispatcher = new QueryDispatcher(registration);
                var result     = await dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(1973));

                Assert.Equal(1973, result);
            }
            public async Task ShouldAllowRegisteredQueryHandlersWithNonReferenceTypeQueryResults()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var registration = new QueryHandlerRegistration();

                registration.Register(() => (IQueryHandler <QuerySomethingWithNonReferenceTypeResult, int>)queryHandler);

                var dispatcher = new QueryDispatcher(registration);
                var result     = await dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(1973));

                result.Should().Be(1973);
            }
 public async Task <TResult> DispatchAsync <TQuery, TResult>(TQuery query, CancellationToken cancellationToken = default(CancellationToken)) where TQuery : class, IQuery <TResult>
 {
     try
     {
         return(await _queryDispatcher.DispatchAsync <TQuery, TResult>(query));
     }
     catch (Exception ex)
     {
         _outputHelper.WriteLine(ex.ToString());
         throw;
     }
 }
            public async Task ShouldInvokeRegisteredAttributeQueryHandlerWhenDispatchedMultipleTimes()
            {
                var queryHandler = new TestAttributedQueryHandler(_outputHelper);
                var registration = new QueryHandlerAttributeRegistration();

                registration.Register(() => queryHandler);

                const string data1 = "Test message 1.";
                const string data2 = "Test message 2.";
                const int    data3 = 1;

                var dispatcher = new QueryDispatcher(registration);
                var result1    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data1));
                var result2    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data2));
                var result3    = dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(data3));

                await Task.WhenAll(result1, result2, result3);

                (await result1).Should().Be(data1);
                (await result2).Should().Be(data2);
                (await result3).Should().Be(data3);
            }
            public async Task ShouldInvokeRegisteredQueryHandler()
            {
                var registration = new QueryHandlerRegistration();

                registration.Register(() => (IQueryAsyncHandler <QuerySomething, string>) new TestQueryHandler(_outputHelper));

                const string data = nameof(ShouldInvokeRegisteredQueryHandler);

                var dispatcher = new QueryDispatcher(registration);
                var result     = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data));

                result.Should().Be(data);
            }
            public async Task Should_Invoke_Registered_Attribute_Query_Handler_When_Dispatched_Multiple_Times()
            {
                var queryHandler = new TestAttributedQueryHandler(_testOutputHelper);
                var registration = new QueryHandlerAttributeRegistration();

                registration.Register(() => queryHandler);

                const string data1 = "Test message 1.";
                const string data2 = "Test message 2.";
                const int    data3 = 1;

                var dispatcher = new QueryDispatcher(registration);
                var result1    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data1));
                var result2    = dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data2));
                var result3    = dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(data3));

                await Task.WhenAll(result1, result2, result3);

                Assert.Equal(data1, await result1);
                Assert.Equal(data2, await result2);
                Assert.Equal(data3, await result3);
            }
            public async Task Should_Invoke_Registered_Query_Handler()
            {
                var registration = new QueryHandlerRegistration();

                registration.Register(() => (IQueryAsyncHandler <QuerySomething, string>) new TestQueryHandler(_testOutputHelper));

                const string data = nameof(Should_Invoke_Registered_Query_Handler);

                var dispatcher = new QueryDispatcher(registration);
                var result     = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data));

                Assert.Equal(result, data);
            }
        public void TestDispatch_QueryHandlerNotFound()
        {
            var services        = new ServiceCollection();
            var serviceProvider = services.BuildServiceProvider();

            var dispatcher = new QueryDispatcher(serviceProvider);

            var query = new TestQuery
            {
                Value = "value",
            };

            Assert.ThrowsAsync <QueryHandlerNotFoundException>(() => dispatcher.DispatchAsync <TestQuery, TestResult>(query));
        }
            public async Task ShouldAllowRegisteredQueryHandlersInContainerWithNonReferenceTypeQueryResults()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomethingWithNonReferenceTypeResult, int> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);
                var dispatcher       = new QueryDispatcher(resolver);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(1973));

                result.Should().Be(1973);
            }
            public async Task Should_Allow_Registered_Query_Handlers_In_Container_With_Non_Reference_Type_Query_Results()
            {
                var queryHandler = new TestQueryHandler(_testOutputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomethingWithNonReferenceTypeResult, int> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);
                var dispatcher       = new QueryDispatcher(resolver);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithNonReferenceTypeResult, int>(new QuerySomethingWithNonReferenceTypeResult(1973));

                Assert.Equal(1973, result);
            }
            public async Task Should_Invoke_Registered_Attribute_Query_Handler_With_Cancellation_Token()
            {
                var queryHandler = new TestAttributedQueryHandler(_testOutputHelper);
                var registration = new QueryHandlerAttributeRegistration();

                registration.Register(() => queryHandler);
                var dispatcher = new QueryDispatcher(registration);

                var          cts  = new CancellationTokenSource();
                const string data = nameof(Should_Invoke_Registered_Attribute_Query_Handler_With_Cancellation_Token);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(new QuerySomethingWithDelay(data, 500), cts.Token);

                Assert.Equal(data, result);
            }
            public async Task ShouldInvokeRegisteredAttributeQueryHandlerWithCancellationToken()
            {
                var queryHandler = new TestAttributedQueryHandler(_outputHelper);
                var registration = new QueryHandlerAttributeRegistration();

                registration.Register(() => queryHandler);
                var dispatcher = new QueryDispatcher(registration);

                var          cts  = new CancellationTokenSource();
                const string data = nameof(ShouldInvokeRegisteredAttributeQueryHandlerWithCancellationToken);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(new QuerySomethingWithDelay(data, 500), cts.Token);

                result.Should().Be(data);
            }
            public async Task ShouldInvokeRegisteredQueryHandlerInContainer()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomething, string> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);

                const string data = nameof(ShouldInvokeRegisteredQueryHandlerInContainer);

                var dispatcher = new QueryDispatcher(resolver);
                var result     = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data));

                result.Should().Be(data);
            }
            public async Task Should_Invoke_Registered_Query_Handler_In_Container()
            {
                var queryHandler = new TestQueryHandler(_testOutputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomething, string> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);

                const string data = nameof(Should_Invoke_Registered_Query_Handler_In_Container);

                var dispatcher = new QueryDispatcher(resolver);
                var result     = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething(data));

                Assert.Equal(data, result);
            }
            public async Task Should_Invoke_Registered_Query_Handler_In_Container_With_Cancellation_Token()
            {
                var queryHandler = new TestQueryHandler(_testOutputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomethingWithDelay, string> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);
                var dispatcher       = new QueryDispatcher(resolver);

                var          cts  = new CancellationTokenSource();
                const string data = nameof(Should_Invoke_Registered_Attribute_Query_Handler_With_Cancellation_Token);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(new QuerySomethingWithDelay(data, 500), cts.Token);

                Assert.Equal(data, result);
            }
            public async Task ShouldInvokeRegisteredQueryHandlerInContainerWithCancellationToken()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var container    = new Container();

                container.Register <IQueryAsyncHandler <QuerySomethingWithDelay, string> >(() => queryHandler, Lifestyle.Singleton);

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var resolver         = new ContainerQueryAsyncHandlerResolver(containerAdapter);
                var dispatcher       = new QueryDispatcher(resolver);

                var          cts  = new CancellationTokenSource();
                const string data = nameof(ShouldInvokeRegisteredAttributeQueryHandlerWithCancellationToken);

                var result = await dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(new QuerySomethingWithDelay(data, 500), cts.Token);

                result.Should().Be(data);
            }
        public async Task TestDispatch_QueryHandlerFound()
        {
            var services = new ServiceCollection();

            services.AddScoped <IQueryHandler <TestQuery, TestResult>, TestQueryHandler>();
            var serviceProvider = services.BuildServiceProvider();

            var dispatcher = new QueryDispatcher(serviceProvider);

            var query = new TestQuery
            {
                Value = "value",
            };

            var result = await dispatcher.DispatchAsync <TestQuery, TestResult>(query);

            Assert.NotNull(result);
            Assert.Equal("value", result.Value);
        }
            public Task Should_Throw_When_No_Registered_Attribute_Query_Handler_Is_Found()
            {
                return(Assert.ThrowsAsync <NoQueryHandlerResolvedException>(async() =>
                {
                    var registration = new QueryHandlerAttributeRegistration();
                    var dispatcher = new QueryDispatcher(registration);

                    const string data = nameof(Should_Throw_When_No_Registered_Attribute_Query_Handler_Is_Found);

                    try
                    {
                        var result = await dispatcher.DispatchAsync <QuerySomething, string>(new QuerySomething((string)data));
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
            public Task Should_Propagate_Exception_From_Query_Handler()
            {
                return(Assert.ThrowsAnyAsync <TestQueryHandlerException>(async() =>
                {
                    var queryHandler = new TestQueryHandler(_testOutputHelper);
                    var registration = new QueryHandlerRegistration();
                    registration.Register(() => (IQueryAsyncHandler <QuerySomethingWithException, string>)queryHandler);

                    var dispatcher = new QueryDispatcher(registration);

                    try
                    {
                        await dispatcher.DispatchAsync <QuerySomethingWithException, string>(new QuerySomethingWithException("This will cause an exception."));
                    }
                    catch (Exception ex)
                    {
                        _testOutputHelper.WriteLine(ex.ToString());
                        throw;
                    }
                }));
            }
            public void ShouldThrowWhenCancelled()
            {
                var queryHandler = new TestQueryHandler(_outputHelper);
                var registration = new QueryHandlerRegistration();

                registration.Register(() => (IQueryAsyncHandler <QuerySomethingWithDelay, string>)queryHandler);
                var dispatcher = new QueryDispatcher(registration);

                using (var cts = new CancellationTokenSource())
                {
                    Task task = dispatcher.DispatchAsync <QuerySomethingWithDelay, string>(
                        new QuerySomethingWithDelay("This will be cancelled", 3000), cts.Token);

                    // Cancel.
                    cts.Cancel();

                    Func <Task> cancelledTask = () => task;

                    cancelledTask.Should().Throw <OperationCanceledException>();
                }
            }
示例#25
0
 public WhenAnExceptionIsThrown(Exception exception, HttpStatusCode expectedStatusCode, object expectedResponse)
     : base(1, expectedStatusCode, expectedResponse)
 {
     QueryDispatcher.DispatchAsync(Arg.Any <CanaryQuery>(), Arg.Any <CancellationToken>())
     .Throws(exception);
 }
 public async Task <IEnumerable <string> > Get()
 {
     return(await QueryDispatcher.DispatchAsync(new TestQuery()));
 }