Example #1
0
        public void SimpleGraphExecute_NoConcurrency()
        {
            var stack = new Stack <int>();

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

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

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

            var resetEvent = new ManualResetEventSlim();

            executive.OnGraphExecutionFinished += (sender, ergs) => resetEvent.Set();

            executive.Execute();

            resetEvent.Wait();

            for (var i = 3; i > 0; i--)
            {
                Assert.AreEqual(i, stack.Pop());
            }
        }
Example #2
0
        public void ConcurrencyThrottle()
        {
            var maxCount = new MaxCount();

            var node0  = new Vertex(() => Thread.Sleep(100));
            var node10 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node11 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node12 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node13 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node14 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node15 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });
            var node16 = new Vertex(() =>
            {
                maxCount.Inc();
                Thread.Sleep(1000);
                maxCount.Dec();
            });

            node10.AddDependencies(node0);
            node11.AddDependencies(node0);
            node12.AddDependencies(node0);
            node13.AddDependencies(node0);
            node14.AddDependencies(node0);
            node15.AddDependencies(node0);
            node16.AddDependencies(node0);

            var executive = new GraphExecutive(new DirectedAcyclicGraph(node0), new ConcurrencyThrottle(2));

            var resetEvent = new ManualResetEventSlim();

            executive.OnGraphExecutionFinished += (sender, ergs) => resetEvent.Set();

            executive.Execute();

            resetEvent.Wait();

            Assert.IsTrue(maxCount.Max < 3);
        }