| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace Azure.Core.TestFramework |
| | | 11 | | { |
| | | 12 | | public static class TaskExtensions |
| | | 13 | | { |
| | 1358 | 14 | | 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 | | { |
| | 1356 | 20 | | 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 | | { |
| | 0 | 27 | | 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 |
| | 1400 | 37 | | if (task.IsCompleted || Debugger.IsAttached) |
| | | 38 | | { |
| | 74 | 39 | | return await task; |
| | | 40 | | } |
| | | 41 | | |
| | 1326 | 42 | | var cts = new CancellationTokenSource(); |
| | 1326 | 43 | | if (task == await Task.WhenAny(task, Task.Delay(timeout, cts.Token))) |
| | | 44 | | { |
| | 1316 | 45 | | cts.Cancel(); |
| | 1316 | 46 | | return await task; |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 0 | 50 | | throw new TimeoutException(CreateMessage(timeout, filePath, lineNumber)); |
| | | 51 | | } |
| | 1302 | 52 | | } |
| | | 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 |
| | 0 | 60 | | if (task.IsCompleted || Debugger.IsAttached) |
| | | 61 | | { |
| | 0 | 62 | | await task; |
| | 0 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | var cts = new CancellationTokenSource(); |
| | 0 | 67 | | if (task == await Task.WhenAny(task, Task.Delay(timeout, cts.Token))) |
| | | 68 | | { |
| | 0 | 69 | | cts.Cancel(); |
| | 0 | 70 | | await task; |
| | | 71 | | } |
| | | 72 | | else |
| | | 73 | | { |
| | 0 | 74 | | throw new TimeoutException(CreateMessage(timeout, filePath, lineNumber)); |
| | | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static string CreateMessage(TimeSpan timeout, string filePath, int lineNumber) |
| | 0 | 79 | | => string.IsNullOrEmpty(filePath) |
| | 0 | 80 | | ? $"The operation timed out after reaching the limit of {timeout.TotalMilliseconds}ms." |
| | 0 | 81 | | : $"The operation at {filePath}:{lineNumber} timed out after reaching the limit of {timeout.TotalMillise |
| | | 82 | | } |
| | | 83 | | } |