Exemplo n.º 1
0
        // create new class that handle all cancel Logic: (Create pipe too)
        // - Immediate cancel: Cancel token given to the pipeline, and wait it to leave (catch in finally ?)
        // - Do not cancel token, but when it's been cancelled, if a task is in progress then it should wait the task to complete.

        private string BeginConsumeQueue <T>(
            IModel channel,
            string queueName,
            ConsumerOptions <T> consumerOptions,
            ProcessorMessageDelegate <T> messageProcessor,
            ActiveMessageProcessorCanceller activeMessageProcessorCanceller
            ) where T : class
        {
            var consumer = new AsyncEventingBasicConsumer(channel);

            consumer.Received += async(_, message) =>
            {
                var consumerPipeContext = new ConsumerPipeContext <T>(_options.RabbitMqConnectionManager, channel, message, messageProcessor, activeMessageProcessorCanceller);
                await ConsumerPipe <T> .ExecutePipelineAsync(consumerPipeContext, consumerOptions.BuildPipeline());
            };

            if (consumerOptions.PrefetchCount.HasValue)
            {
                channel.BasicQos(0, consumerOptions.PrefetchCount.Value, false);
            }

            return(channel.BasicConsume(queueName, false, consumer));
        }
        public void Setup()
        {
            _pipe = new FastRetryMessageAcknowledgementPipe <string>(2);

            _nextPipeMock    = new Mock <IConsumerPipe <string> >(MockBehavior.Strict);
            _fakePipeline    = new ReadOnlyMemory <IConsumerPipe <string> >(new[] { _nextPipeMock.Object });
            _basicProperties = new FakeBasicProperties();

            _basicDeliverEventArgs = new BasicDeliverEventArgs
            {
                DeliveryTag     = DeliveryTag,
                RoutingKey      = RoutingKey,
                Body            = Body,
                BasicProperties = _basicProperties
            };
            _rabbitMqConnectionManager = new Mock <IRabbitMqConnectionManager>(MockBehavior.Strict);
            _channelMock         = new Mock <IModel>(MockBehavior.Strict);
            _consumerPipeContext = new ConsumerPipeContext <string>(_rabbitMqConnectionManager.Object, _channelMock.Object, _basicDeliverEventArgs, (items, message, ct) => Task.CompletedTask, Mock.Of <IActiveMessageProcessorCanceller>());

            _publishChannelMock = new Mock <IModel>();
            _rabbitMqConnectionManager.Setup(m => m.AcquireChannel(ChannelType.Publish))
            .ReturnsAsync(() => new ChannelContainer(Mock.Of <IRabbitMqChannelManager>(), _publishChannelMock.Object));
        }