public async Task ScheduledTaskFiresOnTime(bool scheduleFromExecutor) { var scheduler = new SingleThreadEventExecutor(null, TimeSpan.FromMinutes(1)); var promise = new TaskCompletionSource(); Func <Task> scheduleFunc = () => scheduler.ScheduleAsync(() => promise.Complete(), TimeSpan.FromMilliseconds(100)); Task task = scheduleFromExecutor ? await scheduler.SubmitAsync(scheduleFunc) : scheduleFunc(); await Task.WhenAny(task, Task.Delay(TimeSpan.FromMilliseconds(300))); Assert.True(task.IsCompleted); }
public async Task ScheduledTaskFiresOnTimeWhileBusy() { var scheduler = new SingleThreadEventExecutor(null, TimeSpan.FromMilliseconds(10)); var promise = new TaskCompletionSource(); Action selfQueueAction = null; selfQueueAction = () => { if (!promise.Task.IsCompleted) { scheduler.Execute(selfQueueAction); } }; scheduler.Execute(selfQueueAction); Task task = scheduler.ScheduleAsync(() => promise.Complete(), TimeSpan.FromMilliseconds(100)); await Task.WhenAny(task, Task.Delay(TimeSpan.FromMilliseconds(300))); Assert.True(task.IsCompleted); }
public void STE_should_execute_scheduled_tasks_on_time() { var counter = new AtomicCounter(0); var executor = new SingleThreadEventExecutor("Foo" + ThreadNameCounter.GetAndIncrement(), TimeSpan.FromMilliseconds(100)); Action <object> increment = o => ((AtomicCounter)o).GetAndIncrement(); // schedule a delayed operation var checkCounter = executor.ScheduleAsync(o => Assert.True(((AtomicCounter)o).Current == 4), counter, TimeSpan.FromMilliseconds(40)); // schedule 4 immediate operations executor.Execute(increment, counter); executor.Execute(increment, counter); executor.Execute(increment, counter); executor.Execute(increment, counter); // delay should run after the first 4 previous checkCounter.Wait(TimeSpan.FromMilliseconds(100)); }