Anapher/Strive

View on GitHub
src/Services/ConferenceManagement/Strive.IntegrationTests/Extensions/TaskExtensions.cs

Summary

Maintainability
A
0 mins
Test Coverage
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Strive.IntegrationTests.Extensions
{
    //https://stackoverflow.com/a/22078975
    public static class TaskExtensions
    {
        public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
        {
            using (var timeoutCancellationTokenSource = new CancellationTokenSource())
            {
                var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
                if (completedTask == task)
                {
                    timeoutCancellationTokenSource.Cancel();
                    return await task; // Very important in order to propagate exceptions
                }

                throw new TimeoutException("The operation has timed out.");
            }
        }

        public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
        {
            using (var timeoutCancellationTokenSource = new CancellationTokenSource())
            {
                var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
                if (completedTask == task)
                {
                    timeoutCancellationTokenSource.Cancel();
                    await task; // Very important in order to propagate exceptions
                    return;
                }

                throw new TimeoutException("The operation has timed out.");
            }
        }
    }
}