public async Task Throttle() { var count = 0; var throttled = ThrottleExtensions.Throttle(() => count++, TimeSpan.FromMilliseconds(30)); throttled(); throttled(); await Task.Delay(50).ConfigureAwait(false); Assert.Equal(1, count); throttled(); await Task.Delay(15).ConfigureAwait(false); throttled(); await Task.Delay(15).ConfigureAwait(false); throttled(); await Task.Delay(15).ConfigureAwait(false); throttled(); await Task.Delay(50).ConfigureAwait(false); Assert.Equal(3, count); }
public async Task Throttle_CallActionsWithArgumentsOfTheLastCall() { int lastArg = default; var throttled = ThrottleExtensions.Throttle <int>((i) => lastArg = i, TimeSpan.FromMilliseconds(0)); throttled(1); throttled(2); await Task.Delay(1).ConfigureAwait(false); Assert.Equal(2, lastArg); }
public void Throttle_CallActionsWithArgumentsOfTheLastCall() { using var resetEvent = new ManualResetEventSlim(false); int lastArg = default; int count = 0; var debounced = ThrottleExtensions.Throttle <int>(i => { lastArg = i; count++; resetEvent.Set(); }, TimeSpan.FromMilliseconds(10)); debounced(1); debounced(2); resetEvent.Wait(); Assert.Equal(1, count); Assert.Equal(2, lastArg); }