Skip to content

Commit 383a26f

Browse files
committed
Change: Use shared Lock instance in ThrottledDebouncer tests
1 parent 81fc02a commit 383a26f

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

tests/Tests.CodeOfChaos.Extensions/Debouncers/Throttled/ThrottledDebouncerGenericTests.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ namespace Tests.CodeOfChaos.Extensions.Debouncers.Throttled;
99
// ---------------------------------------------------------------------------------------------------------------------
1010
// ReSharper disable ConvertToLocalFunction
1111
public class ThrottledDebouncerGenericTests {
12+
private Lock Lock { get; } = new();
1213

1314
[Test]
1415
public async Task ThrottledDebouncer_ShouldPassValueToCallback() {
1516
// Arrange
16-
var @lock = new Lock();
1717
string? receivedValue = null;
1818
Action<string> callback = value => {
19-
lock (@lock) {
19+
lock (Lock) {
2020
receivedValue = value;
2121
}
2222
};
@@ -33,10 +33,9 @@ public async Task ThrottledDebouncer_ShouldPassValueToCallback() {
3333
[Test]
3434
public async Task ThrottledDebouncer_MultipleValues_ShouldUseLastValue() {
3535
// Arrange
36-
var @lock = new Lock();
3736
string? receivedValue = null;
3837
Func<string, Task> callback = value => {
39-
lock (@lock) {
38+
lock (Lock) {
4039
receivedValue = value;
4140
}
4241
return Task.CompletedTask;
@@ -58,7 +57,9 @@ public async Task ThrottledDebouncer_ThrottleBehavior_ShouldExecuteImmediatelyAf
5857
// Arrange
5958
var receivedValues = new List<(string Value, DateTime Time)>();
6059
Func<string, Task> callback = value => {
61-
receivedValues.Add((value, DateTime.UtcNow));
60+
lock (Lock) {
61+
receivedValues.Add((value, DateTime.UtcNow));
62+
}
6263
return Task.CompletedTask;
6364
};
6465

@@ -88,11 +89,10 @@ public async Task ThrottledDebouncer_ThrottleBehavior_ShouldExecuteImmediatelyAf
8889
[Test]
8990
public async Task ThrottledDebouncer_ContinuousRequests_ShouldRespectThrottleInterval() {
9091
// Arrange
91-
var @lock = new Lock();
9292
var receivedValues = new List<string>();
9393
int executionCount = 0;
9494
Action<string> callback = value => {
95-
lock (@lock) {
95+
lock (Lock) {
9696
receivedValues.Add(value);
9797
}
9898
Interlocked.Increment(ref executionCount);
@@ -120,11 +120,10 @@ public async Task ThrottledDebouncer_ContinuousRequests_ShouldRespectThrottleInt
120120
[Test]
121121
public async Task ThrottledDebouncer_ContinuousRequests_ShouldRespectThrottleInterval_v2() {
122122
// Arrange
123-
var @lock = new Lock();
124123
var receivedValues = new List<string>();
125124
int executionCount = 0;
126125
Action<string> callback = value => {
127-
lock (@lock) {
126+
lock (Lock) {
128127
receivedValues.Add(value);
129128
}
130129
Interlocked.Increment(ref executionCount);
@@ -171,7 +170,7 @@ public async Task ThrottledDebouncer_ConcurrentValues_ShouldBeThreadSafe() {
171170
// Arrange
172171
var receivedValues = new List<string>();
173172
Func<string, Task> callback = value => {
174-
lock (receivedValues) {
173+
lock (Lock) {
175174
receivedValues.Add(value);
176175
}
177176
return Task.CompletedTask;
@@ -221,7 +220,9 @@ public async Task ThrottledDebouncer_DefaultValues_ShouldUseDefaultDebounceAndTh
221220
// Arrange
222221
var executionTimes = new List<DateTime>();
223222
Func<string, Task> callback = _ => {
224-
executionTimes.Add(DateTime.UtcNow);
223+
lock (Lock) {
224+
executionTimes.Add(DateTime.UtcNow);
225+
}
225226
return Task.CompletedTask;
226227
};
227228

@@ -244,8 +245,10 @@ public async Task ThrottledDebouncer_ZeroDebounce_ShouldExecuteImmediately() {
244245
string? receivedValue = null;
245246
var executionTime = DateTime.MinValue;
246247
Func<string, Task> callback = value => {
247-
receivedValue = value;
248-
executionTime = DateTime.UtcNow;
248+
lock (Lock) {
249+
receivedValue = value;
250+
executionTime = DateTime.UtcNow;
251+
}
249252
return Task.CompletedTask;
250253
};
251254

@@ -268,11 +271,20 @@ public async Task ThrottledDebouncer_LongRunningCallback_ShouldNotBlockSubsequen
268271
int executionCount = 0;
269272
bool isFirstCallRunning = false;
270273
Func<string, Task> callback = async value => {
271-
if (value == "slow") {
272-
isFirstCallRunning = true;
273-
await Task.Delay(200); // Simulate long-running operation
274-
isFirstCallRunning = false;
274+
lock (Lock) {
275+
if (value == "slow") {
276+
isFirstCallRunning = true;
277+
}
278+
}
279+
280+
await Task.Delay(200); // Simulate long-running operation
281+
282+
lock (Lock) {
283+
if (value == "slow") {
284+
isFirstCallRunning = false;
285+
}
275286
}
287+
276288
Interlocked.Increment(ref executionCount);
277289
};
278290

0 commit comments

Comments
 (0)