-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDebounceCommand.tests.cs
More file actions
57 lines (47 loc) · 1.64 KB
/
DebounceCommand.tests.cs
File metadata and controls
57 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Threading;
using System.Threading.Tasks;
using Float.Core.Commands;
#if NETCOREAPP3_1
using Xamarin.Forms;
#else
using Microsoft.Maui;
using Microsoft.Maui.Controls;
#endif
using Xunit;
namespace Float.Core.Tests
{
public class DebounceCommandTests
{
Timer timer;
[Fact]
public async Task TestDoesDebounceTimer()
{
var count = 0;
var incrementCommand = new Command(() => count += 1);
// only allow an increment every 450ms
var debouncedIncrementCommand = new DebounceCommand(incrementCommand, 450);
// attempt to increment every 100ms
timer = new Timer(arg => debouncedIncrementCommand.Execute(arg), null, 100, 100);
// wait one second
await Task.Delay(1000);
// in one second, we should have allowed at most two increments
Assert.Equal(2, count);
}
[Fact(Skip = "Flaky")]
public async Task TestDoesDebounceWhenAll()
{
var count = 0;
var incrementCommand = new Command(() => count += 1);
var debouncedIncrementCommand = new DebounceCommand(incrementCommand, 500);
await Task.WhenAll(new[]
{
Task.Run(() => debouncedIncrementCommand.Execute(default)),
Task.Run(() => debouncedIncrementCommand.Execute(default)),
Task.Run(() => debouncedIncrementCommand.Execute(default)),
Task.Run(() => debouncedIncrementCommand.Execute(default)),
});
await Task.Delay(500);
Assert.Equal(1, count);
}
}
}