< Summary

Information
Class: IceRpc.Internal.LogProtocolConnectionDecorator
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Internal/LogProtocolConnectionDecorator.cs
Tag: 592_20856082467
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{
 3013    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)
 1430    {
 31        try
 1432        {
 1433            (_connectionInformation, Task shutdownRequested) = await _decoratee.ConnectAsync(cancellationToken)
 1434                .ConfigureAwait(false);
 35
 1236            _logger.LogConnectionConnected(
 1237                IsServer,
 1238                _connectionInformation.LocalNetworkAddress,
 1239                _connectionInformation.RemoteNetworkAddress);
 40
 1241            return (_connectionInformation, shutdownRequested);
 42        }
 243        catch (Exception exception)
 244        {
 245            if (_remoteNetworkAddress is null)
 246            {
 247                _logger.LogConnectionConnectFailed(_serverAddress, exception);
 248            }
 49            else
 050            {
 051                _logger.LogConnectionConnectFailed(_serverAddress, _remoteNetworkAddress, exception);
 052            }
 253            throw;
 54        }
 1255    }
 56
 57    public async ValueTask DisposeAsync()
 1458    {
 1459        await _decoratee.DisposeAsync().ConfigureAwait(false);
 60
 1461        if (_connectionInformation is not null)
 1262        {
 1263            _logger.LogConnectionDisposed(
 1264                IsServer,
 1265                _connectionInformation.LocalNetworkAddress,
 1266                _connectionInformation.RemoteNetworkAddress);
 1267        }
 1468    }
 69
 70    public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) =>
 671        _decoratee.InvokeAsync(request, cancellationToken);
 72
 73    public async Task ShutdownAsync(CancellationToken cancellationToken)
 674    {
 75        try
 676        {
 677            await _decoratee.ShutdownAsync(cancellationToken).ConfigureAwait(false);
 78
 479            Debug.Assert(_connectionInformation is not null);
 80
 481            _logger.LogConnectionShutdown(
 482                IsServer,
 483                _connectionInformation.LocalNetworkAddress,
 484                _connectionInformation.RemoteNetworkAddress);
 485        }
 286        catch (Exception exception) when (_connectionInformation is not null)
 287        {
 288            _logger.LogConnectionShutdownFailed(
 289                IsServer,
 290                _connectionInformation.LocalNetworkAddress,
 291                _connectionInformation.RemoteNetworkAddress,
 292                exception);
 293            throw;
 94        }
 495    }
 96
 1497    internal LogProtocolConnectionDecorator(
 1498        IProtocolConnection decoratee,
 1499        ServerAddress serverAddress,
 14100        EndPoint? remoteNetworkAddress,
 14101        ILogger logger)
 14102    {
 14103        _decoratee = decoratee;
 14104        _logger = logger;
 14105        _remoteNetworkAddress = remoteNetworkAddress;
 14106        _serverAddress = serverAddress;
 14107    }
 108}