< Summary

Class:Microsoft.Azure.Batch.Protocol.BatchRequestBase`2
Assembly:Microsoft.Azure.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\Protocol\BatchRequest.cs
Covered lines:87
Uncovered lines:4
Coverable lines:91
Total lines:371
Line coverage:95.6% (87 of 91)
Covered branches:20
Total branches:24
Branch coverage:83.3% (20 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_CustomHeaders()-100%100%
get_ServiceRequestFunc()-100%100%
set_ServiceRequestFunc(...)-100%100%
get_Options()-100%100%
set_Options(...)-100%100%
Microsoft.Azure.Batch.Protocol.IBatchRequest.get_Options()-100%100%
get_RestClient()-100%100%
get_RetryPolicy()-100%100%
set_RetryPolicy(...)-100%100%
get_OperationContext()-100%100%
get_Timeout()-100%100%
set_Timeout(...)-100%100%
get_CancellationToken()-100%100%
set_CancellationToken(...)-100%100%
get_ClientRequestIdProvider()-100%100%
set_ClientRequestIdProvider(...)-100%100%
.ctor(...)-100%100%
ExecuteRequestAsync()-90.24%80%
ThrowIfRequestExecutionHasStarted()-100%100%
ExecuteRequestWithCancellationAsync()-100%100%
ApplyClientRequestIdBehaviorToParams()-100%100%

File(s)

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

#LineLine coverage
 1namespace Microsoft.Azure.Batch.Protocol
 2{
 3    using System;
 4    using System.Collections.Generic;
 5    using System.Diagnostics;
 6    using System.Runtime.ExceptionServices;
 7    using System.Threading;
 8    using System.Threading.Tasks;
 9    using Common;
 10    using Microsoft.Rest.Azure;
 11    using Models;
 12
 13    /// <summary>
 14    /// A base class for all Batch service requests. Represents the information required to make a particular call with 
 15    /// </summary>
 16    /// <typeparam name="TOptions">The type of the parameters passed outside the request body associated with the reques
 17    /// <typeparam name="TResponse">The response type expected from the request.</typeparam>
 18    public abstract class BatchRequestBase<TOptions, TResponse> : IBatchRequest<TResponse>
 19        where TOptions : IOptions, new()
 20        where TResponse : IAzureOperationResponse
 21    {
 22        private volatile bool hasRequestExecutionStarted;
 23        private Func<CancellationToken, Task<TResponse>> serviceRequestFunc;
 24        private TOptions options;
 25        private IRetryPolicy retryPolicy;
 26        private TimeSpan timeout;
 27        private CancellationToken cancellationToken;
 28        private ClientRequestIdProvider clientRequestIdProvider;
 29
 30        /// <summary>
 31        /// Gets the headers used for the request.
 32        /// </summary>
 25433        public Dictionary<string, List<string>> CustomHeaders { get; private set; }
 34
 35        /// <summary>
 36        /// Gets or sets the function which will create a <see cref="Task"/> calling the Batch service.
 37        /// </summary>
 38        public Func<CancellationToken, Task<TResponse>> ServiceRequestFunc
 39        {
 19540            get { return this.serviceRequestFunc; }
 41            set
 42            {
 38743                this.ThrowIfRequestExecutionHasStarted();
 38644                this.serviceRequestFunc = value;
 38645            }
 46        }
 47
 48        /// <summary>
 49        /// Gets or sets the options used for the request.
 50        /// </summary>
 51        public TOptions Options
 52        {
 34953            get { return this.options; }
 54            set
 55            {
 23956                this.ThrowIfRequestExecutionHasStarted();
 23857                this.options = value;
 23858            }
 59        }
 60
 61        /// <summary>
 62        /// Gets the options needed by the REST proxy for the current request.
 63        /// </summary>
 5164        IOptions IBatchRequest.Options { get { return Options; } }
 65
 66        /// <summary>
 67        /// Gets the REST client that will be used for this request.
 68        /// </summary>
 25469        public Protocol.BatchServiceClient RestClient { get; private set; }
 70
 71        /// <summary>
 72        /// Gets or sets the retry policy to be applied.
 73        /// Null means no retries will be attempted.
 74        /// </summary>
 75        public IRetryPolicy RetryPolicy
 76        {
 8977            get { return this.retryPolicy; }
 78            set
 79            {
 2680                this.ThrowIfRequestExecutionHasStarted();
 2581                this.retryPolicy = value;
 2582            }
 83        }
 84
 85        /// <summary>
 86        /// Gets the operation context associated with this <see cref="IBatchRequest"/>.
 87        /// </summary>
 31788        public OperationContext OperationContext { get; private set; }
 89
 90        /// <summary>
 91        /// Gets or sets the client side timeout for a request to the Batch service.
 92        /// </summary>
 93        /// <remarks>
 94        /// <para>
 95        /// This timeout applies to a single Batch service request; if a retry policy is specified, then each retry will
 96        /// full duration of this value.
 97        /// </para>
 98        /// </remarks>
 99        public TimeSpan Timeout
 100        {
 204101            get { return this.timeout; }
 102            set
 103            {
 268104                this.ThrowIfRequestExecutionHasStarted();
 267105                this.timeout = value;
 267106            }
 107        }
 108
 109        /// <summary>
 110        /// Gets or sets the <see cref="CancellationToken"/> associated with this <see cref="IBatchRequest"/>.
 111        /// </summary>
 112        /// <remarks>
 113        /// <para>
 114        /// Cancelling this token will cancel the currently ongoing request. This applies to the initial request as well
 115        /// as any subsequent requests created due to <see cref="RetryPolicy"/>. Cancelling this token also forbids all
 116        /// future retries of this <see cref="IBatchRequest"/>.
 117        /// </para>
 118        /// </remarks>
 119        public CancellationToken CancellationToken
 120        {
 483121            get { return this.cancellationToken; }
 122            set
 123            {
 246124                this.ThrowIfRequestExecutionHasStarted();
 245125                this.cancellationToken = value;
 245126            }
 127        }
 128
 129        /// <summary>
 130        /// Gets or sets the <see cref="ClientRequestIdProvider"/> used by this request to generate client request ids.
 131        /// </summary>
 132        public ClientRequestIdProvider ClientRequestIdProvider
 133        {
 201134            get { return this.clientRequestIdProvider; }
 135            set
 136            {
 2137                this.ThrowIfRequestExecutionHasStarted();
 1138                this.clientRequestIdProvider = value;
 1139            }
 140        }
 141
 142        /// <summary>
 143        /// Initializes a new instance of the <see cref="BatchRequestBase{TOptions,TResponse}"/> class.
 144        /// </summary>
 145        /// <param name="restClient">The REST client to use.</param>
 146        /// <param name="cancellationToken">The cancellationToken to use.</param>
 238147        protected BatchRequestBase(Protocol.BatchServiceClient restClient, CancellationToken cancellationToken)
 148        {
 149            //Construct a default set of options
 238150            this.Options = new TOptions();
 238151            this.OperationContext = new OperationContext();
 238152            this.RestClient = restClient;
 238153            this.Timeout = Constants.DefaultSingleRestRequestClientTimeout;
 238154            this.CancellationToken = cancellationToken;
 238155            this.CustomHeaders = new Dictionary<string, List<string>>();
 238156            this.hasRequestExecutionStarted = false;
 238157        }
 158
 159        /// <summary>
 160        /// Executes the request.
 161        /// </summary>
 162        /// <returns>An asynchronous operation of return type <typeparamref name="TResponse"/>.</returns>
 163        public async Task<TResponse> ExecuteRequestAsync()
 164        {
 171165            this.hasRequestExecutionStarted = true;
 166
 167            bool shouldRetry;
 168            do
 169            {
 195170                shouldRetry = false; //Start every request execution assuming there will be no retry
 195171                Task<TResponse> serviceRequestTask = null;
 172                Exception capturedException;
 173
 174                //Participate in cooperative cancellation
 195175                this.CancellationToken.ThrowIfCancellationRequested();
 176
 177                try
 178                {
 195179                    this.ApplyClientRequestIdBehaviorToParams();
 180
 195181                    serviceRequestTask = this.ExecuteRequestWithCancellationAsync(this.ServiceRequestFunc);
 182
 195183                    TResponse response = await serviceRequestTask.ConfigureAwait(continueOnCapturedContext: false);
 184
 185                    //TODO: It would be nice if we could add to OperationContext.RequestResults here
 139186                    return response;
 187                }
 56188                catch (Exception e)
 189                {
 190                    //If the caught exception was a BatchErrorException, we wrap it in the object model exception type
 56191                    BatchException batchException = null;
 56192                    if (e is BatchErrorException)
 193                    {
 0194                        batchException = new BatchException(e as BatchErrorException);
 195                    }
 196
 56197                    Exception wrappedException = batchException ?? e;
 198
 56199                    if (this.RetryPolicy != null &&
 56200                        //If cancellation is requested at this point, just skip calling the retry policy and throw
 56201                        //This is the most honest thing to do since we will not be issuing another request on the users 
 56202                        //(since the cancellation token has already been set) and thus calling their retry policy would 
 56203                        //be confusing.
 56204                        !this.CancellationToken.IsCancellationRequested)
 205                    {
 206                        RequestInformation requestInformation;
 207
 33208                        if (batchException != null)
 209                        {
 210                            //If there is a BatchException, extract the RequestInformation and capture it
 0211                            requestInformation = batchException.RequestInformation;
 212                        }
 213                        else
 214                        {
 33215                            requestInformation = new RequestInformation()
 33216                            {
 33217                                ClientRequestId = this.Options.ClientRequestId
 33218                            };
 219                        }
 220
 33221                        this.OperationContext.RequestResults.Add(new RequestResult(requestInformation, wrappedException)
 33222                        capturedException = wrappedException;
 223                    }
 224                    else
 225                    {
 23226                        if (batchException != null)
 227                        {
 0228                            throw batchException; //Just forward the wrapped exception if there was one
 229                        }
 230                        else
 231                        {
 23232                            throw;
 233                        }
 234                    }
 33235                }
 236
 33237                if (capturedException != null)
 238                {
 239                    //On an exception, invoke the retry policy
 33240                    RetryDecision retryDecision = await this.RetryPolicy.ShouldRetryAsync(capturedException, this.Operat
 33241                    shouldRetry = retryDecision.ShouldRetry;
 242
 33243                    if (!shouldRetry)
 244                    {
 245                        //Rethrow the exception and explicitly preserve the stack trace
 9246                        ExceptionDispatchInfo.Capture(capturedException).Throw();
 247                    }
 248                    else
 249                    {
 24250                        if (retryDecision.RetryDelay.HasValue)
 251                        {
 24252                            await Task.Delay(retryDecision.RetryDelay.Value).ConfigureAwait(continueOnCapturedContext: f
 253                        }
 254                        else
 255                        {
 256                            Debug.Assert(false, "RetryDecision.ShouldRetry = true but RetryDelay has no value");
 257                        }
 258                    }
 259                }
 260
 48261            } while (shouldRetry);
 262
 263            //Reaching here is a bug, by now the request should have either thrown or returned
 264            const string errorMessage = "Exited ExecuteRequestAsync without throwing or returning";
 265            Debug.Assert(false, errorMessage);
 0266            throw new InvalidOperationException(errorMessage);
 139267        }
 268
 269        /// <summary>
 270        /// Throws an exception if request execution has started.
 271        /// </summary>
 272        protected void ThrowIfRequestExecutionHasStarted()
 273        {
 1229274            if (this.hasRequestExecutionStarted)
 275            {
 7276                throw new InvalidOperationException(BatchErrorMessages.BatchRequestCannotBeModified);
 277            }
 1222278        }
 279
 280        /// <summary>
 281        /// Executes the specified function with a cancellation token which is a composite token for the <see cref="IBat
 282        /// and the <see cref="IBatchRequest.CancellationToken"/>.
 283        /// </summary>
 284        /// <param name="func">The function defining what work to be called in a cancellable way</param>
 285        /// <returns>A task with the async state of the func</returns>
 286        private async Task<TResponse> ExecuteRequestWithCancellationAsync(Func<CancellationToken, Task<TResponse>> func)
 287        {
 288            //CancellationToken(from outside) should cancel BOTH the retries and the currently ongoing REST request
 289            //CancellationToken(from timeout) should cancel just the currently ongoing REST request
 290
 195291            using (CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource(this.Timeout))
 292            {
 191293                CancellationToken timeoutToken = timeoutCancellationTokenSource.Token;
 191294                using (CancellationTokenSource linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeo
 295                {
 191296                    return await func(linkedTokenSource.Token).ConfigureAwait(continueOnCapturedContext: false);
 297                }
 298            }
 139299        }
 300
 301        private void ApplyClientRequestIdBehaviorToParams()
 302        {
 195303            if (this.ClientRequestIdProvider == null)
 304            {
 189305                Guid clientRequestId = Guid.NewGuid();
 189306                this.Options.ClientRequestId = clientRequestId;
 307            }
 308            else
 309            {
 6310                this.Options.ClientRequestId = this.ClientRequestIdProvider.GenerateClientRequestIdFunc(this);
 311            }
 6312        }
 313    }
 314
 315    /// <summary>
 316    /// Represents the information required to make a particular call with no request body to the Batch service REST API
 317    /// </summary>
 318    /// <typeparam name="TOptions">The type of the parameters passed outside the request body associated with the reques
 319    /// <typeparam name="TResponse">The response type expected from the request.</typeparam>
 320    public class BatchRequest<TOptions, TResponse> : BatchRequestBase<TOptions, TResponse>
 321        where TOptions : IOptions, new()
 322        where TResponse : IAzureOperationResponse
 323    {
 324        /// <summary>
 325        /// Initializes a new instance of the <see cref="BatchRequest{TOptions,TResponse}"/> class.
 326        /// </summary>
 327        /// <param name="restClient">The REST client to use.</param>
 328        /// <param name="cancellationToken">The cancellationToken to use.</param>
 329        public BatchRequest(BatchServiceClient restClient, CancellationToken cancellationToken)
 330            : base(restClient, cancellationToken)
 331        {
 332        }
 333    }
 334
 335    /// <summary>
 336    /// Represents the information required to make a particular call with a request body of type <typeparamref name="TB
 337    /// </summary>
 338    /// <typeparam name="TBody">The type of the body parameters associated with the request.</typeparam>
 339    /// <typeparam name="TOptions">The type of the parameters passed outside the request body associated with the reques
 340    /// <typeparam name="TResponse">The response type expected from the request.</typeparam>
 341    public class BatchRequest<TBody, TOptions, TResponse> : BatchRequestBase<TOptions, TResponse>
 342        where TOptions: IOptions, new()
 343        where TResponse : IAzureOperationResponse
 344    {
 345        private TBody parameters;
 346
 347        /// <summary>
 348        /// Gets or sets the parameters passed in the REST API request body.
 349        /// </summary>
 350        public TBody Parameters
 351        {
 352            get { return this.parameters; }
 353            set
 354            {
 355                this.ThrowIfRequestExecutionHasStarted();
 356                this.parameters = value;
 357            }
 358        }
 359
 360        /// <summary>
 361        /// Initializes a new instance of the <see cref="BatchRequest{TParameters,TOptions,TResponse}"/> class.
 362        /// </summary>
 363        /// <param name="restClient">The REST client to use.</param>
 364        /// <param name="parameters">The parameters to use.</param>
 365        /// <param name="cancellationToken">The cancellationToken to use.</param>
 366        public BatchRequest(Protocol.BatchServiceClient restClient, TBody parameters, CancellationToken cancellationToke
 367        {
 368            Parameters = parameters;
 369        }
 370    }
 371}