-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debouncing to FileSystemWatcher events (#129)
* debounces FileSystemWatcher events * makes debounce interval configurable * ignores SQL timeout in unit tests (the purpose is to load the native libraries, not the actual connection to the database)
- Loading branch information
Showing
6 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace McMaster.NETCore.Plugins.Internal | ||
{ | ||
internal class Debouncer : IDisposable | ||
{ | ||
private readonly CancellationTokenSource _cts = new CancellationTokenSource(); | ||
private readonly TimeSpan _waitTime; | ||
private int _counter; | ||
|
||
public Debouncer(TimeSpan waitTime) | ||
{ | ||
_waitTime = waitTime; | ||
} | ||
|
||
public void Execute(Action action) | ||
{ | ||
var current = Interlocked.Increment(ref _counter); | ||
|
||
Task.Delay(_waitTime).ContinueWith(task => | ||
{ | ||
// Is this the last task that was queued? | ||
if (current == _counter && !_cts.IsCancellationRequested) | ||
{ | ||
action(); | ||
} | ||
task.Dispose(); | ||
}, _cts.Token); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_cts.Cancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
McMaster.NETCore.Plugins.Loader.AssemblyLoadContextBuilder.ShadowCopyNativeLibraries() -> McMaster.NETCore.Plugins.Loader.AssemblyLoadContextBuilder | ||
McMaster.NETCore.Plugins.Loader.AssemblyLoadContextBuilder.ShadowCopyNativeLibraries() -> McMaster.NETCore.Plugins.Loader.AssemblyLoadContextBuilder | ||
McMaster.NETCore.Plugins.PluginConfig.ReloadDelay.get -> System.TimeSpan | ||
McMaster.NETCore.Plugins.PluginConfig.ReloadDelay.set -> void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using McMaster.NETCore.Plugins.Internal; | ||
using Xunit; | ||
|
||
namespace McMaster.NETCore.Plugins.Tests | ||
{ | ||
public class DebouncerTests | ||
{ | ||
[Fact] | ||
public async Task InvocationIsDelayed() | ||
{ | ||
var executionCounter = 0; | ||
|
||
var debouncer = new Debouncer(TimeSpan.FromSeconds(.1)); | ||
debouncer.Execute(() => executionCounter++); | ||
|
||
Assert.Equal(0, executionCounter); | ||
|
||
await Task.Delay(TimeSpan.FromSeconds(.5)); | ||
|
||
Assert.Equal(1, executionCounter); | ||
} | ||
|
||
[Fact] | ||
public async Task ActionsAreDebounced() | ||
{ | ||
var executionCounter = 0; | ||
|
||
var debouncer = new Debouncer(TimeSpan.FromSeconds(.1)); | ||
debouncer.Execute(() => executionCounter++); | ||
debouncer.Execute(() => executionCounter++); | ||
debouncer.Execute(() => executionCounter++); | ||
|
||
await Task.Delay(TimeSpan.FromSeconds(.5)); | ||
|
||
Assert.Equal(1, executionCounter); | ||
} | ||
|
||
[Fact] | ||
public async Task OnlyLastActionIsInvoked() | ||
{ | ||
string? invokedAction = null; | ||
|
||
var debouncer = new Debouncer(TimeSpan.FromSeconds(.1)); | ||
foreach (var action in new[]{"a", "b", "c"}) | ||
{ | ||
debouncer.Execute(() => invokedAction = action); | ||
} | ||
|
||
await Task.Delay(TimeSpan.FromSeconds(.5)); | ||
|
||
Assert.NotNull(invokedAction); | ||
Assert.Equal("c", invokedAction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters