public async Task ResetTimerTest() { // Arrange int callbackCalledCount = 0; Task Callback() { Interlocked.Increment(ref callbackCalledCount); return(Task.CompletedTask); } TimeSpan period = TimeSpan.FromSeconds(3); var resettableTimer = new ResettableTimer(Callback, period, null); // Act resettableTimer.Start(); for (int i = 0; i < 5; i++) { await Task.Delay(TimeSpan.FromSeconds(2)); resettableTimer.Reset(); } await Task.Delay(TimeSpan.FromSeconds(4)); // Assert Assert.Equal(1, callbackCalledCount); }
public async Task DisableEnableTimerTest() { // Arrange int callbackCalledCount = 0; Task Callback() { Interlocked.Increment(ref callbackCalledCount); return(Task.CompletedTask); } TimeSpan period = TimeSpan.FromSeconds(15); var resettableTimer = new ResettableTimer(Callback, period, null); // Act resettableTimer.Start(); await Task.Delay(TimeSpan.FromSeconds(20)); // Assert Assert.Equal(1, callbackCalledCount); // Act resettableTimer.Disable(); await Task.Delay(TimeSpan.FromSeconds(20)); // Assert Assert.Equal(1, callbackCalledCount); // Act resettableTimer.Enable(); resettableTimer.Start(); await Task.Delay(TimeSpan.FromSeconds(20)); // Assert Assert.Equal(2, callbackCalledCount); }