< Summary

Class:Microsoft.Azure.Batch.BatchClient
Assembly:Microsoft.Azure.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\BatchClient.cs
Covered lines:47
Uncovered lines:13
Coverable lines:60
Total lines:300
Line coverage:78.3% (47 of 60)
Covered branches:9
Total branches:14
Branch coverage:64.2% (9 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-0%100%
.ctor(...)-100%100%
.ctor(...)-0%100%
get_CustomBehaviors()-100%100%
set_CustomBehaviors(...)-100%100%
get_ApplicationOperations()-100%100%
get_CertificateOperations()-100%100%
get_JobOperations()-100%100%
get_JobScheduleOperations()-100%100%
get_PoolOperations()-100%100%
get_Utilities()-100%100%
Open(...)-66.67%50%
Open(...)-0%0%
Open(...)-66.67%50%
Dispose()-100%100%
Dispose(...)-90.91%83.33%
GetStateThrowIfNotOpen()-100%100%
get_ProtocolLayer()-100%100%
set_ProtocolLayer(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\BatchClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for license information.
 3
 4namespace Microsoft.Azure.Batch
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Threading.Tasks;
 9    using Protocol;
 10    using Rest;
 11
 12    /// <summary>
 13    /// The dispose pattern sets all references to null.
 14    /// Put all references into this box.
 15    ///
 16    /// ONLY ACCESS VIA GetStateThrowIfNotOpen() method!
 17    ///
 18    /// </summary>
 19    internal class BatchClientDisposableStateBox
 20    {
 21        private readonly BatchClient _parentBatchClient;
 22        private readonly Lazy<ApplicationOperations> _applicationOperations;
 23        private readonly Lazy<CertificateOperations> _certificateOperations;
 24        private readonly Lazy<JobOperations> _jobOperations;
 25        private readonly Lazy<JobScheduleOperations>  _jobScheduleOperations;
 26        private readonly Lazy<PoolOperations> _poolOperations;
 27        private readonly Lazy<Utilities> _utilities;
 28
 29        public IList<BatchClientBehavior> CustomBehaviors;
 30        public IProtocolLayer ProtocolLayer;
 31
 32        public BatchClientDisposableStateBox(BatchClient parentBatchClient)
 33        {
 34            this._parentBatchClient = parentBatchClient;
 35            this._applicationOperations = new Lazy<ApplicationOperations>(() => new ApplicationOperations(this._parentBa
 36            this._certificateOperations = new Lazy<CertificateOperations>(() => new CertificateOperations(this._parentBa
 37            this._jobOperations = new Lazy<JobOperations>(() => new JobOperations(this._parentBatchClient, this.CustomBe
 38            this._jobScheduleOperations = new Lazy<JobScheduleOperations>(() => new JobScheduleOperations(this._parentBa
 39            this._poolOperations = new Lazy<PoolOperations>(() => new PoolOperations(this._parentBatchClient, this.Custo
 40            this._utilities = new Lazy<Utilities>(() => new Utilities(this._parentBatchClient, this.CustomBehaviors));
 41
 42            this.CustomBehaviors = new List<BatchClientBehavior>();
 43        }
 44
 45        public ApplicationOperations ApplicationOperations => this._applicationOperations.Value;
 46        public CertificateOperations CertificateOperations => this._certificateOperations.Value;
 47        public JobOperations JobOperations => this._jobOperations.Value;
 48        public JobScheduleOperations JobScheduleOperations => this._jobScheduleOperations.Value;
 49        public PoolOperations PoolOperations => this._poolOperations.Value;
 50        public Utilities Utilities => this._utilities.Value;
 51    }
 52
 53    /// <summary>
 54    /// A client for an Azure Batch account, used to access the Batch service.
 55    /// </summary>
 56    public class BatchClient : IDisposable
 57    {
 58        private BatchClientDisposableStateBox _disposableStateBox;  // null state box signals that the instance is close
 59        private bool _disposed = false;  // used for dispose pattern
 65360        private readonly object _closeLocker = new object();
 61
 62#region // constructors
 63
 65364        private BatchClient()
 65        {
 65366            _disposableStateBox = new BatchClientDisposableStateBox(this);
 67
 68            //
 69            // Add custom behaviors which are by default on every batch client
 70            //
 65371            this.CustomBehaviors.Add(RetryPolicyProvider.ExponentialRetryProvider(TimeSpan.FromSeconds(1), 6));
 72
 73            //Add default AddTaskResultHandler
 65374            this.CustomBehaviors.Add(new AddTaskCollectionResultHandler(AddTaskCollectionResultHandler.DefaultAddTaskCol
 65375        }
 76
 13877        private BatchClient(Auth.BatchSharedKeyCredentials credentials) : this()
 78        {
 13879            ServiceClientCredentials proxyCredentials = new Protocol.BatchSharedKeyCredential(credentials.AccountName, c
 13880            this.ProtocolLayer = new ProtocolLayer(credentials.BaseUrl, proxyCredentials);
 13881        }
 82
 083        private BatchClient(Auth.BatchTokenCredentials credentials) : this()
 84        {
 085            ServiceClientCredentials proxyCredentials = new TokenCredentials(new BatchTokenProvider(credentials.TokenPro
 086            this.ProtocolLayer = new ProtocolLayer(credentials.BaseUrl, proxyCredentials);
 087        }
 88
 89        private BatchClient(Protocol.BatchServiceClient customRestClient)
 990            : this()
 91        {
 992            this.ProtocolLayer = new ProtocolLayer(customRestClient);
 993        }
 94
 95        /// <summary>
 96        /// Holds the protocol layer to be used for this client instance.
 97        /// This enables "mock"ing the protocol layer for testing.
 98        /// </summary>
 99        internal BatchClient(IProtocolLayer protocolLayer)
 0100            : this()
 101        {
 0102            this.ProtocolLayer = protocolLayer;
 0103        }
 104
 105#endregion  Constructors
 106
 107#region IInheritedBehaviors
 108
 109        /// <summary>
 110        /// Gets or sets a list of behaviors that modify or customize requests to the Batch service.
 111        /// </summary>
 112        /// <remarks>
 113        /// <para>These behaviors are inherited by child objects.</para>
 114        /// <para>Modifications are applied in the order of the collection. The last write wins.</para>
 115        /// </remarks>
 116        public IList<BatchClientBehavior> CustomBehaviors
 117        {
 118            get
 119            {
 7505120                return GetStateThrowIfNotOpen().CustomBehaviors;
 121            }
 122            set
 123            {
 393124                GetStateThrowIfNotOpen().CustomBehaviors = value;
 393125            }
 126        }
 127
 128#endregion  IInheritedBehaviors
 129
 130#region // BatchClient
 131
 132        /// <summary>
 133        /// Gets an <see cref="ApplicationOperations"/> for performing application-related operations on the associated 
 134        /// </summary>
 5135        public ApplicationOperations ApplicationOperations => GetStateThrowIfNotOpen().ApplicationOperations;
 136
 137        /// <summary>
 138        /// Gets a <see cref="CertificateOperations"/> for performing certificate-related operations on the associated a
 139        /// </summary>
 13140        public CertificateOperations CertificateOperations => GetStateThrowIfNotOpen().CertificateOperations;
 141
 142        /// <summary>
 143        /// Gets a <see cref="JobOperations"/> for performing job-related operations on the associated account.
 144        /// </summary>
 1065145        public JobOperations JobOperations => GetStateThrowIfNotOpen().JobOperations;
 146
 147        /// <summary>
 148        /// Gets a <see cref="JobScheduleOperations"/> for performing job schedule-related operations on the associated 
 149        /// </summary>
 25150        public JobScheduleOperations JobScheduleOperations => GetStateThrowIfNotOpen().JobScheduleOperations;
 151
 152        /// <summary>
 153        /// Gets a <see cref="PoolOperations"/> for performing pool-related operations on the associated account.
 154        /// </summary>
 50155        public PoolOperations PoolOperations => GetStateThrowIfNotOpen().PoolOperations;
 156
 157        /// <summary>
 158        /// Gets a <see cref="Utilities"/> object containing utility methods for orchestrating multiple Batch operations
 159        /// </summary>
 5160        public Utilities Utilities => GetStateThrowIfNotOpen().Utilities;
 161
 162        /// <summary>
 163        /// Creates an instance of <see cref="BatchClient" />.
 164        /// </summary>
 165        /// <param name="credentials">The Batch account credentials.</param>
 166        /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns>
 167        public static BatchClient Open(Auth.BatchSharedKeyCredentials credentials)
 168        {
 138169            if (null == credentials)
 170            {
 0171                throw new ArgumentNullException(nameof(credentials));
 172            }
 173
 138174            return new BatchClient(credentials);
 175        }
 176
 177        /// <summary>
 178        /// Creates an instance of <see cref="BatchClient" />.
 179        /// </summary>
 180        /// <param name="credentials">The Azure Active Directory Batch account credentials.</param>
 181        /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns>
 182        public static BatchClient Open(Auth.BatchTokenCredentials credentials)
 183        {
 0184            if (null == credentials)
 185            {
 0186                throw new ArgumentNullException(nameof(credentials));
 187            }
 188
 0189            return new BatchClient(credentials);
 190        }
 191
 192        /// <summary>
 193        /// Blocking call that creates an instance of <see cref="BatchClient"/> associated with the specified <see cref=
 194        /// </summary>
 195        /// <param name="restClient">The instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/> to 
 196        /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns>
 197        public static BatchClient Open(Protocol.BatchServiceClient restClient)
 198        {
 9199            if (null == restClient)
 200            {
 0201                throw new ArgumentNullException(nameof(restClient));
 202            }
 203
 9204            return new BatchClient(restClient);
 205        }
 206
 207#endregion // BatchClient
 208
 209#region // IDisposable
 210
 211        /// <summary>
 212        /// Releases the unmanaged resources and disposes of the managed resources used by the <see cref="Microsoft.Azur
 213        /// </summary>
 214        public void Dispose()
 215        {
 145216            Dispose(true);
 217
 145218            GC.SuppressFinalize(this);
 145219        }
 220
 221        /// <summary>
 222        /// Releases unmanaged resources used by the <see cref="BatchClient"/>, and optionally disposes of managed resou
 223        /// </summary>
 224        /// <param name="disposing">Indicates whether the object is being disposed or finalized.  If true, the object is
 225        /// being disposed and can dispose managed resource.  If false, the object is being finalized and should only
 226        /// release unmanaged resources.</param>
 227        protected virtual void Dispose(bool disposing)
 228        {
 145229            if (_disposed)
 230            {
 0231                return;
 232            }
 233
 145234            if (disposing)
 235            {
 236                // IDisposable only section
 237
 145238                lock (this._closeLocker)
 239                {
 145240                    if (this._disposableStateBox != null)
 241                    {
 145242                        IProtocolLayer localProto = this.ProtocolLayer;
 145243                        localProto.Dispose();
 244
 145245                        this._disposableStateBox = null; // null state box signals that the instance is closed
 246                    }
 145247                }
 248            }
 249
 145250            _disposed = true;
 145251        }
 252
 253#endregion // IDisposable
 254
 255#region internal/private methods
 256
 257        /// <summary>
 258        /// Enforces that current instance is not "close".
 259        /// All access to disposable state should go through this routine.
 260        /// </summary>
 261        /// <returns></returns>
 262        internal BatchClientDisposableStateBox GetStateThrowIfNotOpen()
 263        {
 9584264            BatchClientDisposableStateBox localState = _disposableStateBox;
 265
 9584266            if (null != localState)
 267            {
 9572268                return localState;
 269            }
 270
 271            // TODO: BatchException is not yet ready for this... do we need to create simpler BatchExceptions for stuff 
 12272            throw new InvalidOperationException(BatchErrorMessages.BatchClientIsClosed);
 273        }
 274
 275        /// <summary>
 276        /// Holds the protocol layer to be used for this client instance.
 277        /// This enables "mock"ing the protocol layer for testing.
 278        ///
 279        /// Since 100% of all calls indirect through this property, it
 280        /// provides a single place to immediately stop all (new) call attempts
 281        /// when the underlying BatchClient is closed.
 282        /// </summary>
 283        internal IProtocolLayer ProtocolLayer
 284        {
 285            get
 286            {
 376287                IProtocolLayer localProto = GetStateThrowIfNotOpen().ProtocolLayer;
 288
 376289                return localProto;
 290            }
 291
 292            private set
 293            {
 147294                GetStateThrowIfNotOpen().ProtocolLayer = value;
 147295            }
 296        }
 297
 298#endregion internal/private methods
 299    }
 300}