Beispiel #1
0
        public void TestPrivateTryParallelDelegate()
        {
            // ReSharper disable once RedundantArgumentDefaultValue
            var boundedParallel = new BoundedParallel(2)
            {
                AbortOnSerialInvocationException = false
            };
            var methodInfo = boundedParallel.GetType().GetMethod("TryParallel",
                                                                 BindingFlags.NonPublic | BindingFlags.Instance, null,
                                                                 new[] { typeof(BoundedParallel.ParallelInvokeDelegate), typeof(int) }, null);

            Assert.IsNotNull(methodInfo);
            var tryParallel = (TryParallelDelegate)Delegate.CreateDelegate(typeof(TryParallelDelegate), boundedParallel, methodInfo);

            Assert.AreEqual(0, boundedParallel.ConcurrentInvocationsCount);
            Assert.AreEqual(0, boundedParallel.ConcurrentThreadsCount);
            var delegateCalled = false;
            var retVal         = tryParallel(count =>
            {
                Assert.AreEqual(1, boundedParallel.ConcurrentInvocationsCount);
                Assert.AreEqual(2, boundedParallel.ConcurrentThreadsCount);
                Parallel.For(0, 1, idx => delegateCalled = true);
            }, 2);

            Assert.IsTrue(retVal);
            Assert.IsTrue(delegateCalled);
            Assert.AreEqual(0, boundedParallel.ConcurrentInvocationsCount);
            Assert.AreEqual(0, boundedParallel.ConcurrentThreadsCount);

            boundedParallel.MaxParallelInvocations = 1;
            var innerDelegateCalled = false;
            var retValOuter         = tryParallel(countOuter =>
            {
                delegateCalled = true;
                retVal         = tryParallel(count =>
                {
                    innerDelegateCalled = true;
                }, 2);
            }, 2);

            Assert.IsTrue(retValOuter);
            Assert.IsFalse(retVal);
            Assert.IsTrue(delegateCalled);
            Assert.IsFalse(innerDelegateCalled);
            Assert.AreEqual(0, boundedParallel.ConcurrentInvocationsCount);
            Assert.AreEqual(0, boundedParallel.ConcurrentThreadsCount);
        }
Beispiel #2
0
        public void TestPrivateGetAllowedThreadCount()
        {
            // ReSharper disable once RedundantArgumentDefaultValue
            var boundedParallel = new BoundedParallel(2)
            {
                AbortOnSerialInvocationException = false
            };
            var methodInfo = boundedParallel.GetType()
                             .GetMethod("GetAllowedThreadCount", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.IsNotNull(methodInfo);
            var getAllowedThreadCount =
                (GetAllowedThreadCountDelegate)Delegate.CreateDelegate(typeof(GetAllowedThreadCountDelegate),
                                                                       boundedParallel, methodInfo);

            boundedParallel.MaxParallelThreads = 3;
            Assert.AreEqual(2, getAllowedThreadCount(4, 2)); // Could fit one thread of the two requested, which makes no sense so returns requestedThreadCount
            Assert.AreEqual(2, getAllowedThreadCount(3, 2)); // Exact fit of the two requests
            Assert.AreEqual(2,
                            getAllowedThreadCount(5, 2));    // Could not fit even 1 thread, returned requestedThreadCount
            boundedParallel.MaxParallelThreads = BoundedParallel.Unlimited;
            Assert.AreEqual(2, getAllowedThreadCount(8, 2)); // Could fit both thread. Using unlimited ThreadLimit
        }