private void MultipleWorkerMultipleTaskTestCase(WorkingLine wl, EventMonitor <WorkingLine> wlMon) { var q = wl.Queue; var tasks = Enumerable.Range(0, 30).Select(i => new TestTask($"multi {i}")).ToArray(); var taskMons = tasks.Select(t => new EventMonitor <TestTask>(t)).ToArray(); Assert.IsTrue(q.IsEmpty); wl.Enqueue(tasks); wl.WaitForEnd(timeout: 10000); Assert.IsTrue(q.IsEmpty); AssertState(wl, isDisposed: false, busy: false); Assert.AreEqual(tasks.Length, wlMon.FilterHistory(ByEventName(nameof(WorkingLine.TaskBegin))).Count); Assert.AreEqual(tasks.Length, wlMon.FilterHistory(ByEventName(nameof(WorkingLine.TaskEnd))).Count); wlMon.History.AssertSender(wl); wlMon.FilterHistory(ByPropertyChanges <bool>(nameof(WorkingLine.Busy))) .AssertPropertyValues(true, false); wlMon.FilterHistory(ByPropertyChanges <int>(nameof(WorkingLine.BusyWorkerCount))) .AssertPropertyValueChanges <int>() .AssertPropertyValues(1, 2, 3, 2, 1, 0); for (int i = 0; i < tasks.Length; i++) { var task = tasks[i]; var taskMon = taskMons[i]; taskMon.History.AssertSender(task); taskMon.FilterHistory(ByPropertyChanges <TaskState>(nameof(ITask.State))) .AssertPropertyValues( TaskState.InProgress, TaskState.CleaningUp, TaskState.Succeeded); } }
private void ParallelCancellationTestCase(WorkingLine wl, EventMonitor <WorkingLine> wlMon) { var q = wl.Queue; var tasks = Enumerable.Range(0, 18).Select(i => new TestTask(i.ToString())).ToArray(); var taskMons = tasks.Select(t => new EventMonitor <TestTask>(t)).ToArray(); var cancelTask = tasks[9]; cancelTask.StateChanging += (s, e) => { if (e.NewValue == TaskState.InProgress) { wl.Cancel(); } }; wl.Enqueue(tasks); Assert.IsTrue(wl.WaitForEnd(timeout: 4000)); AssertState(wl, isDisposed: false, busy: false); Assert.AreEqual(0, wl.CurrentTasks.Length); Assert.IsTrue(wl.Queue.IsEmpty); Assert.AreEqual(TaskState.Succeeded, tasks.First().State); Assert.AreEqual(TaskState.Obsolete, tasks.Last().State); Assert.AreEqual(tasks.Length, tasks.Where(t => t.State == TaskState.Succeeded || t.State == TaskState.Canceled || t.State == TaskState.Obsolete) .Count()); wlMon.FilterHistory(ByEventName( nameof(WorkingLine.BusyChanged), nameof(WorkingLine.Cancelled))) .AssertEventNames( nameof(WorkingLine.BusyChanged), nameof(WorkingLine.Cancelled), nameof(WorkingLine.BusyChanged)); wlMon.FilterHistory(ByEventName(nameof(WorkingLine.BusyChanged))) .AssertPropertyValues(true, false); wlMon.FilterHistory(ByEventName(nameof(WorkingLine.BusyWorkerCountChanged))) .AssertPropertyValues(1, 2, 3, 2, 1, 0); }
private void SingleWorkerSingleTaskTestCase(WorkingLine wl, EventMonitor <WorkingLine> wlMon) { var q = wl.Queue; var task = new TestTask("single"); var taskMon = new EventMonitor <TestTask>(task); Assert.IsTrue(q.IsEmpty); wl.Enqueue(task); Assert.IsFalse(q.IsEmpty); AssertState(wl, isDisposed: false, busy: false); wl.Start(); wl.WaitForEnd(timeout: 10000); Assert.IsTrue(q.IsEmpty); AssertState(wl, isDisposed: false, busy: false); wlMon.History.AssertSender(wl); wlMon.FilterHistory(ByEventName( nameof(WorkingLine.TaskBegin), nameof(WorkingLine.TaskEnd))) .AssertEventNames( nameof(WorkingLine.TaskBegin), nameof(WorkingLine.TaskEnd)); wlMon.FilterHistory(ByEventName( nameof(WorkingLine.BusyChanged), nameof(WorkingLine.BusyWorkerCountChanged))) .AssertEventNames( nameof(WorkingLine.BusyChanged), nameof(WorkingLine.BusyWorkerCountChanged), nameof(WorkingLine.BusyWorkerCountChanged), nameof(WorkingLine.BusyChanged)); wlMon.FilterHistory(ByPropertyChanges <bool>(nameof(WorkingLine.Busy))) .AssertPropertyValues(true, false); wlMon.FilterHistory(ByPropertyChanges <int>(nameof(WorkingLine.BusyWorkerCount))) .AssertPropertyValues(1, 0); taskMon.History.AssertSender(task); taskMon.FilterHistory(ByPropertyChanges <TaskState>(nameof(ITask.State))) .AssertPropertyValues( TaskState.InProgress, TaskState.CleaningUp, TaskState.Succeeded); }
private void SequentialCancellationTestCase(WorkingLine wl, EventMonitor <WorkingLine> wlMon) { var q = wl.Queue; var tasks = Enumerable.Range(0, 4).Select(i => new TestTask(i.ToString())).ToArray(); var cancelTask = tasks[1]; cancelTask.StateChanging += (s, e) => { if (e.NewValue == TaskState.InProgress) { wl.Cancel(); } }; wl.Enqueue(tasks); Assert.IsTrue(wl.WaitForEnd(timeout: 4000)); AssertState(wl, isDisposed: false, busy: false); Assert.AreEqual(0, wl.CurrentTasks.Length); Assert.IsTrue(wl.Queue.IsEmpty); Assert.AreEqual(TaskState.Succeeded, tasks[0].State); Assert.AreEqual(TaskState.Canceled, tasks[1].State); Assert.AreEqual(TaskState.Obsolete, tasks[2].State); Assert.AreEqual(TaskState.Obsolete, tasks[3].State); wlMon.History .AssertSender(wl) .AssertEventNames( nameof(WorkingLine.TaskBegin), nameof(WorkingLine.BusyChanged), nameof(WorkingLine.BusyWorkerCountChanged), nameof(WorkingLine.TaskEnd), nameof(WorkingLine.TaskBegin), nameof(WorkingLine.Cancelled), nameof(WorkingLine.TaskEnd), nameof(WorkingLine.BusyWorkerCountChanged), nameof(WorkingLine.BusyChanged)); }