private CountdownEvent _countdownEvent; // event to signal the main thread that the whole task tree has been created #endregion /// <summary> /// .ctor /// </summary> public TaskCancelWaitTest(TestParameters parameters) { _api = parameters.API_CancelWait; _waitBy = parameters.WaitBy_CancelWait; _waitTimeout = parameters.WaitTime; _taskTree = parameters.RootNode; _countdownEvent = new CountdownEvent(CaluateLeafNodes(_taskTree)); }
public static void TaskCancelWait5() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Light, "RespectParentCancellation"); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait4() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Light, "AttachedToParent"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.VeryHeavy, "RespectParentCancellation"); node.AddChildren(new[] { node_1, }); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait1() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Heavy, string.Empty, true); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Light, string.Empty); node.AddChildren(new[] { node_1, }); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait52() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryLight, "None"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Medium, "LongRunning, RespectParentCancellation"); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.VeryLight, "LongRunning, AttachedToParent"); TaskInfo node_3 = new TaskInfo(node, "node_3", WorkloadType.Medium, "RespectParentCancellation"); TaskInfo node_4 = new TaskInfo(node, "node_4", WorkloadType.VeryHeavy, "LongRunning"); TaskInfo node_5 = new TaskInfo(node, "node_5", WorkloadType.VeryHeavy, "LongRunning, RespectParentCancellation"); TaskInfo node_6 = new TaskInfo(node, "node_6", WorkloadType.VeryLight, "AttachedToParent"); TaskInfo node_7 = new TaskInfo(node, "node_7", WorkloadType.VeryLight, "LongRunning"); node.AddChildren(new[] { node_1, node_2, node_3, node_4, node_5, node_6, node_7, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 0); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait47() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Light, "LongRunning, RespectParentCancellation"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.VeryLight, "LongRunning"); TaskInfo node_1_1 = new TaskInfo(node_1, "node_1_1", WorkloadType.VeryLight, "LongRunning, RespectParentCancellation"); TaskInfo node_1_2 = new TaskInfo(node_1, "node_1_2", WorkloadType.Medium, "None"); node_1.AddChildren(new[] { node_1_1, node_1_2, }); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.VeryHeavy, "RespectParentCancellation"); TaskInfo node_2_1 = new TaskInfo(node_2, "node_2_1", WorkloadType.Light, "LongRunning, RespectParentCancellation"); TaskInfo node_2_2 = new TaskInfo(node_2, "node_2_2", WorkloadType.VeryHeavy, "None"); node_2.AddChildren(new[] { node_2_1, node_2_2, }); node.AddChildren(new[] { node_1, node_2, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 0); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait49() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryLight, "LongRunning, AttachedToParent"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 0); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait120() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryLight, "LongRunning, RespectParentCancellation"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 27); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public TaskInfo(TaskInfo parent, string TaskInfo_CancelWaitName, WorkloadType workType, string optionsString, bool cancelChildren) : this(parent, TaskInfo_CancelWaitName, workType, optionsString) { CancelChildren = cancelChildren; }
public static void TaskCancelWait126() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Heavy, "AttachedToParent"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.VeryLight, "LongRunning, AttachedToParent"); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.VeryLight, "None"); node.AddChildren(new[] { node_1, node_2, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 27); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
private void VerifyResult(TaskInfo current) { TaskInfo ti = current; WorkloadType workType = ti.WorkType; if (workType == WorkloadType.Exceptional && _api != API.Cancel) { bool expCaught = FindException((ex) => { TPLTestException expectedExp = ex as TPLTestException; return expectedExp != null && expectedExp.FromTaskId == ti.Task.Id; }); if (!expCaught) Assert.True(false, string.Format("expected TPLTestException in Task.Name = Task{0} NOT caught", current.Name)); } else { if (ti.Task.Exception != null && _api == API.Wait) Assert.True(false, string.Format("UNEXPECTED exception in Task.Name = Task{0} caught. Exception: {1}", current.Name, ti.Task.Exception)); if (ti.Task.IsCanceled && ti.Result != -42) { //this means that the task was not scheduled - it was cancelled or it is still in the queue //-42 = UNINITIALED_RESULT Assert.True(false, string.Format("Result must remain uninitialized for unstarted task")); } else if (ti.Task.IsCompleted) { //Function point comparison cant be done by rounding off to nearest decimal points since //1.64 could be represented as 1.63999999 or as 1.6499999999. To perform floating point comparisons, //a range has to be defined and check to ensure that the result obtained is within the specified range double minLimit = 1.63; double maxLimit = 1.65; if (ti.Result < minLimit || ti.Result > maxLimit) Assert.True(ti.Task.IsCanceled || ti.Task.IsFaulted, string.Format( "Expected Result to lie between {0} and {1} for completed task. Actual Result {2}. Using n={3} IsCanceled={4}", minLimit, maxLimit, ti.Result, workType, ti.Task.IsCanceled)); } } }
public static void TaskCancelWait31() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Heavy, "RespectParentCancellation"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
/// <summary> /// Walk the tree and calculates the tree nodes count /// </summary> /// <param name="tree"></param> private int CaluateLeafNodes(TaskInfo tree) { if (tree.IsLeaf) return 1; int sum = 0; foreach (TaskInfo child in tree.Children) sum += CaluateLeafNodes(child); return sum; }
public static void TaskCancelWait19() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Medium, "LongRunning, AttachedToParent"); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait24() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Medium, "LongRunning"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Medium, "LongRunning, AttachedToParent"); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.Light, "None"); node.AddChildren(new[] { node_1, node_2, }); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait141() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Heavy, "LongRunning, RespectParentCancellation"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.TimeSpan, 27); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait137() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Heavy, "RespectParentCancellation"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Heavy, "RespectParentCancellation"); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.Light, "RespectParentCancellation"); node.AddChildren(new[] { node_1, node_2, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.TimeSpan, 27); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait14() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryLight, "AttachedToParent"); TestParameters parameters = new TestParameters(node, API.Cancel, WaitBy.None, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public static void TaskCancelWait53() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Medium, "AttachedToParent"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Heavy, "LongRunning"); node.AddChildren(new[] { node_1, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 0); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
/// <summary> /// Verify the Wait code path /// </summary> private void VerifyWait(TaskInfo current) { TaskInfo ti = current; TaskInfo parent = current.Parent; if (_taskCompleted) { if (parent == null) { Assert.True(ti.Task.IsCompleted, "Root task must complete"); } else if (parent != null && parent.Task.IsCompleted) { if ((ti.Option & TaskCreationOptions.AttachedToParent) != 0 && !ti.Task.IsCompleted) { Assert.True(false, string.Format("Inner attached task must complete")); } } } }
/// <summary> /// recursively walk the tree and attach the tasks to the nodes /// </summary> private void CreateTask(TaskScheduler tm, TaskInfo treeNode) { treeNode.Task = Task.Factory.StartNew( delegate (object o) { TaskInfo current = (TaskInfo)o; if (current.IsLeaf) { if (!_countdownEvent.IsSet) _countdownEvent.Signal(); } else { // create children tasks foreach (TaskInfo child in current.Children) { if (child.IsRespectParentCancellation) { // // if child to respect parent cancellation we need to wire a linked token // child.CancellationToken = CancellationTokenSource.CreateLinkedTokenSource(treeNode.CancellationToken, child.CancellationToken).Token; } CreateTask(tm, child); } } if (current.CancelChildren) { try { foreach (TaskInfo child in current.Children) { child.CancellationTokenSource.Cancel(); } } finally { // stop the tree creation and let the main thread proceed if (!_countdownEvent.IsSet) { _countdownEvent.Signal(_countdownEvent.CurrentCount); } } } // run the workload current.RunWorkload(); }, treeNode, treeNode.CancellationToken, treeNode.Option, tm); }
public static void TaskCancelWait124() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryLight, "None"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.Millisecond, 27); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
/// <summary> /// Cancel Verification /// </summary> private void VerifyCancel(TaskInfo current) { TaskInfo ti = current; if (current.Parent == null) { if (!ti.CancellationToken.IsCancellationRequested) Assert.True(false, string.Format("Root task must be cancel-requested")); else if (_countdownEvent.IsSet && ti.Task.IsCanceled) Assert.True(false, string.Format("Root task should not be cancelled when the whole tree has been created")); } else if (current.Parent.CancelChildren) { // need to make sure the parent task at least called .Cancel() on the child if (!ti.CancellationToken.IsCancellationRequested) Assert.True(false, string.Format("Task which has been explictly cancel-requested either by parent must have CancellationRequested set as true")); } else if (ti.IsRespectParentCancellation) { if (ti.CancellationToken.IsCancellationRequested != current.Parent.CancellationToken.IsCancellationRequested) Assert.True(false, string.Format("Task with RespectParentCancellationcontract is broken")); } else { if (ti.CancellationToken.IsCancellationRequested || ti.Task.IsCanceled) Assert.True(false, string.Format("Inner non-directly canceled task which opts out RespectParentCancellationshould not be cancelled")); } // verify IsCanceled indicate successfully dequeued based on the observing that // - Thread is recorded the first thing in the RunWorkload from user delegate //if (ti.Task.IsCompleted && (ti.Thread == null) != ti.Task.IsCanceled) // Assert.Fail("IsCanceled contract is broken -- completed task which has the delegate executed can't have IsCanceled return true") }
public static void TaskCancelWait38() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.Medium, "AttachedToParent"); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.TimeSpan, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
private void VerifyTaskCanceledException(TaskInfo current) { bool expCaught; TaskInfo ti = current; //a task will get into cancelled state only if: //1.Its token was cancelled before as its action to get invoked //2.The token was cancelled before the task's action to finish, task observed the cancelled token and threw OCE(token) if (ti.Task.Status == TaskStatus.Canceled) { expCaught = FindException((ex) => { TaskCanceledException expectedExp = ex as TaskCanceledException; return expectedExp != null && expectedExp.Task == ti.Task; }); Assert.True(expCaught, "expected TaskCanceledException in Task.Name = Task " + current.Name + " NOT caught"); } else { expCaught = FindException((ex) => { TaskCanceledException expectedExp = ex as TaskCanceledException; return expectedExp != null && expectedExp.Task == ti.Task; }); Assert.False(expCaught, "NON-expected TaskCanceledException in Task.Name = Task " + current.Name + " caught"); } }
public TestParameters(TaskInfo rootNode, API api_CancelWait, WaitBy waitBy_CancelWait, int waitTime) { WaitBy_CancelWait = waitBy_CancelWait; WaitTime = waitTime; RootNode = rootNode; API_CancelWait = api_CancelWait; }
public static void TaskCancelWait40() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryHeavy, "LongRunning, RespectParentCancellation"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Medium, "RespectParentCancellation"); TaskInfo node_1_1 = new TaskInfo(node_1, "node_1_1", WorkloadType.VeryHeavy, "RespectParentCancellation"); node_1.AddChildren(new[] { node_1_1, }); TaskInfo node_2 = new TaskInfo(node, "node_2", WorkloadType.VeryHeavy, "LongRunning, RespectParentCancellation"); TaskInfo node_3 = new TaskInfo(node, "node_3", WorkloadType.Heavy, "RespectParentCancellation"); TaskInfo node_3_1 = new TaskInfo(node_3, "node_3_1", WorkloadType.Medium, "RespectParentCancellation"); TaskInfo node_3_2 = new TaskInfo(node_3, "node_3_2", WorkloadType.Light, "AttachedToParent"); node_3.AddChildren(new[] { node_3_1, node_3_2, }); node.AddChildren(new[] { node_1, node_2, node_3, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.TimeSpan, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
public TaskInfo(TaskInfo parent, string TaskInfo_CancelWaitName, WorkloadType workType, string optionsString) { Children = new LinkedList<TaskInfo>(); Result = s_UNINITIALED_RESULT; Option = s_DEFAULT_OPTION; Name = TaskInfo_CancelWaitName; WorkType = workType; Parent = parent; CancelChildren = false; CancellationTokenSource = new CancellationTokenSource(); CancellationToken = CancellationTokenSource.Token; if (string.IsNullOrEmpty(optionsString)) return; // // Parse Task CreationOptions, if RespectParentCancellation we would want to acknowledge that // and passed the remaining options for creation // string[] options = optionsString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int index = -1; for (int i = 0; i < options.Length; i++) { string o = options[i].Trim(); // remove any white spaces. options[i] = o; if (o.Equals("RespectParentCancellation", StringComparison.OrdinalIgnoreCase)) { IsRespectParentCancellation = true; index = i; } } if (index != -1) { string[] temp = new string[options.Length - 1]; int excludeIndex = index + 1; Array.Copy(options, 0, temp, 0, index); int leftToCopy = options.Length - excludeIndex; Array.Copy(options, excludeIndex, temp, index, leftToCopy); options = temp; } if (options.Length > 0) { TaskCreationOptions parsedOptions; string joinedOptions = string.Join(",", options); bool parsed = Enum.TryParse<TaskCreationOptions>(joinedOptions, out parsedOptions); if (!parsed) throw new NotSupportedException("could not parse the options string: " + joinedOptions); Option = parsedOptions; } }
public static void TaskCancelWait42() { TaskInfo node = new TaskInfo(null, "node", WorkloadType.VeryHeavy, "LongRunning"); TaskInfo node_1 = new TaskInfo(node, "node_1", WorkloadType.Light, "LongRunning, RespectParentCancellation"); node.AddChildren(new[] { node_1, }); TestParameters parameters = new TestParameters(node, API.Wait, WaitBy.TimeSpan, -1); TaskCancelWaitTest test = new TaskCancelWaitTest(parameters); test.RealRun(); }
internal void AddChildren(TaskInfo[] children) { foreach (var child in children) Children.AddLast(child); }
public TaskInfo(TaskInfo parent, string TaskInfo_CancelWaitName, WorkloadType workType, string optionsString) { Children = new LinkedList <TaskInfo>(); Result = s_UNINITIALED_RESULT; Option = s_DEFAULT_OPTION; Name = TaskInfo_CancelWaitName; WorkType = workType; Parent = parent; CancelChildren = false; CancellationTokenSource = new CancellationTokenSource(); CancellationToken = CancellationTokenSource.Token; if (string.IsNullOrEmpty(optionsString)) { return; } // // Parse Task CreationOptions, if RespectParentCancellation we would want to acknowledge that // and passed the remaining options for creation // string[] options = optionsString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int index = -1; for (int i = 0; i < options.Length; i++) { string o = options[i].Trim(); // remove any white spaces. options[i] = o; if (o.Equals("RespectParentCancellation", StringComparison.OrdinalIgnoreCase)) { IsRespectParentCancellation = true; index = i; } } if (index != -1) { string[] temp = new string[options.Length - 1]; int excludeIndex = index + 1; Array.Copy(options, 0, temp, 0, index); int leftToCopy = options.Length - excludeIndex; Array.Copy(options, excludeIndex, temp, index, leftToCopy); options = temp; } if (options.Length > 0) { TaskCreationOptions parsedOptions; string joinedOptions = string.Join(",", options); bool parsed = Enum.TryParse <TaskCreationOptions>(joinedOptions, out parsedOptions); if (!parsed) { throw new NotSupportedException("could not parse the options string: " + joinedOptions); } Option = parsedOptions; } }