< Summary

Information
Class: IceRpc.Internal.LogProtocolConnectionDecorator
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Internal/LogProtocolConnectionDecorator.cs
Tag: 275_13775359185
Line coverage
95%
Covered lines: 58
Uncovered lines: 3
Coverable lines: 61
Total lines: 108
Line coverage: 95%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage
100%
Covered methods: 6
Total methods: 6
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsServer()100%11100%
ConnectAsync()50%2.01285%
DisposeAsync()100%22100%
InvokeAsync(...)100%11100%
ShutdownAsync()100%11100%
.ctor(...)100%11100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Internal/LogProtocolConnectionDecorator.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports;
 4using Microsoft.Extensions.Logging;
 5using System.Diagnostics;
 6using System.Net;
 7
 8namespace IceRpc.Internal;
 9
 10/// <summary>Provides a decorator that adds logging to the <see cref="IProtocolConnection" />.</summary>
 11internal class LogProtocolConnectionDecorator : IProtocolConnection
 12{
 6013    private bool IsServer => _remoteNetworkAddress is not null;
 14
 15    // _connectionInformation is not volatile because all correct callers of IProtocolConnection.ConnectAsync wait for
 16    // the connection establishment to complete (successfully or not) before calling any other method on
 17    // IProtocolConnection, including DisposeAsync.
 18    private TransportConnectionInformation? _connectionInformation;
 19
 20    private readonly IProtocolConnection _decoratee;
 21
 22    private readonly ILogger _logger;
 23
 24    private readonly EndPoint? _remoteNetworkAddress;
 25
 26    private readonly ServerAddress _serverAddress;
 27
 28    public async Task<(TransportConnectionInformation ConnectionInformation, Task ShutdownRequested)> ConnectAsync(
 29        CancellationToken cancellationToken)
 2830    {
 31        try
 2832        {
 2833            (_connectionInformation, Task shutdownRequested) = await _decoratee.ConnectAsync(cancellationToken)
 2834                .ConfigureAwait(false);
 35
 2436            _logger.LogConnectionConnected(
 2437                IsServer,
 2438                _connectionInformation.LocalNetworkAddress,
 2439                _connectionInformation.RemoteNetworkAddress);
 40
 2441            return (_connectionInformation, shutdownRequested);
 42        }
 443        catch (Exception exception)
 444        {
 445            if (_remoteNetworkAddress is null)
 446            {
 447                _logger.LogConnectionConnectFailed(_serverAddress, exception);
 448            }
 49            else
 050            {
 051                _logger.LogConnectionConnectFailed(_serverAddress, _remoteNetworkAddress, exception);
 052            }
 453            throw;
 54        }
 2455    }
 56
 57    public async ValueTask DisposeAsync()
 2858    {
 2859        await _decoratee.DisposeAsync().ConfigureAwait(false);
 60
 2861        if (_connectionInformation is not null)
 2462        {
 2463            _logger.LogConnectionDisposed(
 2464                IsServer,
 2465                _connectionInformation.LocalNetworkAddress,
 2466                _connectionInformation.RemoteNetworkAddress);
 2467        }
 2868    }
 69
 70    public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) =>
 1271        _decoratee.InvokeAsync(request, cancellationToken);
 72
 73    public async Task ShutdownAsync(CancellationToken cancellationToken)
 1274    {
 75        try
 1276        {
 1277            await _decoratee.ShutdownAsync(cancellationToken).ConfigureAwait(false);
 78
 879            Debug.Assert(_connectionInformation is not null);
 80
 881            _logger.LogConnectionShutdown(
 882                IsServer,
 883                _connectionInformation.LocalNetworkAddress,
 884                _connectionInformation.RemoteNetworkAddress);
 885        }
 486        catch (Exception exception) when (_connectionInformation is not null)
 487        {
 488            _logger.LogConnectionShutdownFailed(
 489                IsServer,
 490                _connectionInformation.LocalNetworkAddress,
 491                _connectionInformation.RemoteNetworkAddress,
 492                exception);
 493            throw;
 94        }
 895    }
 96
 2897    internal LogProtocolConnectionDecorator(
 2898        IProtocolConnection decoratee,
 2899        ServerAddress serverAddress,
 28100        EndPoint? remoteNetworkAddress,
 28101        ILogger logger)
 28102    {
 28103        _decoratee = decoratee;
 28104        _logger = logger;
 28105        _remoteNetworkAddress = remoteNetworkAddress;
 28106        _serverAddress = serverAddress;
 28107    }
 108}