Exemplo n.º 1
0
        public void TestBoundedParallelArgumentsCheck()
        {
            var             boundedParallel     = new BoundedParallel();
            ParallelOptions nullParallelOptions = null;

            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.Invoke(nullParallelOptions, delegate { })).Message.Contains("parallelOptions"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.Invoke(new ParallelOptions(), null)).Message.Contains("actions"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentException>(() => boundedParallel.Invoke(new ParallelOptions(), new Action[] { null })).Message.Contains("null action"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.For(0, 1, nullParallelOptions, delegate { })).Message.Contains("parallelOptions"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.For(0, 1, new ParallelOptions(), null)).Message.Contains("body"));
            IEnumerable <int> nullEnumerable = null;

            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.ForEach(nullEnumerable, new ParallelOptions(), delegate { })).Message.Contains("source"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.ForEach(new List <int>(), null, delegate { })).Message.Contains("parallelOptions"));
            Assert.IsTrue(Assert.ThrowsException <ArgumentNullException>(() => boundedParallel.ForEach(new List <int>(), new ParallelOptions(), null)).Message.Contains("body"));
        }
Exemplo n.º 2
0
        public void TestBoundedParallelForEachForceSerial()
        {
            var items = new[] { 1, 2, 3 };
            var cnt   = 0;
            // ReSharper disable once RedundantArgumentDefaultValue
            var boundedParallel = new BoundedParallel(2);

            Parallel.Invoke(() =>
            {
                boundedParallel.ForEach(items, (item) =>
                {
                    Thread.Sleep(1500);
                    Interlocked.Increment(ref cnt);
                });
            }, () =>
            {
                boundedParallel.ForEach(items, (item) =>
                {
                    Thread.Sleep(1500);
                    Interlocked.Increment(ref cnt);
                });
            }, () =>
            {
                Thread.Sleep(500);
                var result1 = boundedParallel.ForEach(items, (item) =>
                {
                    Thread.Sleep(500);
                    Interlocked.Increment(ref cnt);
                });
                Assert.IsTrue(result1.IsCompleted);
            }, () =>
            {
                Thread.Sleep(500);
                var result2 = boundedParallel.ForEach(items, (item) =>
                {
                    Thread.Sleep(500);
                    Interlocked.Increment(ref cnt);
                });
                Assert.IsTrue(result2.IsCompleted);
            });

            Assert.AreEqual(12, cnt);
            Assert.IsTrue(boundedParallel.Stats.TotalSerialRunCount > 0, "TotalSerialRunCount should be > 0");
        }
Exemplo n.º 3
0
        public void TestBoundedParallelSimpleForEachWithZeroItems()
        {
            var regularParallelResult = Parallel.ForEach(new List <int>(), (n) => { });
            var boundedParallel       = new BoundedParallel(3);
            var boundedResult         = boundedParallel.ForEach(new List <int>(), (n) => { });

            Assert.AreEqual(1, boundedParallel.Stats.TotalSerialRunCount);
            Assert.AreEqual(0, boundedParallel.Stats.TotalParallelRunCount);
            Assert.AreEqual(regularParallelResult.IsCompleted, boundedResult.IsCompleted);
        }
Exemplo n.º 4
0
        public void TestBoundedParallelSimpleForEachCall()
        {
            var items           = new[] { 1, 2, 3 };
            var cnt             = 0;
            var boundedParallel = new BoundedParallel(3);
            var result          = boundedParallel.ForEach(items, (item) => { Interlocked.Increment(ref cnt); });

            Assert.AreEqual(3, cnt);
            Assert.IsTrue(boundedParallel.Stats.TotalSerialRunCount == 0, "TotalSerialRunCount must be zero");
            Assert.IsTrue(result.IsCompleted);
        }