From 81ee7e41af170be119254e3c24a873e18897f841 Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Wed, 23 Feb 2022 18:03:02 +0100 Subject: [PATCH 1/3] Converted `AwaitAssert()` to sync over async and called `AwaitAssertAsync()` --- .../Akka.TestKit/TestKitBase_AwaitAssert.cs | 43 +++---------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs index 54d0afe09a1..b4993c779e6 100644 --- a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs +++ b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Akka.TestKit.Internal; +using Nito.AsyncEx.Synchronous; namespace Akka.TestKit { @@ -33,44 +34,12 @@ public abstract partial class TestKitBase /// The interval to wait between executing the assertion. public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null) { - var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100)); - if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue; - intervalValue.EnsureIsPositiveFinite("interval"); - var max = RemainingOrDilated(duration); - var stop = Now + max; - var t = max.Min(intervalValue); - while(true) - { - try - { - assertion(); - return; - } - catch(Exception) - { - if(Now + t >= stop) - throw; - } - Thread.Sleep(t); - t = (stop - Now).Min(intervalValue); - } + var task = AwaitAssertAsync(assertion, duration, interval).AsTask(); + task.WaitAndUnwrapException(); } - /// - /// Await until the given assertion does not throw an exception or the timeout - /// expires, whichever comes first. If the timeout expires the last exception - /// is thrown. - /// The action is called, and if it throws an exception the thread sleeps - /// the specified interval before retrying. - /// If no timeout is given, take it from the innermost enclosing `within` - /// block. - /// Note that the timeout is scaled using , - /// which uses the configuration entry "akka.test.timefactor". - /// - /// The action. - /// The timeout. - /// The interval to wait between executing the assertion. - public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null) + /// + public async ValueTask AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null) { var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100)); if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue; @@ -109,7 +78,7 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti /// The action. /// The timeout. /// The interval to wait between executing the assertion. - public async Task AwaitAssertAsync(Func assertion, TimeSpan? duration=null, TimeSpan? interval=null) + public async ValueTask AwaitAssertAsync(Func assertion, TimeSpan? duration=null, TimeSpan? interval=null) { var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100)); if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue; From 11eeb7d4e136c99f5fec280b310a2dee8087ac0b Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Thu, 24 Feb 2022 13:59:25 +0100 Subject: [PATCH 2/3] Added CancellationToken support --- .../Akka.TestKit/TestKitBase_AwaitAssert.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs index b4993c779e6..437be47d21c 100644 --- a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs +++ b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs @@ -32,14 +32,15 @@ public abstract partial class TestKitBase /// The action. /// The timeout. /// The interval to wait between executing the assertion. - public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null) + /// + public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default) { - var task = AwaitAssertAsync(assertion, duration, interval).AsTask(); + var task = AwaitAssertAsync(assertion, duration, interval, cancellationToken); task.WaitAndUnwrapException(); } - /// - public async ValueTask AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null) + /// + public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default) { var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100)); if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue; @@ -47,7 +48,7 @@ public async ValueTask AwaitAssertAsync(Action assertion, TimeSpan? duration=nul var max = RemainingOrDilated(duration); var stop = Now + max; var t = max.Min(intervalValue); - while(true) + while(!cancellationToken.IsCancellationRequested) { try { @@ -59,11 +60,11 @@ public async ValueTask AwaitAssertAsync(Action assertion, TimeSpan? duration=nul if(Now + t >= stop) throw; } - await Task.Delay(t); + await Task.Delay(t, cancellationToken); t = (stop - Now).Min(intervalValue); } } - + /// /// Await until the given assertion does not throw an exception or the timeout /// expires, whichever comes first. If the timeout expires the last exception @@ -78,7 +79,8 @@ public async ValueTask AwaitAssertAsync(Action assertion, TimeSpan? duration=nul /// The action. /// The timeout. /// The interval to wait between executing the assertion. - public async ValueTask AwaitAssertAsync(Func assertion, TimeSpan? duration=null, TimeSpan? interval=null) + /// + public async Task AwaitAssertAsync(Func assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default) { var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100)); if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue; @@ -86,7 +88,7 @@ public async ValueTask AwaitAssertAsync(Func assertion, TimeSpan? duration var max = RemainingOrDilated(duration); var stop = Now + max; var t = max.Min(intervalValue); - while(true) + while(!cancellationToken.IsCancellationRequested) { try { @@ -98,7 +100,7 @@ public async ValueTask AwaitAssertAsync(Func assertion, TimeSpan? duration if(Now + t >= stop) throw; } - await Task.Delay(t); + await Task.Delay(t, cancellationToken); t = (stop - Now).Min(intervalValue); } } From 15de41fbcc4f89d3cab8cb5b7f934681ba202b5f Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Thu, 24 Feb 2022 16:46:39 +0100 Subject: [PATCH 3/3] Prefer `ThrowIfCancellationRequested()` over `IsCancellationRequested` --- src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs index 437be47d21c..ccf2907add1 100644 --- a/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs +++ b/src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs @@ -48,8 +48,9 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti var max = RemainingOrDilated(duration); var stop = Now + max; var t = max.Min(intervalValue); - while(!cancellationToken.IsCancellationRequested) + while(true) { + cancellationToken.ThrowIfCancellationRequested(); try { assertion(); @@ -88,8 +89,9 @@ public async Task AwaitAssertAsync(Func assertion, TimeSpan? duration=null var max = RemainingOrDilated(duration); var stop = Now + max; var t = max.Min(intervalValue); - while(!cancellationToken.IsCancellationRequested) + while(true) { + cancellationToken.ThrowIfCancellationRequested(); try { await assertion();