Skip to content

Commit 56f936e

Browse files
authored
test(IThrottleDispatcherFactory): improve code coverage (#6411)
* test: 更新单元测试 * test: 更新单元测试
1 parent cd6c8fd commit 56f936e

File tree

2 files changed

+4
-35
lines changed

2 files changed

+4
-35
lines changed

src/BootstrapBlazor/Services/ThrottleDispatcher.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ThrottleDispatcher(ThrottleOptions options)
2323
/// </summary>
2424
/// <param name="function">异步回调方法</param>
2525
/// <param name="cancellationToken">取消令牌</param>
26-
public Task ThrottleAsync(Func<Task> function, CancellationToken cancellationToken = default) => InternalThrottleAsync(() => Task.Run(function), cancellationToken);
26+
public Task ThrottleAsync(Func<Task> function, CancellationToken cancellationToken = default) => InternalThrottleAsync(function, cancellationToken);
2727

2828
/// <summary>
2929
/// 同步限流方法
@@ -32,19 +32,12 @@ public class ThrottleDispatcher(ThrottleOptions options)
3232
/// <param name="token">取消令牌</param>
3333
public void Throttle(Action action, CancellationToken token = default)
3434
{
35-
var task = InternalThrottleAsync(() => Task.Run(() =>
35+
var task = InternalThrottleAsync(() =>
3636
{
3737
action();
3838
return Task.CompletedTask;
39-
}, CancellationToken.None), token);
40-
try
41-
{
42-
task.Wait(token);
43-
}
44-
catch (Exception)
45-
{
46-
throw;
47-
}
39+
}, token);
40+
task.Wait(token);
4841
return;
4942
}
5043

test/UnitTest/Services/ThrottleTest.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,6 @@ await dispatcher.ThrottleAsync(() =>
9898
Assert.Equal(2, count);
9999
}
100100

101-
[Fact]
102-
public async Task Cancel_Ok()
103-
{
104-
var factory = Context.Services.GetRequiredService<IThrottleDispatcherFactory>();
105-
var dispatcher = factory.GetOrCreate("Cancel");
106-
107-
var cts = new CancellationTokenSource();
108-
var ex = await Assert.ThrowsAsync<OperationCanceledException>(() =>
109-
{
110-
dispatcher.Throttle(() =>
111-
{
112-
cts.Cancel();
113-
}, cts.Token);
114-
return Task.CompletedTask;
115-
});
116-
Assert.NotNull(ex);
117-
118-
cts = new CancellationTokenSource(100);
119-
await dispatcher.ThrottleAsync(async () =>
120-
{
121-
await Task.Delay(300);
122-
}, cts.Token);
123-
}
124-
125101
[Fact]
126102
public void Clear()
127103
{

0 commit comments

Comments
 (0)