Skip to content

Commit 78e5072

Browse files
committed
Refactor: Improve thread safety in debouncer tests using Interlocked.Increment and adjust test delays
1 parent e2b9072 commit 78e5072

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

tests/Tests.CodeOfChaos.Extensions/Debouncers/Regular/ActionDebouncerTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public async Task DefaultDebounceMs_ShouldBe100() {
1919

2020
// ReSharper disable once ConvertToLocalFunction
2121
Action callback = () => {
22-
callCount++;
22+
Interlocked.Increment(ref callCount);
2323
};
2424

2525
// Act
@@ -39,15 +39,15 @@ public async Task CustomDebounceMs_ShouldRespectSpecifiedTime_Action() {
3939

4040
// ReSharper disable once ConvertToLocalFunction
4141
Action callback = () => {
42-
callCount++;
42+
Interlocked.Increment(ref callCount);
4343
};
4444

4545
const int customDebounceMs = 200;
4646

4747
// Act
4848
await using Debouncer debouncer = Debouncer.FromDelegate(callback, customDebounceMs);
4949
await debouncer.InvokeDebouncedAsync();
50-
await Task.Delay(150);// Less than debouncing time
50+
await Task.Delay(50);// Less than debouncing time
5151

5252
// Assert
5353
await Assert.That(callCount).IsEqualTo(0);
@@ -65,7 +65,7 @@ public async Task MultipleInvocations_ShouldDebounce() {
6565

6666
// ReSharper disable once ConvertToLocalFunction
6767
Action callback = () => {
68-
callCount++;
68+
Interlocked.Increment(ref callCount);
6969
};
7070

7171
// Act
@@ -85,7 +85,7 @@ public async Task ConcurrentInvocations_ShouldBeThreadSafe() {
8585
// Arrange
8686
int callCount = 0;
8787
Action callback = () => {
88-
callCount++;
88+
Interlocked.Increment(ref callCount);
8989
};
9090

9191
// Act
@@ -138,9 +138,8 @@ public async Task InvocationDuringDebounce_ShouldCancelPrevious_Action() {
138138
};
139139

140140
// Act
141-
await using Debouncer debouncer = Debouncer.FromDelegate(callback);
141+
await using Debouncer debouncer = Debouncer.FromDelegate(callback, debounceMs:100);
142142
await debouncer.InvokeDebouncedAsync();
143-
await Task.Delay(50);// Wait half the debounced time
144143
await debouncer.InvokeDebouncedAsync();
145144
await Task.Delay(150);
146145
await debouncer.FlushAsync();

tests/Tests.CodeOfChaos.Extensions/Debouncers/Regular/FuncDebouncerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public async Task InvocationDuringDebounce_ShouldCancelPrevious_Func() {
139139
// Act
140140
await using Debouncer debouncer = Debouncer.FromDelegate(callback, debounceMs: 200);
141141
await debouncer.InvokeDebouncedAsync();
142-
await Task.Delay(50);
143142
await debouncer.InvokeDebouncedAsync();
144143
await Task.Delay(150);
145144
await debouncer.FlushAsync();

0 commit comments

Comments
 (0)