Esempio n. 1
0
        public void ResultThreadSafeTest()
        {
            var task = threadPool.Submit(() =>
            {
                manualResetEvent.WaitOne();
                return(1);
            });

            for (var i = 0; i < numberOfThreads; ++i)
            {
                threads[i] = new Thread(() => results.Enqueue(task.Result));
                threads[i].Start();
            }

            manualResetEvent.Set();
            foreach (var thread in threads)
            {
                if (!thread.Join(100))
                {
                    Assert.Fail("Deadlock");
                }
            }

            Assert.AreEqual(numberOfThreads, results.Count);
            foreach (var result in results)
            {
                Assert.AreEqual(1, result);
            }
        }
        public void NumberOfWorkingThreadsTest()
        {
            int expectedNumberOfThreads = Environment.ProcessorCount;
            var manualResetEvent        = new ManualResetEvent(false);

            using var countdownEvent = new CountdownEvent(expectedNumberOfThreads);

            for (var i = 0; i < expectedNumberOfThreads; ++i)
            {
                threadPool.Submit(() =>
                {
                    countdownEvent.Signal();
                    manualResetEvent.WaitOne();
                    return(0);
                });
            }

            var waitingThread = new Thread(() =>
            {
                countdownEvent.Wait();
                manualResetEvent.Set();
            });

            waitingThread.Start();
            if (!waitingThread.Join(10))
            {
                Assert.Fail($"Number of threads in the {nameof(threadPool)} is less than expected");
            }
        }
        public void ThreadCountTest()
        {
            using var countDownEventForTestThread = new CountdownEvent(threadsCount);
            using var countDownEventForTasks      = new CountdownEvent(1);
            int callsCount = 0;

            for (var i = 0; i < threadsCount * 2; i++)
            {
                threadPool.Submit(() =>
                {
                    Interlocked.Increment(ref callsCount);
                    countDownEventForTestThread.Signal();
                    countDownEventForTasks.Wait();
                    return(true);
                });
            }

            countDownEventForTestThread.Wait();
            Assert.AreEqual(threadsCount, callsCount);
        }