示例#1
0
        private static KafkaSubscriber <IMessage> BuildSUT(
            QueueReferences queueRefs,
            IConsumer <Guid, byte[]> consumer,
            IKafkaMessageHandler messageHandler = null)
        {
            var config = new ConsumerConfig()
            {
                GroupId = "group id"
            };
            var builder = NSubstitute.Substitute.ForPartsOf <ConsumerBuilder <Guid, byte[]> >(config);

            builder.When(b => b.Build()).DoNotCallBase();
            builder.Build().Returns(consumer);

            var builderFactory = NSubstitute.Substitute.For <IConsumerBuilderFactory>();

            builderFactory.Create <IMessage, Guid, byte[]>().Returns(builder);

            messageHandler ??= NSubstitute.Substitute.For <IKafkaMessageHandler>();

            var queueReferenceFactory = NSubstitute.Substitute.For <IQueueReferenceFactory>();

            queueReferenceFactory.Create <IMessage>().ReturnsForAnyArgs(queueRefs);

            var logger = NSubstitute.Substitute.For <ILogger <KafkaSubscriber <IMessage> > >();

            return(new KafkaSubscriber <IMessage>(builderFactory, queueReferenceFactory, messageHandler, logger));
        }
示例#2
0
        public KafkaSubscriber(IConsumerBuilderFactory builderFactory,
                               IQueueReferenceFactory queueReferenceFactory,
                               IKafkaMessageHandler messageHandler,
                               ILogger <KafkaSubscriber <TM> > logger,
                               KafkaSubscriberConfig config = null)
        {
            if (builderFactory is null)
            {
                throw new ArgumentNullException(nameof(builderFactory));
            }

            if (queueReferenceFactory is null)
            {
                throw new ArgumentNullException(nameof(queueReferenceFactory));
            }

            var builder = builderFactory.Create <TM, Guid, byte[]>();

            _consumer        = builder.Build();
            _queueReferences = queueReferenceFactory.Create <TM>();
            _messageHandler  = messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
            _logger          = logger ?? throw new ArgumentNullException(nameof(logger));
            _config          = config ?? KafkaSubscriberConfig.Default;
        }