Exemplo n.º 1
0
        public void SimpleGraphExecute_WithConcurrency_NoThrottle_WithException()
        {
            var stack = new ConcurrentStack <int>();

            var node0 = new Vertex(() => stack.Push(1));
            var node1 = new Vertex(() => stack.Push(2));
            var node2 = new Vertex(() => { throw new Exception(); });
            var node3 = new Vertex(() => stack.Push(2));
            var node4 = new Vertex(() => stack.Push(3));

            node3.AddDependencies(node0);
            node2.AddDependencies(node0);
            node1.AddDependencies(node0);

            node4.AddDependencies(node1, node2, node3);

            var executive = new GraphExecutive(new DirectedAcyclicGraph(node0, node1, node2, node3, node4));

            executive.ExecuteAndWait();

            Assert.IsTrue(executive.VerticesFailed.Count == 2);

            var vals = stack.ToArray();

            var expected = new[] { 2, 2, 1 };

            for (var i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], vals[i]);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Create six node that each do nothing for at least 1 second
            var nodeA = new Vertex(() => Thread.Sleep(1000));
            var nodeB = new Vertex(() => Thread.Sleep(1000));
            var nodeC = new Vertex(() => Thread.Sleep(1000));
            var nodeD = new Vertex(() => Thread.Sleep(1000));
            var nodeE = new Vertex(() => Thread.Sleep(1000));
            var nodeF = new Vertex(() => Thread.Sleep(1000));

            //Create a DAG from the nodes
            nodeC.AddDependencies(nodeA, nodeB);
            nodeD.AddDependencies(nodeC);
            nodeE.AddDependencies(nodeC);
            nodeF.AddDependencies(nodeB);
            var graph = new DirectedAcyclicGraph(nodeA, nodeB, nodeC, nodeD, nodeE, nodeF);

            //Create a graph executive
            var graphExecutive = new GraphExecutive(graph, new ConcurrencyThrottle(8));

            graphExecutive.ExecuteAndWait();

            //Output result
            Console.WriteLine("Graph Execution Complete");
            Console.WriteLine(graphExecutive.VerticesFailed.Any() ? "\tn nodes failed" : "\tall nodes successful");

#if DEBUG
            Console.ReadLine();
#endif
        }
Exemplo n.º 3
0
        public void SimpleGraphExecute_WithConcurrency_NoThrottle()
        {
            //TODO: Sledgehammer, should be improved
            for (int ii = 0; ii < 1000; ii++)
            {
                var stack = new ConcurrentStack <int>();

                var node0 = new Vertex(() => stack.Push(1));
                var node1 = new Vertex(() => stack.Push(2));
                var node2 = new Vertex(() => stack.Push(2));
                var node3 = new Vertex(() => stack.Push(2));
                var node4 = new Vertex(() => stack.Push(3));

                node3.AddDependencies(node0);
                node2.AddDependencies(node0);
                node1.AddDependencies(node0);

                node4.AddDependencies(node1, node2, node3);

                var executive = new GraphExecutive(new DirectedAcyclicGraph(node0, node1, node2, node3, node4));

                executive.ExecuteAndWait();

                Assert.IsTrue(executive.VerticesFailed.Count == 0);

                var vals = stack.ToArray();

                var expected = new[] { 3, 2, 2, 2, 1 };

                for (var i = 0; i < expected.Length; i++)
                {
                    Assert.AreEqual(expected[i], vals[i]);
                }
            }
        }