mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
test(extensions): 更新异步扩展方法测试以使用直接调用方式
- 移除旧的Arrange步骤,直接在Act步骤中调用AsyncExtensions.WithTimeout方法 - 添加新的测试用例验证超时时内部任务能够被正确取消 - 修改现有测试用例适配新的方法调用方式 - 简化异常处理相关的断言逻辑
This commit is contained in:
parent
afbff7127d
commit
cddd1fa627
@ -13,11 +13,10 @@ public class AsyncExtensionsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout()
|
public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout()
|
||||||
{
|
{
|
||||||
// Arrange
|
|
||||||
var task = Task.FromResult(42);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await task.WithTimeout(TimeSpan.FromSeconds(1));
|
var result = await AsyncExtensions.WithTimeout(
|
||||||
|
_ => Task.FromResult(42),
|
||||||
|
TimeSpan.FromSeconds(1));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(42));
|
Assert.That(result, Is.EqualTo(42));
|
||||||
@ -26,12 +25,15 @@ public class AsyncExtensionsTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void WithTimeout_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout()
|
public void WithTimeout_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout()
|
||||||
{
|
{
|
||||||
// Arrange
|
|
||||||
var task = Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith(_ => 42);
|
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(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]
|
[Test]
|
||||||
@ -39,23 +41,57 @@ public class AsyncExtensionsTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
using var cts = new CancellationTokenSource();
|
using var cts = new CancellationTokenSource();
|
||||||
var task = Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith(_ => 42);
|
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<OperationCanceledException>(async () =>
|
Assert.ThrowsAsync<OperationCanceledException>(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<TimeoutException>(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]
|
[Test]
|
||||||
public async Task WithTimeout_NoResult_Should_Complete_When_Task_Completes_Before_Timeout()
|
public async Task WithTimeout_NoResult_Should_Complete_When_Task_Completes_Before_Timeout()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var task = Task.CompletedTask;
|
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await task.WithTimeout(TimeSpan.FromSeconds(1));
|
await AsyncExtensions.WithTimeout(
|
||||||
|
_ => Task.CompletedTask,
|
||||||
|
TimeSpan.FromSeconds(1));
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@ -65,13 +101,38 @@ public class AsyncExtensionsTests
|
|||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void WithTimeout_NoResult_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout()
|
public void WithTimeout_NoResult_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.ThrowsAsync<TimeoutException>(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
|
// Arrange
|
||||||
var task = Task.Delay(TimeSpan.FromSeconds(2));
|
var innerTaskCanceled = false;
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.ThrowsAsync<TimeoutException>(async () =>
|
Assert.ThrowsAsync<TimeoutException>(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]
|
[Test]
|
||||||
@ -231,7 +292,7 @@ public class AsyncExtensionsTests
|
|||||||
var task = Task.FromException<int>(new InvalidOperationException("Test error"));
|
var task = Task.FromException<int>(new InvalidOperationException("Test error"));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await task.WithFallback(ex => -1);
|
var result = await task.WithFallback(_ => -1);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.EqualTo(-1));
|
Assert.That(result, Is.EqualTo(-1));
|
||||||
@ -255,4 +316,4 @@ public class AsyncExtensionsTests
|
|||||||
// Assert
|
// Assert
|
||||||
Assert.That(capturedEx, Is.SameAs(expectedException));
|
Assert.That(capturedEx, Is.SameAs(expectedException));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user