< Summary

Class:Microsoft.Azure.ServiceBus.ClientEntity
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\ClientEntity.cs
Covered lines:10
Uncovered lines:26
Coverable lines:36
Total lines:162
Line coverage:27.7% (10 of 36)
Covered branches:1
Total branches:12
Branch coverage:8.3% (1 of 12)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%50%
get_IsClosedOrClosing()-0%100%
set_IsClosedOrClosing(...)-0%100%
get_OwnsConnection()-0%100%
get_ClientId()-100%100%
get_RetryPolicy()-100%100%
CloseAsync()-0%0%
GetNextId()-100%100%
GenerateClientId(...)-100%100%
ThrowIfClosed()-0%0%
UpdateClientId(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\ClientEntity.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Threading;
 9    using System.Threading.Tasks;
 10    using Core;
 11
 12    /// <summary>
 13    /// Contract for all client entities with Open-Close/Abort state m/c
 14    /// main-purpose: closeAll related entities
 15    /// </summary>
 16    public abstract class ClientEntity : IClientEntity
 17    {
 18        static int nextId;
 19        readonly string clientTypeName;
 20        readonly object syncLock;
 21        bool isClosedOrClosing;
 22
 1223        protected ClientEntity(string clientTypeName, string postfix, RetryPolicy retryPolicy)
 24        {
 1225            this.clientTypeName = clientTypeName;
 1226            this.ClientId = GenerateClientId(clientTypeName, postfix);
 1227            this.RetryPolicy = retryPolicy ?? RetryPolicy.Default;
 1228            this.syncLock = new object();
 1229        }
 30
 31        /// <summary>
 32        /// Returns true if the client is closed or closing.
 33        /// </summary>
 34        public bool IsClosedOrClosing
 35        {
 36            get
 37            {
 038                lock (syncLock)
 39                {
 040                    return isClosedOrClosing;
 41                }
 042            }
 43            internal set
 44            {
 045                lock (syncLock)
 46                {
 047                    isClosedOrClosing = value;
 048                }
 049            }
 50        }
 51
 52        /// <summary>
 53        /// Connection object to the service bus namespace.
 54        /// </summary>
 55        public abstract ServiceBusConnection ServiceBusConnection { get; }
 56
 57        /// <summary>
 58        /// Returns true if connection is owned and false if connection is shared.
 59        /// </summary>
 060        public bool OwnsConnection { get; internal set; }
 61
 62        /// <summary>
 63        /// Gets the name of the entity.
 64        /// </summary>
 65        public abstract string Path { get; }
 66
 67        /// <summary>
 68        /// Duration after which individual operations will timeout.
 69        /// </summary>
 70        public abstract TimeSpan OperationTimeout { get; set; }
 71
 72        /// <summary>
 73        /// Gets the ID to identify this client. This can be used to correlate logs and exceptions.
 74        /// </summary>
 75        /// <remarks>Every new client has a unique ID (in that process).</remarks>
 3676        public string ClientId { get; private set; }
 77
 78        /// <summary>
 79        /// Gets the <see cref="ServiceBus.RetryPolicy"/> defined on the client.
 80        /// </summary>
 1281        public RetryPolicy RetryPolicy { get; }
 82
 83        /// <summary>
 84        /// Closes the Client. Closes the connections opened by it.
 85        /// </summary>
 86        public async Task CloseAsync()
 87        {
 088            var callClose = false;
 089            lock (this.syncLock)
 90            {
 091                if (!this.IsClosedOrClosing)
 92                {
 093                    this.IsClosedOrClosing = true;
 094                    callClose = true;
 95                }
 096            }
 97
 098            if (callClose)
 99            {
 0100                await this.OnClosingAsync().ConfigureAwait(false);
 0101                if (OwnsConnection && this.ServiceBusConnection.IsClosedOrClosing == false)
 102                {
 0103                    await this.ServiceBusConnection.CloseAsync().ConfigureAwait(false);
 104                }
 105            }
 0106        }
 107
 108        /// <summary>
 109        /// Gets a list of currently registered plugins for this client.
 110        /// </summary>
 111        public abstract IList<ServiceBusPlugin> RegisteredPlugins { get; }
 112
 113        /// <summary>
 114        /// Registers a <see cref="ServiceBusPlugin"/> to be used with this client.
 115        /// </summary>
 116        /// <param name="serviceBusPlugin">The <see cref="ServiceBusPlugin"/> to register.</param>
 117        public abstract void RegisterPlugin(ServiceBusPlugin serviceBusPlugin);
 118
 119        /// <summary>
 120        /// Unregisters a <see cref="ServiceBusPlugin"/>.
 121        /// </summary>
 122        /// <param name="serviceBusPluginName">The name <see cref="ServiceBusPlugin.Name"/> to be unregistered</param>
 123        public abstract void UnregisterPlugin(string serviceBusPluginName);
 124
 125        protected abstract Task OnClosingAsync();
 126
 127        protected static long GetNextId()
 128        {
 12129            return Interlocked.Increment(ref nextId);
 130        }
 131
 132        /// <summary>
 133        /// Generates a new client id that can be used to identify a specific client in logs and error messages.
 134        /// </summary>
 135        /// <param name="postfix">Information that can be appended by the client.</param>
 136        protected static string GenerateClientId(string clientTypeName, string postfix = "")
 137        {
 12138            return $"{clientTypeName}{GetNextId()}{postfix}";
 139        }
 140
 141        /// <summary>
 142        /// Throw an OperationCanceledException if the object is Closing.
 143        /// </summary>
 144        protected virtual void ThrowIfClosed()
 145        {
 0146            this.ServiceBusConnection.ThrowIfClosed();
 0147            if (this.IsClosedOrClosing)
 148            {
 0149                throw new ObjectDisposedException($"{this.clientTypeName} with Id '{this.ClientId}' has already been clo
 150            }
 0151        }
 152
 153        /// <summary>
 154        /// Updates the client id.
 155        /// </summary>
 156        internal void UpdateClientId(string newClientId)
 157        {
 0158            MessagingEventSource.Log.UpdateClientId(this.ClientId, newClientId);
 0159            this.ClientId = newClientId;
 0160        }
 161    }
 162}