Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using System.Threading;
using System.Threading.Tasks;

namespace Akka.Tests.Util
namespace Akka.TestKit.Extensions
{
public static class TaskHelpers
public static class TaskExtensions
{
public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan timeout)
public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan timeout, CancellationToken cancellationToken = default)
{
using (var cts = new CancellationTokenSource())
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
{
try
{
Expand All @@ -19,7 +19,10 @@ public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan t
if(returnedTask == parentTask && returnedTask.Exception != null)
{
var flattened = returnedTask.Exception.Flatten();
ExceptionDispatchInfo.Capture(flattened.InnerException).Throw();
if(flattened.InnerExceptions.Count == 1)
ExceptionDispatchInfo.Capture(flattened.InnerExceptions[0]).Throw();
else
ExceptionDispatchInfo.Capture(returnedTask.Exception).Throw();
}

return parentTask.IsCompleted;
Expand All @@ -31,9 +34,9 @@ public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan t
}
}

public static async Task<T> WithTimeout<T>(this Task<T> parentTask, TimeSpan timeout)
public static async Task<T> WithTimeout<T>(this Task<T> parentTask, TimeSpan timeout, CancellationToken cancellationToken = default)
{
using (var cts = new CancellationTokenSource())
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
{
try
{
Expand All @@ -46,7 +49,10 @@ public static async Task<T> WithTimeout<T>(this Task<T> parentTask, TimeSpan tim
if(returnedTask == parentTask && returnedTask.Exception != null)
{
var flattened = returnedTask.Exception.Flatten();
ExceptionDispatchInfo.Capture(flattened.InnerException).Throw();
if(flattened.InnerExceptions.Count == 1)
ExceptionDispatchInfo.Capture(flattened.InnerExceptions[0]).Throw();
else
ExceptionDispatchInfo.Capture(returnedTask.Exception).Throw();
}

return parentTask.Result;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/ActorSystemSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Akka.Configuration;
using Akka.Dispatch;
using Akka.Event;
using Akka.TestKit.Extensions;
using FluentAssertions.Execution;
using Akka.Tests.Util;

Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/CoordinatedShutdownSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Linq;
using System.Threading.Tasks;
using Akka.Configuration;
using Akka.TestKit.Extensions;
using FluentAssertions;
using Xunit;
using static Akka.Actor.CoordinatedShutdown;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/DeathWatchSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Akka.Dispatch.SysMsg;
using Akka.Event;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Tests.TestUtils;
using Akka.Tests.Util;
using Akka.Util.Internal;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/InboxSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Actor.Internal;
using Akka.Event;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Tests.Util;
using Xunit;

Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/LocalActorRefProviderSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Xunit;
using Akka.TestKit.TestActors;
using Akka.Tests.Util;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/PatternSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Akka.Actor;
using Akka.Event;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Tests.Util;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Dispatch;
using Akka.Event;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Tests.Util;
using Akka.Util.Internal;
using FluentAssertions;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Actor/Stash/ActorWithStashSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.TestKit.TestActors;
using Akka.Tests.TestUtils;
using Akka.Tests.Util;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Pattern/CircuitBreakerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading.Tasks;
using Akka.Pattern;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Tests.Util;
using Xunit;

Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Util/IndexSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading.Tasks;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Util;
using Xunit;
using FluentAssertions;
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka.Tests/Util/Internal/InterlockedSpinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Akka.TestKit;
using Akka.TestKit.Extensions;
using Akka.Util.Internal;
using FluentAssertions.Extensions;
using Xunit;
Expand Down