Skip to content

Commit ebad7c0

Browse files
committed
Refactor: Ensure thread safety in debouncer tests by adding locking mechanisms
1 parent d341cf6 commit ebad7c0

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Tests.CodeOfChaos.Extensions.Debouncers.Regular;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// ReSharper disable ConvertToLocalFunction
1212
public class ActionDebouncerTests {
13+
private Lock Lock { get; } = new();
14+
1315
[Test]
1416
public async Task DefaultDebounceMs_ShouldBe100() {
1517
// Arrange
@@ -130,7 +132,9 @@ public async Task InvocationDuringDebounce_ShouldCancelPrevious_Action() {
130132
// Arrange
131133
var executionTimes = new List<DateTime>();
132134
Action callback = () => {
133-
executionTimes.Add(DateTime.UtcNow);
135+
lock (Lock) {
136+
executionTimes.Add(DateTime.UtcNow);
137+
}
134138
};
135139

136140
// Act

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task CustomDebounceMs_ShouldRespectSpecifiedTime_Func() {
3636
// Arrange
3737
int callCount = 0;
3838
Func<Task> callback = () => {
39-
callCount++;
39+
Interlocked.Increment(ref callCount);
4040
return Task.CompletedTask;
4141
};
4242

0 commit comments

Comments
 (0)