From dd07ef5686acd8198f3f102a3eae01ea7e5aa3af Mon Sep 17 00:00:00 2001 From: ZjzMisaka Date: Thu, 30 Apr 2026 05:32:42 +0800 Subject: [PATCH] test: add tests for ConfigureAwaitFalse --- UnitTest/AsyncTest.cs | 137 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/UnitTest/AsyncTest.cs b/UnitTest/AsyncTest.cs index 43c7ca4..995be71 100644 --- a/UnitTest/AsyncTest.cs +++ b/UnitTest/AsyncTest.cs @@ -1924,6 +1924,128 @@ public async void TestStopByIDInnerUseCancellationToken() powerPool.Wait(); } + [Fact(Timeout = 5 * 60 * 1000)] + public async void TestConfigureAwaitFalseCallPauseIfRequestedBefore3Rd() + { + PowerPool powerPool = new PowerPool(); + Exception ex = null; + powerPool.QueueWorkItem(async () => + { + try + { + powerPool.PauseIfRequested(); + } + catch (Exception e) + { + ex = e; + } + + await DoSomething3Rd(() => { }); + + await Task.Delay(10); + await Task.Delay(10); + await Task.Delay(10); + + }); + + powerPool.Wait(); + + Assert.Null(ex); + } + + [Fact(Timeout = 5 * 60 * 1000)] + public async void TestConfigureAwaitFalseCallPauseIfRequestedAfter3Rd() + { + PowerPool powerPool = new PowerPool(); + Exception ex = null; + powerPool.QueueWorkItem(async () => + { + await Task.Delay(10); + + await DoSomething3Rd(() => { }); + + await Task.Delay(10); + await Task.Delay(10); + + try + { + powerPool.PauseIfRequested(); + } + catch (Exception e) + { + ex = e; + } + + await Task.Delay(10); + }); + + powerPool.Wait(); + + Assert.Null(ex); + } + + [Fact(Timeout = 5 * 60 * 1000)] + public async void TestConfigureAwaitFalseCallPauseIfRequestedIn3Rd() + { + PowerPool powerPool = new PowerPool(); + Exception ex = null; + powerPool.QueueWorkItem(async () => + { + await Task.Delay(10); + + await DoSomething3Rd(() => + { + try + { + powerPool.PauseIfRequested(); + } + catch (Exception e) + { + ex = e; + } + }); + + await Task.Delay(10); + await Task.Delay(10); + }); + + powerPool.Wait(); + + Assert.Null(ex); + } + + [Fact(Timeout = 5 * 60 * 1000)] + public async void TestConfigureAwaitInUserWork() + { + PowerPool powerPool = new PowerPool(); + Exception ex = null; + powerPool.QueueWorkItem(async () => + { + await Task.Delay(10); + + await Task.Delay(10).ConfigureAwait(false); + + await Task.Delay(10); + await Task.Delay(10); + + try + { + powerPool.PauseIfRequested(); + } + catch (Exception e) + { + ex = e; + } + + await Task.Delay(10); + }); + + powerPool.Wait(); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + private async Task OuterAsync() { string result = await InnerAsync(); @@ -1954,5 +2076,20 @@ private long GetNowSs() { return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); } + + private async Task DoSomething3Rd(Action action) + { + await DoSomething().ConfigureAwait(false); + await DoSomething().ConfigureAwait(false); + await DoSomething().ConfigureAwait(false); + action(); + await DoSomething().ConfigureAwait(false); + await DoSomething().ConfigureAwait(false); + await DoSomething().ConfigureAwait(false); + } + + private async Task DoSomething() + { + } } }