From cddd1fa627a3927b96911e1dc8385de4c8ed1921 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Wed, 25 Feb 2026 17:10:29 +0800 Subject: [PATCH] =?UTF-8?q?test(extensions):=20=E6=9B=B4=E6=96=B0=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A5=E4=BD=BF=E7=94=A8=E7=9B=B4=E6=8E=A5=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除旧的Arrange步骤,直接在Act步骤中调用AsyncExtensions.WithTimeout方法 - 添加新的测试用例验证超时时内部任务能够被正确取消 - 修改现有测试用例适配新的方法调用方式 - 简化异常处理相关的断言逻辑 --- .../extensions/AsyncExtensionsTests.cs | 93 +++++++++++++++---- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs index c23843a..b673667 100644 --- a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs @@ -13,11 +13,10 @@ public class AsyncExtensionsTests [Test] public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout() { - // Arrange - var task = Task.FromResult(42); - // Act - var result = await task.WithTimeout(TimeSpan.FromSeconds(1)); + var result = await AsyncExtensions.WithTimeout( + _ => Task.FromResult(42), + TimeSpan.FromSeconds(1)); // Assert Assert.That(result, Is.EqualTo(42)); @@ -26,12 +25,15 @@ public class AsyncExtensionsTests [Test] public void WithTimeout_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout() { - // Arrange - var task = Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith(_ => 42); - // Act & Assert Assert.ThrowsAsync(async () => - await task.WithTimeout(TimeSpan.FromMilliseconds(100))); + await AsyncExtensions.WithTimeout( + async ct => + { + await Task.Delay(TimeSpan.FromSeconds(2), ct); + return 42; + }, + TimeSpan.FromMilliseconds(100))); } [Test] @@ -39,23 +41,57 @@ public class AsyncExtensionsTests { // Arrange using var cts = new CancellationTokenSource(); - var task = Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith(_ => 42); cts.Cancel(); // Act & Assert Assert.ThrowsAsync(async () => - await task.WithTimeout(TimeSpan.FromSeconds(1), cts.Token)); + await AsyncExtensions.WithTimeout( + async ct => + { + await Task.Delay(TimeSpan.FromSeconds(2), ct); + return 42; + }, + TimeSpan.FromSeconds(1), + cts.Token)); + } + + [Test] + public void WithTimeout_Should_Cancel_Inner_Task_When_Timeout_Elapses() + { + // Arrange + var innerTaskCanceled = false; + + // Act & Assert + Assert.ThrowsAsync(async () => + await AsyncExtensions.WithTimeout( + async ct => + { + try + { + await Task.Delay(TimeSpan.FromSeconds(5), ct); + return 0; + } + catch (OperationCanceledException) + { + innerTaskCanceled = true; + throw; + } + }, + TimeSpan.FromMilliseconds(100))); + + Assert.That(innerTaskCanceled, Is.True, "内部任务应在超时时收到取消信号"); } [Test] public async Task WithTimeout_NoResult_Should_Complete_When_Task_Completes_Before_Timeout() { // Arrange - var task = Task.CompletedTask; var stopwatch = Stopwatch.StartNew(); // Act - await task.WithTimeout(TimeSpan.FromSeconds(1)); + await AsyncExtensions.WithTimeout( + _ => Task.CompletedTask, + TimeSpan.FromSeconds(1)); stopwatch.Stop(); // Assert @@ -65,13 +101,38 @@ public class AsyncExtensionsTests [Test] public void WithTimeout_NoResult_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout() + { + // Act & Assert + Assert.ThrowsAsync(async () => + await AsyncExtensions.WithTimeout( + ct => Task.Delay(TimeSpan.FromSeconds(2), ct), + TimeSpan.FromMilliseconds(100))); + } + + [Test] + public void WithTimeout_NoResult_Should_Cancel_Inner_Task_When_Timeout_Elapses() { // Arrange - var task = Task.Delay(TimeSpan.FromSeconds(2)); + var innerTaskCanceled = false; // Act & Assert Assert.ThrowsAsync(async () => - await task.WithTimeout(TimeSpan.FromMilliseconds(100))); + await AsyncExtensions.WithTimeout( + async ct => + { + try + { + await Task.Delay(TimeSpan.FromSeconds(5), ct); + } + catch (OperationCanceledException) + { + innerTaskCanceled = true; + throw; + } + }, + TimeSpan.FromMilliseconds(100))); + + Assert.That(innerTaskCanceled, Is.True, "内部任务应在超时时收到取消信号"); } [Test] @@ -231,7 +292,7 @@ public class AsyncExtensionsTests var task = Task.FromException(new InvalidOperationException("Test error")); // Act - var result = await task.WithFallback(ex => -1); + var result = await task.WithFallback(_ => -1); // Assert Assert.That(result, Is.EqualTo(-1)); @@ -255,4 +316,4 @@ public class AsyncExtensionsTests // Assert Assert.That(capturedEx, Is.SameAs(expectedException)); } -} +} \ No newline at end of file