-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
348 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/Kook.Net.Tests.Integration.Socket/Extensions/TaskExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kook; | ||
|
||
/// <summary> | ||
/// Adds extension methods to the <see cref="Task"/> class. | ||
/// </summary> | ||
public static class TaskExtension | ||
{ | ||
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(15); | ||
|
||
/// <summary> | ||
/// Waits for the task to complete within the specified timeout. | ||
/// </summary> | ||
/// <param name="task"> The task to wait for. </param> | ||
/// <returns> The task. </returns> | ||
/// <exception cref="TimeoutException"> The task did not complete within the specified timeout. </exception> | ||
public static async Task WithTimeout(this Task task) => await task.WithTimeout(DefaultTimeout); | ||
|
||
/// <summary> | ||
/// Waits for the task to complete within the specified timeout. | ||
/// </summary> | ||
/// <param name="task"> The task to wait for. </param> | ||
/// <param name="timeout"> A <see cref="TimeSpan"/> representing the timeout duration. </param> | ||
/// <returns> The task. </returns> | ||
/// <exception cref="TimeoutException"> The task did not complete within the specified timeout. </exception> | ||
public static async Task WithTimeout(this Task task, TimeSpan timeout) | ||
{ | ||
Task delayTask = Task.Delay(timeout); | ||
Task completedTask = await Task.WhenAny(task, delayTask); | ||
if (completedTask == delayTask) | ||
throw new TimeoutException(); | ||
await task; | ||
} | ||
|
||
/// <summary> | ||
/// Waits for the task to complete within the specified timeout. | ||
/// </summary> | ||
/// <param name="task"> The task to wait for. </param> | ||
/// <typeparam name="T"> The type of the task result. </typeparam> | ||
/// <returns> The result of the task. </returns> | ||
/// <exception cref="TimeoutException"> The task did not complete within the specified timeout. </exception> | ||
public static Task<T> WithTimeout<T>(this Task<T> task) => WithTimeout(task, DefaultTimeout); | ||
|
||
/// <summary> | ||
/// Waits for the task to complete within the specified timeout. | ||
/// </summary> | ||
/// <param name="task"> The task to wait for. </param> | ||
/// <param name="timeout"> A <see cref="TimeSpan"/> representing the timeout duration. </param> | ||
/// <typeparam name="T"> The type of the task result. </typeparam> | ||
/// <returns> The result of the task. </returns> | ||
/// <exception cref="TimeoutException"> The task did not complete within the specified timeout. </exception> | ||
public static async Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout) | ||
{ | ||
Task delayTask = Task.Delay(timeout); | ||
Task completedTask = await Task.WhenAny(task, delayTask); | ||
if (completedTask == delayTask) | ||
throw new TimeoutException(); | ||
return await task; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.