Skip to content

Commit 68e5780

Browse files
committed
test: 更新单元测试
1 parent 469cec9 commit 68e5780

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

test/UnitTest/Services/ThrottleTest.cs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using System.Runtime.InteropServices;
7+
68
namespace UnitTest.Services;
79

810
public class ThrottleTest : BootstrapBlazorTestBase
@@ -81,19 +83,21 @@ public async Task ResetIntervalOnException_Ok()
8183
var dispatcher = factory.GetOrCreate("Error", new ThrottleOptions() { ResetIntervalOnException = true });
8284

8385
var count = 0;
84-
await Assert.ThrowsAnyAsync<InvalidOperationException>(() => dispatcher.ThrottleAsync(() =>
86+
await dispatcher.ThrottleAsync(() =>
8587
{
8688
count++;
87-
throw new InvalidOperationException();
88-
}));
89+
throw new Exception();
90+
});
91+
Assert.Equal(1, count);
8992

90-
Assert.ThrowsAny<InvalidOperationException>(() => dispatcher.Throttle(() => throw new InvalidOperationException()));
93+
dispatcher.Throttle(() => throw new InvalidOperationException());
9194

9295
// 发生错误后可以立即执行下一次任务,不限流
9396
dispatcher.Throttle(() =>
9497
{
9598
count++;
9699
});
100+
Assert.Equal(2, count);
97101
}
98102

99103
[Fact]
@@ -104,16 +108,21 @@ public async Task Cancel_Ok()
104108

105109
var cts = new CancellationTokenSource();
106110
cts.Cancel();
107-
Assert.ThrowsAny<OperationCanceledException>(() => dispatcher.Throttle(async () =>
111+
var ex = await Assert.ThrowsAsync<OperationCanceledException>(() =>
108112
{
109-
await Task.Delay(300);
110-
}, cts.Token));
113+
dispatcher.Throttle(() =>
114+
{
115+
116+
}, cts.Token);
117+
return Task.CompletedTask;
118+
});
119+
Assert.NotNull(ex);
111120

112121
cts = new CancellationTokenSource(100);
113-
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => dispatcher.ThrottleAsync(async () =>
122+
await dispatcher.ThrottleAsync(async () =>
114123
{
115124
await Task.Delay(300);
116-
}, cts.Token));
125+
}, cts.Token);
117126
}
118127

119128
[Fact]
@@ -125,38 +134,37 @@ public void Clear()
125134
factory.Clear("Clear");
126135
}
127136

128-
[Fact]
129-
public void LatTask_Ok()
130-
{
131-
var dispatch = new MockDispatcher(new ThrottleOptions());
132-
Assert.NotNull(dispatch.TestLastTask());
133-
}
134-
135137
[Fact]
136138
public void ShouldWait_Ok()
137139
{
138-
var dispatch = new MockDispatcher(new ThrottleOptions());
140+
var dispatch = new ThrottleDispatcher(new ThrottleOptions());
139141
var count = 0;
140142
dispatch.Throttle(() => count++);
141-
Assert.Equal(0, count);
143+
Assert.Equal(1, count);
144+
dispatch.Throttle(() => count++);
145+
Assert.Equal(1, count);
142146
}
143147

144-
class MockDispatcher(ThrottleOptions options) : ThrottleDispatcher(options)
148+
[Fact]
149+
public async Task MultipleThread_ThrottleAsync_Ok()
145150
{
146-
public Task TestLastTask()
151+
var count = 0;
152+
var dispatch = new ThrottleDispatcher(new ThrottleOptions()
147153
{
148-
return LastTask;
149-
}
150-
151-
private int count = 0;
152-
153-
/// <summary>
154-
/// <inheritdoc/>
155-
/// </summary>
156-
/// <returns></returns>
157-
protected override bool ShouldWait()
154+
Interval = TimeSpan.FromMilliseconds(100),
155+
DelayAfterExecution = true
156+
});
157+
var tasks = Enumerable.Range(1, 2).Select(i => dispatch.ThrottleAsync(() =>
158158
{
159-
return count++ == 1;
160-
}
159+
count++;
160+
return Task.CompletedTask;
161+
})).ToList();
162+
tasks.Add(dispatch.ThrottleAsync(async () =>
163+
{
164+
await Task.Delay(120);
165+
count++;
166+
}));
167+
await Task.WhenAll(tasks);
168+
Assert.Equal(1, count);
161169
}
162170
}

0 commit comments

Comments
 (0)