private ManagedTestCommand CreateCommand(Dictionary <Test, ManagedTestCommand> commands,
                                                 Test test, IEnumerable <ManagedTestCommand> children, bool isExplicit, ITestContextManager contextManager)
        {
            var testMonitor = new ManagedTestCommand(contextManager, test, isExplicit);

            foreach (ManagedTestCommand child in children)
            {
                testMonitor.AddChild(child);
            }

            commands.Add(test, testMonitor);
            return(testMonitor);
        }
        private void SortChildren(ManagedTestCommand parent, MultiMap<ManagedTestCommand, ManagedTestCommand> siblingDependencies)
        {
            ManagedTestCommand[] children = parent.ChildrenToArray();
            if (children.Length == 0)
                return;

            // Clear the array of children since we are about to reshuffle them.
            parent.ClearChildren();

            // Sort the children by order.  Because the topological sort emits vertices precisely
            // in the ordert that it visits them (depth-first) it will preserve the relative ordering
            // of independent vertices.  So we influence test execution order by pre-sorting.
            // Dependencies will of course interfere with the ordering slightly.  However, if the
            // user explicitly specifies orderings in dependency order then they'll indeed run in
            // that specified order.  -- Jeff.
            SortCommandsByOrder(children);

            // Perform a topological sort of the children using depth-first search.
            // Because at this stage a command only has dependencies on its siblings the depth-first search
            // actually proceeds down the chain of sibling dependencies only; it does not
            // traverse the whole test hierarchy.  -- Jeff.
            Dictionary<ManagedTestCommand, bool> visitedSet = new Dictionary<ManagedTestCommand, bool>();
            Stack<DepthFirstEntry> stack = new Stack<DepthFirstEntry>();

            stack.Push(new DepthFirstEntry(null, children));
            for (;;)
            {
                DepthFirstEntry top = stack.Peek();
                if (top.DependencyEnumerator.MoveNext())
                {
                    ManagedTestCommand current = top.DependencyEnumerator.Current;

                    bool inProgressFlag;
                    if (visitedSet.TryGetValue(current, out inProgressFlag))
                    {
                        if (inProgressFlag)
                            throw new ModelException(String.Format("Found a test dependency cycle involving test '{0}'.",
                                current.Test.FullName));
                    }
                    else
                    {
                        IList<ManagedTestCommand> unorderedDependencies = siblingDependencies[current];
                        if (unorderedDependencies.Count != 0)
                        {
                            visitedSet[current] = true;

                            // We need to sort all visited children so that dependencies run in relative order.
                            ManagedTestCommand[] dependencies = GenericCollectionUtils.ToArray(unorderedDependencies);
                            SortCommandsByOrder(dependencies);

                            stack.Push(new DepthFirstEntry(current, dependencies));
                        }
                        else
                        {
                            parent.AddChild(current);
                            visitedSet[current] = false;
                        }
                    }
                }
                else
                {
                    ManagedTestCommand current = top.Source;
                    if (current == null)
                        break;

                    parent.AddChild(current);
                    visitedSet[current] = false;
                    stack.Pop();
                }
            }

            // Recursively sort the children of this command.
            foreach (ManagedTestCommand child in children)
                SortChildren(child, siblingDependencies);
        }
        private ManagedTestCommand CreateCommand(Dictionary<Test, ManagedTestCommand> commands,
            Test test, IEnumerable<ManagedTestCommand> children, bool isExplicit, ITestContextManager contextManager)
        {
            var testMonitor = new ManagedTestCommand(contextManager, test, isExplicit);
            foreach (ManagedTestCommand child in children)
                testMonitor.AddChild(child);

            commands.Add(test, testMonitor);
            return testMonitor;
        }
        private void SortChildren(ManagedTestCommand parent, MultiMap <ManagedTestCommand, ManagedTestCommand> siblingDependencies)
        {
            ManagedTestCommand[] children = parent.ChildrenToArray();
            if (children.Length == 0)
            {
                return;
            }

            // Clear the array of children since we are about to reshuffle them.
            parent.ClearChildren();

            // Sort the children by order.  Because the topological sort emits vertices precisely
            // in the ordert that it visits them (depth-first) it will preserve the relative ordering
            // of independent vertices.  So we influence test execution order by pre-sorting.
            // Dependencies will of course interfere with the ordering slightly.  However, if the
            // user explicitly specifies orderings in dependency order then they'll indeed run in
            // that specified order.  -- Jeff.
            SortCommandsByOrder(children);

            // Perform a topological sort of the children using depth-first search.
            // Because at this stage a command only has dependencies on its siblings the depth-first search
            // actually proceeds down the chain of sibling dependencies only; it does not
            // traverse the whole test hierarchy.  -- Jeff.
            Dictionary <ManagedTestCommand, bool> visitedSet = new Dictionary <ManagedTestCommand, bool>();
            Stack <DepthFirstEntry> stack = new Stack <DepthFirstEntry>();

            stack.Push(new DepthFirstEntry(null, children));
            for (;;)
            {
                DepthFirstEntry top = stack.Peek();
                if (top.DependencyEnumerator.MoveNext())
                {
                    ManagedTestCommand current = top.DependencyEnumerator.Current;

                    bool inProgressFlag;
                    if (visitedSet.TryGetValue(current, out inProgressFlag))
                    {
                        if (inProgressFlag)
                        {
                            throw new ModelException(String.Format("Found a test dependency cycle involving test '{0}'.",
                                                                   current.Test.FullName));
                        }
                    }
                    else
                    {
                        IList <ManagedTestCommand> unorderedDependencies = siblingDependencies[current];
                        if (unorderedDependencies.Count != 0)
                        {
                            visitedSet[current] = true;

                            // We need to sort all visited children so that dependencies run in relative order.
                            ManagedTestCommand[] dependencies = GenericCollectionUtils.ToArray(unorderedDependencies);
                            SortCommandsByOrder(dependencies);

                            stack.Push(new DepthFirstEntry(current, dependencies));
                        }
                        else
                        {
                            parent.AddChild(current);
                            visitedSet[current] = false;
                        }
                    }
                }
                else
                {
                    ManagedTestCommand current = top.Source;
                    if (current == null)
                    {
                        break;
                    }

                    parent.AddChild(current);
                    visitedSet[current] = false;
                    stack.Pop();
                }
            }

            // Recursively sort the children of this command.
            foreach (ManagedTestCommand child in children)
            {
                SortChildren(child, siblingDependencies);
            }
        }