< Summary

Class:Azure.Core.TestFramework.TaskExtensions
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\TaskExtensions.cs
Covered lines:9
Uncovered lines:14
Coverable lines:23
Total lines:83
Line coverage:39.1% (9 of 23)
Covered branches:5
Total branches:14
Branch coverage:35.7% (5 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_DefaultTimeout()-100%100%
TimeoutAfterDefault(...)-100%100%
TimeoutAfterDefault(...)-0%100%
TimeoutAfter()-87.5%83.33%
TimeoutAfter()-0%0%
CreateMessage(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\TaskExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Runtime.CompilerServices;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace Azure.Core.TestFramework
 11{
 12    public static class TaskExtensions
 13    {
 135814        public static TimeSpan DefaultTimeout { get; } = TimeSpan.FromSeconds(10);
 15
 16        public static Task<T> TimeoutAfterDefault<T>(this Task<T> task,
 17            [CallerFilePath] string filePath = null,
 18            [CallerLineNumber] int lineNumber = default)
 19        {
 135620            return task.TimeoutAfter(DefaultTimeout, filePath, lineNumber);
 21        }
 22
 23        public static Task TimeoutAfterDefault(this Task task,
 24            [CallerFilePath] string filePath = null,
 25            [CallerLineNumber] int lineNumber = default)
 26        {
 027            return task.TimeoutAfter(DefaultTimeout, filePath, lineNumber);
 28        }
 29
 30
 31        public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout,
 32            [CallerFilePath] string filePath = null,
 33            [CallerLineNumber] int lineNumber = default)
 34        {
 35            // Don't create a timer if the task is already completed
 36            // or the debugger is attached
 140037            if (task.IsCompleted || Debugger.IsAttached)
 38            {
 7439                return await task;
 40            }
 41
 132642            var cts = new CancellationTokenSource();
 132643            if (task == await Task.WhenAny(task, Task.Delay(timeout, cts.Token)))
 44            {
 131645                cts.Cancel();
 131646                return await task;
 47            }
 48            else
 49            {
 050                throw new TimeoutException(CreateMessage(timeout, filePath, lineNumber));
 51            }
 130252        }
 53
 54        public static async Task TimeoutAfter(this Task task, TimeSpan timeout,
 55            [CallerFilePath] string filePath = null,
 56            [CallerLineNumber] int lineNumber = default)
 57        {
 58            // Don't create a timer if the task is already completed
 59            // or the debugger is attached
 060            if (task.IsCompleted || Debugger.IsAttached)
 61            {
 062                await task;
 063                return;
 64            }
 65
 066            var cts = new CancellationTokenSource();
 067            if (task == await Task.WhenAny(task, Task.Delay(timeout, cts.Token)))
 68            {
 069                cts.Cancel();
 070                await task;
 71            }
 72            else
 73            {
 074                throw new TimeoutException(CreateMessage(timeout, filePath, lineNumber));
 75            }
 076        }
 77
 78        private static string CreateMessage(TimeSpan timeout, string filePath, int lineNumber)
 079            => string.IsNullOrEmpty(filePath)
 080                ? $"The operation timed out after reaching the limit of {timeout.TotalMilliseconds}ms."
 081                : $"The operation at {filePath}:{lineNumber} timed out after reaching the limit of {timeout.TotalMillise
 82    }
 83}