Beispiel #1
0
        private void CancellationTestCase(WorkerThread wt, EventMonitor <WorkerThread> wtMon)
        {
            var q          = wt.Queue;
            var tasks      = Enumerable.Range(0, 4).Select(i => new TestTask(i.ToString())).ToArray();
            var taskMons   = tasks.Select(t => new EventMonitor <TestTask>(t)).ToArray();
            var cancelTask = tasks[1];

            cancelTask.StateChanging += (s, e) =>
            {
                if (e.NewValue == TaskState.InProgress)
                {
                    wt.Cancel();
                }
            };

            foreach (var task in tasks)
            {
                q.Enqueue(task);
            }

            Assert.IsTrue(wt.WaitForDeath(timeout: 4000));

            AssertState(wt, isDisposed: false, isAlive: false, busy: false);

            Assert.AreEqual(TaskState.Succeeded, tasks[0].State);
            Assert.AreEqual(TaskState.Canceled, tasks[1].State);
            Assert.AreEqual(TaskState.Waiting, tasks[2].State);
            Assert.AreEqual(TaskState.Waiting, tasks[3].State);

            wtMon.History.AssertEventNames(
                nameof(WorkerThread.BusyChanged),
                nameof(WorkerThread.CurrentTaskChanged),
                nameof(WorkerThread.TaskBegin),
                nameof(WorkerThread.TaskEnd),
                nameof(WorkerThread.CurrentTaskChanged),
                nameof(WorkerThread.TaskBegin),
                nameof(WorkerThread.Cancelled),
                nameof(WorkerThread.TaskEnd),
                nameof(WorkerThread.CurrentTaskChanged),
                nameof(WorkerThread.BusyChanged),
                nameof(WorkerThread.IsAliveChanged));
        }