public override async Task InvokeAsync(IPipeContext context, CancellationToken token = default(CancellationToken))
        {
            var config = ExtractFromContextProperty(context) ?? ExtractConfigFromMessageType(context);

            if (config == null)
            {
                Logger.LogInformation("Unable to extract configuration for Subscriber.");
            }

            var action = GetConfigurationAction(context);

            if (action != null)
            {
                Logger.LogInformation("Configuration action for {queueName} found.", config?.Consume.QueueName);
                var builder = new ConsumerConfigurationBuilder(config);
                action(builder);
                config = builder.Config;
            }

            context.Properties.TryAdd(PipeKey.ConsumerConfiguration, config);
            context.Properties.TryAdd(PipeKey.ConsumeConfiguration, config.Consume);
            context.Properties.TryAdd(PipeKey.QueueDeclaration, config.Queue);
            context.Properties.TryAdd(PipeKey.ExchangeDeclaration, config.Exchange);

            await Next.InvokeAsync(context, token);
        }
Пример #2
0
        private ConsumerConfiguration(ConsumerConfigurationBuilder builder)
        {
            _deliverPolicy = builder._deliverPolicy;
            _ackPolicy     = builder._ackPolicy;
            _replayPolicy  = builder._replayPolicy;

            Description     = builder._description;
            Durable         = builder._durable;
            DeliverSubject  = builder._deliverSubject;
            DeliverGroup    = builder._deliverGroup;
            FilterSubject   = builder._filterSubject;
            SampleFrequency = builder._sampleFrequency;

            StartTime         = builder._startTime;
            AckWait           = builder._ackWait;
            IdleHeartbeat     = builder._idleHeartbeat;
            MaxExpires        = builder._maxExpires;
            InactiveThreshold = builder._inactiveThreshold;
            _flowControl      = builder._flowControl;
            _headersOnly      = builder._headersOnly;

            _startSeq       = builder._startSeq;
            _maxDeliver     = builder._maxDeliver;
            _rateLimitBps   = builder._rateLimitBps;
            _maxAckPending  = builder._maxAckPending;
            _maxPullWaiting = builder._maxPullWaiting;
            _maxBatch       = builder._maxBatch;
            _maxBytes       = builder._maxBytes;

            Backoff = builder._backoff;
        }
        public IRequestConfigurationBuilder ConsumeResponse(Action <IConsumerConfigurationBuilder> consume)
        {
            var builder = new ConsumerConfigurationBuilder(Config.Response);

            consume(builder);
            Config.Response = builder.Config;
            return(this);
        }
        public void returns_expected_auto_commit_when_not_set()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithGroupId("foo")
                                .WithBootstrapServers("bar")
                                .Build();

            Assert.True(configuration.EnableAutoCommit);
        }
        public void returns_expected_auto_commit_when_configured_with_valid_value(string configValue, bool expected)
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithGroupId("foo")
                                .WithBootstrapServers("bar")
                                .WithConfiguration(ConfigurationKey.EnableAutoCommit, configValue)
                                .Build();

            Assert.Equal(expected, configuration.EnableAutoCommit);
        }
        public void Can_build_minimal_configuration()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithGroupId("foo")
                                .WithBootstrapServers("bar")
                                .Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "foo");
            AssertKeyValue(configuration, ConfigurationKey.BootstrapServers, "bar");
        }
        public void Can_ignore_out_of_scope_values_from_configuration_source()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithConfigurationSource(new ConfigurationSourceStub(
                                                             (key: ConfigurationKey.GroupId, value: "foo"),
                                                             (key: ConfigurationKey.BootstrapServers, value: "bar"),
                                                             (key: "dummy", value: "baz")
                                                             )).Build();

            AssertKeyValue(configuration, "dummy", null);
        }
        public void Can_register_message_handler()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithGroupId("foo")
                                .WithBootstrapServers("bar")
                                .RegisterMessageHandler <DummyMessage, DummyMessageHandler>("dummyTopic", nameof(DummyMessage))
                                .Build();

            var registration = configuration.MessageHandlerRegistry.GetRegistrationFor(nameof(DummyMessage));

            Assert.Equal(typeof(DummyMessageHandler), registration.HandlerInstanceType);
        }
        public void Can_overwrite_values_from_source()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithConfigurationSource(new ConfigurationSourceStub(
                                                             (key: ConfigurationKey.GroupId, value: "foo"),
                                                             (key: ConfigurationKey.BootstrapServers, value: "bar")
                                                             ))
                                .WithConfiguration(ConfigurationKey.GroupId, "baz")
                                .Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "baz");
        }
        public void Can_use_configuration_value_from_source_with_environment_naming_convention_and_prefix()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithConfigurationSource(new ConfigurationSourceStub(
                                                             (key: "DEFAULT_KAFKA_GROUP_ID", value: "foo"),
                                                             (key: "DEFAULT_KAFKA_BOOTSTRAP_SERVERS", value: "bar")
                                                             ))
                                .WithEnvironmentStyle("DEFAULT_KAFKA")
                                .Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "foo");
            AssertKeyValue(configuration, ConfigurationKey.BootstrapServers, "bar");
        }
        public void Can_use_configuration_value_from_source()
        {
            var sut = new ConsumerConfigurationBuilder();

            sut.WithConfigurationSource(new ConfigurationSourceStub(
                                            (key: ConfigurationKey.GroupId, value: "foo"),
                                            (key: ConfigurationKey.BootstrapServers, value: "bar")
                                            ));

            var configuration = sut.Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "foo");
            AssertKeyValue(configuration, ConfigurationKey.BootstrapServers, "bar");
        }
        public void Only_take_value_from_first_source_that_matches()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithConfigurationSource(new ConfigurationSourceStub(
                                                             (key: ConfigurationKey.GroupId, value: "foo"),
                                                             (key: ConfigurationKey.BootstrapServers, value: "bar"),
                                                             (key: "GROUP_ID", value: "baz")
                                                             ))
                                .WithNamingConvention(NamingConvention.Default)
                                .WithEnvironmentStyle()
                                .Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "foo");
        }
        public void Can_use_configuration_value_from_source_with_environment_naming_convention()
        {
            var sut = new ConsumerConfigurationBuilder();

            sut.WithConfigurationSource(new ConfigurationSourceStub(
                                            (key: "GROUP_ID", value: "foo"),
                                            (key: "BOOTSTRAP_SERVERS", value: "bar")
                                            ));
            sut.WithEnvironmentStyle();

            var configuration = sut.Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "foo");
            AssertKeyValue(configuration, ConfigurationKey.BootstrapServers, "bar");
        }
Пример #14
0
        public async Task Has_expected_number_of_creations_and_disposals_when_singleton()
        {
            var dummyMessage = new DummyMessage();
            var messageStub  = new TransportLevelMessageBuilder()
                               .WithType(nameof(DummyMessage))
                               .WithData(dummyMessage)
                               .Build();
            var messageResult = new MessageResultBuilder()
                                .WithTransportLevelMessage(messageStub)
                                .Build();

            var services = new ServiceCollection();

            services.AddTransient <Repository>();
            services.AddSingleton <IHostApplicationLifetime, DummyApplicationLifetime>();
            services.AddTransient <DummyMessageHandler>();
            services.AddLogging();

            var createCount  = 0;
            var disposeCount = 0;

            services.AddSingleton <ScopeSpy>(provider => new ScopeSpy(onCreate: () => createCount++, onDispose: () => disposeCount++));

            var serviceProvider = services.BuildServiceProvider();

            var consumerConfiguration = new ConsumerConfigurationBuilder()
                                        .WithGroupId("dummy")
                                        .WithBootstrapServers("dummy")
                                        .RegisterMessageHandler <DummyMessage, DummyMessageHandler>("dummyTopic", nameof(DummyMessage))
                                        .WithConsumerScopeFactory(_ => new ConsumerScopeFactoryStub(new ConsumerScopeStub(messageResult)))
                                        .WithUnitOfWorkFactory(new ServiceProviderUnitOfWorkFactory(serviceProvider))
                                        .Build();

            var consumer = new ConsumerBuilder()
                           .WithMessageHandlerRegistry(consumerConfiguration.MessageHandlerRegistry)
                           .WithUnitOfWorkFactory(consumerConfiguration.UnitOfWorkFactory)
                           .WithConsumerScopeFactory(consumerConfiguration.ConsumerScopeFactory)
                           .WithEnableAutoCommit(consumerConfiguration.EnableAutoCommit)
                           .Build();

            await consumer.ConsumeSingle(CancellationToken.None);

            await consumer.ConsumeSingle(CancellationToken.None);

            Assert.Equal(1, createCount);
            Assert.Equal(0, disposeCount);
        }
        public void Can_build_consumer_configuration()
        {
            var configuration = new ConsumerConfigurationBuilder()
                                .WithConfigurationSource(new ConfigurationSourceStub(
                                                             (key: "DEFAULT_KAFKA_GROUP_ID", value: "default_foo"),
                                                             (key: "SAMPLE_KAFKA_ENABLE_AUTO_COMMIT", value: "true"),
                                                             (key: ConfigurationKey.GroupId, value: "foo"),
                                                             (key: ConfigurationKey.BootstrapServers, value: "bar"),
                                                             (key: "dummy", value: "ignored")

                                                             ))
                                .WithNamingConvention(NamingConvention.Default)
                                .WithEnvironmentStyle("DEFAULT_KAFKA", "SAMPLE_KAFKA")
                                .WithConfiguration(ConfigurationKey.GroupId, "baz")
                                .Build();

            AssertKeyValue(configuration, ConfigurationKey.GroupId, "baz");
            AssertKeyValue(configuration, ConfigurationKey.BootstrapServers, "bar");
            AssertKeyValue(configuration, ConfigurationKey.EnableAutoCommit, "true");
            AssertKeyValue(configuration, "dummy", null);
        }
Пример #16
0
        public void Setup()
        {
            this.dependencyConfiguratorMock = new Mock <IDependencyConfigurator>();

            this.target = new ConsumerConfigurationBuilder(this.dependencyConfiguratorMock.Object);
        }
        public void Can_validate_configuration()
        {
            var sut = new ConsumerConfigurationBuilder();

            Assert.Throws <InvalidConfigurationException>(() => sut.Build());
        }