< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.RabbitMqTransportPump
Assembly: CoreWCF.RabbitMQ
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ/src/CoreWCF/Channels/RabbitmqTransportPump.cs
Line coverage
98%
Covered lines: 59
Uncovered lines: 1
Coverable lines: 60
Total lines: 119
Line coverage: 98.3%
Branch coverage
50%
Covered branches: 5
Total branches: 10
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
StartPumpAsync(...)50%2296.15%
StopPumpAsync(...)50%88100%
ConsumeMessage()100%11100%
GetContext(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ/src/CoreWCF/Channels/RabbitmqTransportPump.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Buffers;
 6using System.IO.Pipelines;
 7using System.Net;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using CoreWCF.Channels.Configuration;
 11using CoreWCF.Configuration;
 12using CoreWCF.Queue.Common;
 13using Microsoft.Extensions.DependencyInjection;
 14using Microsoft.Extensions.Logging;
 15using RabbitMQ.Client;
 16using RabbitMQ.Client.Events;
 17
 18namespace CoreWCF.Channels
 19{
 20    internal class RabbitMqTransportPump : QueueTransportPump
 21    {
 22        private const bool DefaultAutoAck = false;
 23        private readonly ILogger<RabbitMqTransportPump> _logger;
 24        private SslOption _sslOption;
 25        private QueueDeclareConfiguration _queueConfiguration;
 26        private ICredentials _credentials;
 27        private string _virtualHost;
 28        private ConnectionFactory _factory;
 29        private IConnection _connection;
 30        private IModel _channel;
 31        private EventingBasicConsumer _consumer;
 32        private readonly Uri _baseAddress;
 33        private bool _isAutoAck;
 34
 335        public RabbitMqTransportPump(
 336            IServiceProvider serviceProvider,
 337            IServiceDispatcher serviceDispatcher,
 338            SslOption sslOption,
 339            QueueDeclareConfiguration queueConfiguration,
 340            ICredentials credentials,
 341            string virtualHost)
 42        {
 343            _baseAddress = serviceDispatcher.BaseAddress;
 344            _logger = serviceProvider.GetRequiredService<ILogger<RabbitMqTransportPump>>();
 345            _sslOption = sslOption;
 346            _queueConfiguration = queueConfiguration;
 347            _credentials = credentials;
 348            _virtualHost = virtualHost;
 349            _isAutoAck = DefaultAutoAck;
 350        }
 51
 52        public override Task StartPumpAsync(QueueTransportContext queueTransportContext, CancellationToken token)
 53        {
 354            var rabbitMqTransport = queueTransportContext.QueueBindingElement as RabbitMqTransportBindingElement;
 355            if (rabbitMqTransport == null)
 56            {
 057                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rabbitMqTransport));
 58            }
 59
 360            var connectionSettings = RabbitMqConnectionSettings.FromUri(_baseAddress, _credentials, _sslOption, _virtual
 361            _factory = connectionSettings.GetConnectionFactory();
 362            _connection = _factory.CreateConnection();
 363            _channel = _connection.CreateModel();
 364            _channel.BasicQos(0, _queueConfiguration.PrefetchCount, _queueConfiguration.GlobalQosPrefetch);
 365            _channel.QueueDeclare(
 366                connectionSettings.QueueName,
 367                _queueConfiguration.Durable,
 368                _queueConfiguration.Exclusive,
 369                _queueConfiguration.AutoDelete,
 370                _queueConfiguration.ToDictionary());
 371            _channel.QueueBind(
 372                connectionSettings.QueueName,
 373                connectionSettings.Exchange,
 374                connectionSettings.RoutingKey,
 375                null);
 76
 377            _consumer = new EventingBasicConsumer(_channel);
 378            _consumer.Received += (_, ea) =>
 379            {
 380                ConsumeMessage(ea, queueTransportContext);
 681            };
 382            _channel.BasicConsume(connectionSettings.QueueName, DefaultAutoAck, _consumer);
 383            return Task.CompletedTask;
 84        }
 85
 86        public override Task StopPumpAsync(CancellationToken token)
 87        {
 388            _channel?.Close();
 389            _channel?.Dispose();
 90
 391            _connection?.Close();
 392            _connection?.Dispose();
 393            return Task.CompletedTask;
 94        }
 95
 96        private async void ConsumeMessage(BasicDeliverEventArgs eventArgs, QueueTransportContext queueTransportContext)
 97        {
 398            _logger.LogInformation("Receiving message from RabbitMQ");
 399            var reader = PipeReader.Create(new ReadOnlySequence<byte>(eventArgs.Body));
 3100            var deliveryTag = eventArgs.DeliveryTag;
 101
 3102            await queueTransportContext.QueueMessageDispatcher(GetContext(reader, queueTransportContext, deliveryTag));
 3103        }
 104
 105        private QueueMessageContext GetContext(PipeReader reader, QueueTransportContext transportContext, ulong delivery
 106        {
 3107            var receiveContext = new RabbitMqReceiveContext(deliveryTag, _channel, _isAutoAck, _logger);
 3108            var context = new QueueMessageContext
 3109            {
 3110                QueueMessageReader = reader,
 3111                LocalAddress = new EndpointAddress(transportContext.ServiceDispatcher.BaseAddress),
 3112                QueueTransportContext = transportContext,
 3113                ReceiveContext = receiveContext
 3114            };
 115
 3116            return context;
 117        }
 118    }
 119}