예제 #1
0
        public void Invoke_ScopedHandler_ScopeCreatedAndHandlerInvoked()
        {
            var callbackOneHandlerOne = new CallbackOneHandlerOne();
            var serviceProvider       = ServiceProviderHelper.GetServiceProvider(
                services => services
                .AddFakeLogger()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddBroker <TestBroker>())
                .AddScopedBrokerCallbackHandler(_ => callbackOneHandlerOne));

            var invoker = serviceProvider.GetRequiredService <IBrokerCallbacksInvoker>();

            invoker.Invoke <ICallbackOneHandler>(handler => handler.Handle());

            callbackOneHandlerOne.CallCount.Should().Be(1);
        }
예제 #2
0
        public void Invoke_WithServiceProvider_HandlerResolvedUsingSpecifiedProvider()
        {
            var serviceProvider = ServiceProviderHelper.GetServiceProvider(
                services => services
                .AddFakeLogger()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddBroker <TestBroker>()));

            var callbackOneHandlerOne     = new CallbackOneHandlerOne();
            var substituteServiceProvider = Substitute.For <IServiceProvider>();

            substituteServiceProvider
            .GetService(typeof(IEnumerable <IBrokerCallback>))
            .Returns(new[] { callbackOneHandlerOne });

            var invoker = serviceProvider.GetRequiredService <IBrokerCallbacksInvoker>();

            invoker.Invoke <ICallbackOneHandler>(handler => handler.Handle(), substituteServiceProvider);

            callbackOneHandlerOne.CallCount.Should().Be(1);
        }
예제 #3
0
        public void Invoke_DisablingCallbackDuringShutdown_CallbackNotInvoked()
        {
            var callbackOneHandlerOne   = new CallbackOneHandlerOne();
            var hostApplicationLifetime = new FakeHostApplicationLifetime();

            var serviceProvider = ServiceProviderHelper.GetServiceProvider(
                services => services
                .AddFakeLogger()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddBroker <TestBroker>())
                .AddSingletonBrokerCallbackHandler(callbackOneHandlerOne),
                hostApplicationLifetime);

            hostApplicationLifetime.TriggerApplicationStopping();

            var invoker = serviceProvider.GetRequiredService <IBrokerCallbacksInvoker>();

            invoker.Invoke <ICallbackOneHandler>(handler => handler.Handle(), invokeDuringShutdown: false);

            callbackOneHandlerOne.CallCount.Should().Be(0);
        }
예제 #4
0
        public void Invoke_SomeHandlers_MatchingHandlersInvoked()
        {
            var callbackOneHandlerOne = new CallbackOneHandlerOne();
            var callbackOneHandlerTwo = new CallbackOneHandlerTwo();
            var callbackTwoHandlerOne = new CallbackTwoHandlerOne();

            var serviceProvider = ServiceProviderHelper.GetServiceProvider(
                services => services
                .AddFakeLogger()
                .AddSilverback()
                .WithConnectionToMessageBroker(options => options.AddBroker <TestBroker>())
                .AddSingletonBrokerCallbackHandler(callbackOneHandlerOne)
                .AddSingletonBrokerCallbackHandler(callbackOneHandlerTwo)
                .AddSingletonBrokerCallbackHandler(callbackTwoHandlerOne));

            var invoker = serviceProvider.GetRequiredService <IBrokerCallbacksInvoker>();

            invoker.Invoke <ICallbackOneHandler>(handler => handler.Handle());

            callbackOneHandlerOne.CallCount.Should().Be(1);
            callbackOneHandlerTwo.CallCount.Should().Be(1);
            callbackTwoHandlerOne.CallCount.Should().Be(0);
        }