public async Task TestThrottleAsync() { var taskTokens = new List <Task>(); var result = string.Empty; var task = new Interceptor <string>( v => result = v, InterceptorPolicy.Throttle(TimeSpan.FromMilliseconds(100)) ); Assert.IsFalse(task.IsWorking); _ = task.InvokeAsync("opq"); await Task.Delay(10); Assert.AreEqual("opq", result); _ = task.InvokeAsync("rst"); await Task.Delay(10); Assert.AreEqual("opq", result); _ = task.InvokeAsync("uvw"); await Task.Delay(110); Assert.AreEqual("opq", result); _ = task.InvokeAsync("xyz"); Assert.AreEqual("xyz", result); await task.WaitAsync(); Assert.IsFalse(task.IsWorking); }
/// <summary> /// Creates a throttle interceptor policy. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="duration">The duration.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// You may want to request to call an action only once in a short time /// even if you request to call several times. /// The rest will be ignored. /// So the handler will be frozen for a while after it has processed. /// </remarks> public static Func <T, Task> Throttle(Func <T, Task> action, TimeSpan duration) => Action(action, InterceptorPolicy.Throttle(duration));
/// <summary> /// Creates a throttle interceptor policy. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="duration">The duration.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// You may want to request to call an action only once in a short time /// even if you request to call several times. /// The rest will be ignored. /// So the handler will be frozen for a while after it has processed. /// </remarks> public static Action <T> Throttle(Action <T> action, TimeSpan duration) => Action(action, InterceptorPolicy.Throttle(duration));
private async Task TestKeyFeaturesAsync() { Console.WriteLine("Interceptor testing."); Console.WriteLine("Debounce"); var task = new Interceptor <string>( v => value = v, InterceptorPolicy.Debounce(TimeSpan.FromMilliseconds(200)) ); _ = task.InvokeAsync("b"); await Task.Delay(100); _ = task.InvokeAsync("c"); await Task.Delay(100); _ = task.InvokeAsync("d"); await Task.Delay(100); _ = task.InvokeAsync("e"); Check("a"); await Task.Delay(300); Check("e"); await task.WaitAsync(); Check("e"); Console.WriteLine("Throttle"); task.ResetDuration(); task.Policy = InterceptorPolicy.Throttle(TimeSpan.FromMilliseconds(800)); _ = task.InvokeAsync("f"); await Task.Delay(100); _ = task.InvokeAsync("g"); await Task.Delay(100); _ = task.InvokeAsync("h"); await Task.Delay(100); _ = task.InvokeAsync("i"); await Task.Delay(100); Check("f"); await task.WaitAsync(); Check("f"); Console.WriteLine("Multiple"); task.ResetDuration(); task.Policy = InterceptorPolicy.Mutliple(2, 4, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("j"); Check("f"); await Task.Delay(100); _ = task.InvokeAsync("k"); Check("k"); await Task.Delay(100); _ = task.InvokeAsync("l"); await Task.Delay(100); _ = task.InvokeAsync("m"); await Task.Delay(100); Check("m"); _ = task.InvokeAsync("n"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("m"); Console.WriteLine("Times 1"); task.ResetDuration(); task.Policy = InterceptorPolicy.Times(2, 3, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("o"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("p"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("q"); await Task.Delay(100); _ = task.InvokeAsync("r"); await Task.Delay(100); Check("m"); _ = task.InvokeAsync("s"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("m"); Console.WriteLine("Times 2"); task.ResetDuration(); task.Policy = InterceptorPolicy.Times(2, 3, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("t"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("u"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("v"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("v"); }