yavsc/test/yavscTests/TestHelpers.cs

37 lines
760 B
C#
Raw Normal View History

2018-07-20 02:48:21 +02:00
using System;
using System.Threading.Tasks;
2021-06-06 20:32:39 +01:00
namespace yavscTests {
2018-07-20 02:48:21 +02:00
public static class AssertAsync {
2025-07-16 00:47:50 +01:00
/// <summary>
/// Completes In
/// </summary>
/// <param name="timeoutFromSecond"></param>
/// <param name="action"></param>
public static void CompletesIn(int timeoutFromSecond, Action action)
2018-07-20 02:48:21 +02:00
{
var task = Task.Run(action);
2025-07-16 00:47:50 +01:00
var completedInTime = Task.WaitAll(new[] { task }, TimeSpan.FromSeconds(timeoutFromSecond));
2018-07-20 02:48:21 +02:00
if (task.Exception != null)
{
if (task.Exception.InnerExceptions.Count == 1)
{
throw task.Exception.InnerExceptions[0];
}
throw task.Exception;
}
if (!completedInTime)
{
2025-07-16 00:47:50 +01:00
throw new TimeoutException($"Task did not complete in {timeoutFromSecond} seconds.");
2018-07-20 02:48:21 +02:00
}
}
}
}