Exemplo n.º 1
0
        public CommandProcessorNoOutboxAsyncTests()
        {
            _myCommand.Value = "Hello World";

            _fakeMessageProducerWithPublishConfirmation = new FakeMessageProducerWithPublishConfirmation();

            var messageMapperRegistry = new MessageMapperRegistry(new SimpleMessageMapperFactory((_) => new MyCommandMessageMapper()));

            messageMapperRegistry.Register <MyCommand, MyCommandMessageMapper>();

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

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

            _commandProcessor = new CommandProcessor(
                new InMemoryRequestContextFactory(),
                new PolicyRegistry {
                { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                null,
                new ProducerRegistry(new Dictionary <string, IAmAMessageProducer>()
            {
                { "MyCommand", _fakeMessageProducerWithPublishConfirmation },
            }));
        }
        public PostCommandTests()
        {
            _myCommand.Value = "Hello World";

            _fakeOutboxSync = new FakeOutboxSync();
            _fakeMessageProducerWithPublishConfirmation = new FakeMessageProducerWithPublishConfirmation();

            const string topic = "MyCommand";

            _message = new Message(
                new MessageHeader(_myCommand.Id, topic, MessageType.MT_COMMAND),
                new MessageBody(JsonSerializer.Serialize(_myCommand, JsonSerialisationOptions.Options))
                );

            var messageMapperRegistry =
                new MessageMapperRegistry(new SimpleMessageMapperFactory((_) => new MyCommandMessageMapper()));

            messageMapperRegistry.Register <MyCommand, MyCommandMessageMapper>();

            _commandProcessor = CommandProcessorBuilder.With()
                                .Handlers(new HandlerConfiguration(new SubscriberRegistry(), new EmptyHandlerFactorySync()))
                                .DefaultPolicy()
                                .ExternalBus(new ExternalBusConfiguration(
                                                 new ProducerRegistry(new Dictionary <string, IAmAMessageProducer>()
            {
                { topic, _fakeMessageProducerWithPublishConfirmation },
            }),
                                                 messageMapperRegistry),
                                             _fakeOutboxSync)
                                .RequestContextFactory(new InMemoryRequestContextFactory())
                                .Build();
        }
        public CommandProcessorPostBoxBulkClearAsyncTests()
        {
            var myCommand = new MyCommand {
                Value = "Hello World"
            };
            var myCommand2 = new MyCommand {
                Value = "Hello World 2"
            };

            _fakeOutboxSync = new FakeOutboxSync();
            _fakeMessageProducerWithPublishConfirmation = new FakeMessageProducerWithPublishConfirmation();

            var topic  = "MyCommand";
            var topic2 = "MyCommand2";

            _message = new Message(
                new MessageHeader(myCommand.Id, topic, MessageType.MT_COMMAND),
                new MessageBody(JsonSerializer.Serialize(myCommand, JsonSerialisationOptions.Options))
                );

            _message2 = new Message(
                new MessageHeader(myCommand.Id, topic2, MessageType.MT_COMMAND),
                new MessageBody(JsonSerializer.Serialize(myCommand2, JsonSerialisationOptions.Options))
                );

            var messageMapperRegistry = new MessageMapperRegistry(new SimpleMessageMapperFactory((_) => new MyCommandMessageMapper()));

            messageMapperRegistry.Register <MyCommand, MyCommandMessageMapper>();

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

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

            _commandProcessor = new CommandProcessor(
                new InMemoryRequestContextFactory(),
                new PolicyRegistry {
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicy }, { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                _fakeOutboxSync,
                new ProducerRegistry(new Dictionary <string, IAmAMessageProducer>()
            {
                { topic, _fakeMessageProducerWithPublishConfirmation }, { topic2, _fakeMessageProducerWithPublishConfirmation }
            }));
        }
        public CommandProcessorNoMessageMapperTests()
        {
            _myCommand.Value = "Hello World";

            _fakeOutbox = new FakeOutboxSync();
            _fakeMessageProducerWithPublishConfirmation = new FakeMessageProducerWithPublishConfirmation();

            const string topic = "MyCommand";

            _message = new Message(
                new MessageHeader(_myCommand.Id, topic, MessageType.MT_COMMAND),
                new MessageBody(JsonSerializer.Serialize(_myCommand, JsonSerialisationOptions.Options))
                );

            var messageMapperRegistry = new MessageMapperRegistry(new SimpleMessageMapperFactory((_) => new MyCommandMessageMapper()));

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

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

            _commandProcessor = new CommandProcessor(
                new InMemoryRequestContextFactory(),
                new PolicyRegistry {
                { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                _fakeOutbox,
                new ProducerRegistry(new Dictionary <string, IAmAMessageProducer>()
            {
                { topic, _fakeMessageProducerWithPublishConfirmation },
            }));
        }
        public CommandProcessorCallTests()
        {
            _myRequest.RequestValue = "Hello World";

            _fakeMessageProducerWithPublishConfirmation = new FakeMessageProducerWithPublishConfirmation();

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

            var body = new MessageBody(JsonSerializer.Serialize(new MyRequestDTO(_myRequest.Id.ToString(), _myRequest.RequestValue), JsonSerialisationOptions.Options));

            _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 TestHandlerFactorySync <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 });

            var replySubs = new List <Subscription>
            {
                new Subscription <MyResponse>()
            };

            _commandProcessor = new CommandProcessor(
                subscriberRegistry,
                handlerFactory,
                new InMemoryRequestContextFactory(),
                new PolicyRegistry {
                { CommandProcessor.RETRYPOLICY, retryPolicy }, { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            },
                messageMapperRegistry,
                new InMemoryOutbox(),
                new ProducerRegistry(new Dictionary <string, IAmAMessageProducer>()
            {
                { topic, _fakeMessageProducerWithPublishConfirmation },
            }),
                replySubs,
                responseChannelFactory: inMemoryChannelFactory);

            PipelineBuilder <MyRequest> .ClearPipelineCache();
        }