Skip to content

Commit 67c63d7

Browse files
committed
revert: 撤销更改
1 parent 5c1eff5 commit 67c63d7

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

test/UnitTest/Services/ThrottleTest.cs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,21 @@ public async Task ResetIntervalOnException_Ok()
8181
var dispatcher = factory.GetOrCreate("Error", new ThrottleOptions() { ResetIntervalOnException = true });
8282

8383
var count = 0;
84-
await Assert.ThrowsAnyAsync<InvalidOperationException>(() => dispatcher.ThrottleAsync(() =>
84+
await dispatcher.ThrottleAsync(() =>
8585
{
8686
count++;
87-
throw new InvalidOperationException();
88-
}));
87+
throw new Exception();
88+
});
89+
Assert.Equal(1, count);
8990

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

9293
// 发生错误后可以立即执行下一次任务,不限流
9394
dispatcher.Throttle(() =>
9495
{
9596
count++;
9697
});
98+
Assert.Equal(2, count);
9799
}
98100

99101
[Fact]
@@ -104,16 +106,21 @@ public async Task Cancel_Ok()
104106

105107
var cts = new CancellationTokenSource();
106108
cts.Cancel();
107-
Assert.ThrowsAny<OperationCanceledException>(() => dispatcher.Throttle(async () =>
109+
var ex = await Assert.ThrowsAsync<OperationCanceledException>(() =>
108110
{
109-
await Task.Delay(300);
110-
}, cts.Token));
111+
dispatcher.Throttle(() =>
112+
{
113+
114+
}, cts.Token);
115+
return Task.CompletedTask;
116+
});
117+
Assert.NotNull(ex);
111118

112119
cts = new CancellationTokenSource(100);
113-
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => dispatcher.ThrottleAsync(async () =>
120+
await dispatcher.ThrottleAsync(async () =>
114121
{
115122
await Task.Delay(300);
116-
}, cts.Token));
123+
}, cts.Token);
117124
}
118125

119126
[Fact]
@@ -125,38 +132,37 @@ public void Clear()
125132
factory.Clear("Clear");
126133
}
127134

128-
[Fact]
129-
public void LatTask_Ok()
130-
{
131-
var dispatch = new MockDispatcher(new ThrottleOptions());
132-
Assert.NotNull(dispatch.TestLastTask());
133-
}
134-
135135
[Fact]
136136
public void ShouldWait_Ok()
137137
{
138-
var dispatch = new MockDispatcher(new ThrottleOptions());
138+
var dispatch = new ThrottleDispatcher(new ThrottleOptions());
139139
var count = 0;
140140
dispatch.Throttle(() => count++);
141-
Assert.Equal(0, count);
141+
Assert.Equal(1, count);
142+
dispatch.Throttle(() => count++);
143+
Assert.Equal(1, count);
142144
}
143145

144-
class MockDispatcher(ThrottleOptions options) : ThrottleDispatcher(options)
146+
[Fact]
147+
public async Task MultipleThread_ThrottleAsync_Ok()
145148
{
146-
public Task TestLastTask()
149+
var count = 0;
150+
var dispatch = new ThrottleDispatcher(new ThrottleOptions()
147151
{
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()
152+
Interval = TimeSpan.FromMilliseconds(100),
153+
DelayAfterExecution = true
154+
});
155+
var tasks = Enumerable.Range(1, 2).Select(i => dispatch.ThrottleAsync(() =>
158156
{
159-
return count++ == 1;
160-
}
157+
count++;
158+
return Task.CompletedTask;
159+
})).ToList();
160+
tasks.Add(dispatch.ThrottleAsync(async () =>
161+
{
162+
await Task.Delay(120);
163+
count++;
164+
}));
165+
await Task.WhenAll(tasks);
166+
Assert.Equal(1, count);
161167
}
162168
}

0 commit comments

Comments
 (0)