public void TaskSchedulerAsyncTest() { var strings = new List <string>(); var taskScheduler = new TaskSchedulerService(); taskScheduler.QueueTask(async() => { await Task.Delay(100); await AddStringAsync(strings, "a"); }); taskScheduler.QueueTask(async() => await AddStringAsync(strings, "b")); var lastTask = new ManualResetEvent(false); taskScheduler.QueueTask(() => { lastTask.Set(); return(Task.CompletedTask); }); lastTask.WaitOne(); Assert.IsTrue(strings.Take(100).All(x => x == "a"), string.Join(Environment.NewLine, strings)); Assert.IsTrue(strings.Skip(100).All(x => x == "b")); }
public void TaskSchedulerServiceTest() { var strings = new List <string>(); var taskScheduler = new TaskSchedulerService(); taskScheduler.QueueTask(() => { Thread.Sleep(10); for (int i = 0; i < 100; i++) { strings.Add("a"); } return(Task.CompletedTask); }); taskScheduler.QueueTask(() => { for (int i = 0; i < 100; i++) { strings.Add("b"); } return(Task.CompletedTask); }); var lastTask = new ManualResetEvent(false); taskScheduler.QueueTask(() => { lastTask.Set(); return(Task.CompletedTask); }); lastTask.WaitOne(); Assert.IsTrue(strings.Take(100).All(x => x == "a"), string.Join(Environment.NewLine, strings)); Assert.IsTrue(strings.Skip(100).All(x => x == "b")); }