示例#1
0
        public CommandProcessorMissingOutMapperTests()
        {
            _myRequest.RequestValue = "Hello World";

            var messageMapperRegistry = new MessageMapperRegistry(new SimpleMessageMapperFactory((type) =>
            {
                if (type == typeof(MyResponseMessageMapper))
                {
                    return(new MyResponseMessageMapper());
                }

                throw new ConfigurationException($"No mapper found for {type.Name}");
            }));

            messageMapperRegistry.Register <MyResponse, MyResponseMessageMapper>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyResponse, MyResponseHandler>();
            var handlerFactory = new TestHandlerFactory <MyResponse, MyResponseHandler>(() => new MyResponseHandler());

            var retryPolicy = Policy
                              .Handle <Exception>()
                              .Retry();

            var circuitBreakerPolicy = Policy
                                       .Handle <Exception>()
                                       .CircuitBreaker(1, TimeSpan.FromMilliseconds(1));

            _commandProcessor = new CommandProcessor(
                subscriberRegistry,
                handlerFactory,
                new InMemoryRequestContextFactory(),
                new PolicyRegistry
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                (IAmAMessageProducer) new FakeMessageProducer(),
                responseChannelFactory: new InMemoryChannelFactory());

            PipelineBuilder <MyResponse> .ClearPipelineCache();
        }
        public ContextBagVisibilityTests()
        {
            var registry = new SubscriberRegistry();

            registry.Register <MyCommand, MyContextAwareCommandHandler>();
            var handlerFactory = new TestHandlerFactory <MyCommand, MyContextAwareCommandHandler>(() => new MyContextAwareCommandHandler());

            _request_context = new RequestContext();
            _myCommand       = new MyCommand();
            MyContextAwareCommandHandler.TestString = null;

            var requestContextFactory = A.Fake <IAmARequestContextFactory>();

            A.CallTo(() => requestContextFactory.Create()).Returns(_request_context);

            _commandProcessor = new CommandProcessor(registry, handlerFactory, requestContextFactory, new PolicyRegistry());

            _request_context.Bag["TestString"] = I_AM_A_TEST_OF_THE_CONTEXT_BAG;
        }
示例#3
0
        public void CachesReusableHandlers()
        {
            MockRepository     mocks               = new MockRepository();
            TestHandlerFactory f                   = (TestHandlerFactory)mocks.PartialMock(typeof(TestHandlerFactory));
            IHttpHandler       reusableHandler     = (IHttpHandler)mocks.DynamicMock(typeof(IHttpHandler));
            IConfigurableApplicationContext appCtx = (IConfigurableApplicationContext)mocks.DynamicMock(typeof(IConfigurableApplicationContext));

            // if (IHttpHandler.IsReusable == true) => always returns the same handler instance
            // - CreateHandlerInstance() is only called once
            using (Record(mocks))
            {
                Expect.Call(reusableHandler.IsReusable).Return(true);
                Expect.Call(f.GetContextStub("reusable")).Return(appCtx);
                Expect.Call(f.CreateHandlerInstanceStub(appCtx, null, null, "reusable", null)).Return(reusableHandler);
            }
            using (Playback(mocks))
            {
                Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
                Assert.AreSame(reusableHandler, f.GetHandler(null, null, "reusable", null));
            }
        }
        public void DoesntCacheNonReusableHandlers()
        {
            TestHandlerFactory f = A.Fake <TestHandlerFactory>(options => options.CallsBaseMethods());
            IHttpHandler       nonReusableHandler = A.Fake <IHttpHandler>();

            A.CallTo(() => nonReusableHandler.IsReusable).Returns(false);
            IHttpHandler nonReusableHandler2 = A.Fake <IHttpHandler>();

            A.CallTo(() => nonReusableHandler2.IsReusable).Returns(false);
            IConfigurableApplicationContext appCtx = A.Fake <IConfigurableApplicationContext>();

            // if (IHttpHandler.IsReusable == false) => always create new handler instance
            // - CreateHandlerInstance() is called for each request

            A.CallTo(() => f.GetContextStub("notreusable")).Returns(appCtx);
            A.CallTo(() => f.CreateHandlerInstanceStub(appCtx, null, null, "notreusable", null))
            .Returns(nonReusableHandler).Once()
            .Then.Returns(nonReusableHandler2).Once();

            Assert.AreSame(nonReusableHandler, f.GetHandler(null, null, "notreusable", null));
            Assert.AreSame(nonReusableHandler2, f.GetHandler(null, null, "notreusable", null));
        }
示例#5
0
        public void Setup()
        {
            var logger = new Mock <ILog>().Object;

            var registry = new SubscriberRegistry();

            registry.Register <TestCommand, TestContextAwareCommandHandler>();
            var handlerFactory = new TestHandlerFactory <TestCommand, TestContextAwareCommandHandler>(() => new TestContextAwareCommandHandler(logger));

            requestContext = new RequestContext();
            testCommand    = new TestCommand();
            TestContextAwareCommandHandler.TestString = null;

            var requestContextFactory = new Mock <IAmARequestContextFactory>();

            requestContextFactory.Setup(r => r.Create()).Returns(requestContext);

            commandProcessor = new CommandProcessor(registry, handlerFactory, requestContextFactory.Object, new PolicyRegistry(), logger);

            requestContext.Bag["TestString"] = BagMessage;

            commandProcessor.Send(testCommand);
        }
        public CommandProcessorCallTests()
        {
            _myRequest.RequestValue = "Hello World";

            _fakeMessageProducer = new FakeMessageProducer();

            var header = new MessageHeader(
                messageId: _myRequest.Id,
                topic: "MyRequest",
                messageType: MessageType.MT_COMMAND,
                correlationId: _myRequest.ReplyAddress.CorrelationId,
                replyTo: _myRequest.ReplyAddress.Topic);

            var json = new JObject(new JProperty("Id", _myRequest.Id), new JProperty("RequestValue", _myRequest.RequestValue));
            var body = new MessageBody(json.ToString());

            _message = new Message(header, body);

            var messageMapperRegistry = new MessageMapperRegistry(new SimpleMessageMapperFactory((type) =>
            {
                if (type == typeof(MyRequestMessageMapper))
                {
                    return(new MyRequestMessageMapper());
                }
                if (type == typeof(MyResponseMessageMapper))
                {
                    return(new MyResponseMessageMapper());
                }

                throw new ConfigurationException($"No mapper found for {type.Name}");
            }));

            messageMapperRegistry.Register <MyRequest, MyRequestMessageMapper>();
            messageMapperRegistry.Register <MyResponse, MyResponseMessageMapper>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyResponse, MyResponseHandler>();
            var handlerFactory = new TestHandlerFactory <MyResponse, MyResponseHandler>(() => new MyResponseHandler());

            var retryPolicy = Policy
                              .Handle <Exception>()
                              .Retry();

            var circuitBreakerPolicy = Policy
                                       .Handle <Exception>()
                                       .CircuitBreaker(1, TimeSpan.FromMilliseconds(1));

            InMemoryChannelFactory inMemoryChannelFactory = new InMemoryChannelFactory();

            //we need to seed the response as the fake producer does not actually send across the wire
            inMemoryChannelFactory.SeedChannel(new[] { _message });

            _commandProcessor = new CommandProcessor(
                subscriberRegistry,
                handlerFactory,
                new InMemoryRequestContextFactory(),
                new PolicyRegistry {
                { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                (IAmAMessageProducer)_fakeMessageProducer,
                responseChannelFactory: inMemoryChannelFactory);

            PipelineBuilder <MyRequest> .ClearPipelineCache();
        }