Esempio n. 1
0
        public void TestMultiThreaded()
        {
            int loopTimes   = 3;
            int threadCount = 20;

            var test = new Sec07_ReusableBarrier(threadCount);

            var threads = new List <Thread>();

            for (int i = 0; i < threadCount; i++)
            {
                var thread = new Thread(() => test.RunCode(loopTimes));
                threads.Add(thread);
                thread.Start();
            }

            var expectedStatementsExecuted =
                Enumerable.Repeat(StatementExecuted.Rendezvous, threadCount).Concat(
                    Enumerable.Repeat(StatementExecuted.CriticalPoint, threadCount)).Concat(
                    Enumerable.Repeat(StatementExecuted.Rendezvous, threadCount)).Concat(
                    Enumerable.Repeat(StatementExecuted.CriticalPoint, threadCount)).Concat(
                    Enumerable.Repeat(StatementExecuted.Rendezvous, threadCount)).Concat(
                    Enumerable.Repeat(StatementExecuted.CriticalPoint, threadCount));

            threads.ForEach(t => t.Join());

            CollectionAssert.AreEqual(expectedStatementsExecuted, test.StatementsExecuted);
        }
Esempio n. 2
0
        public void TestSingleThreaded()
        {
            var test = new Sec07_ReusableBarrier(1);

            int loopTimes = 5;

            test.RunCode(loopTimes);

            for (int i = 0; i < loopTimes; i++)
            {
                Assert.That(test.StatementsExecuted[2 * i], Is.EqualTo(StatementExecuted.Rendezvous));
                Assert.That(test.StatementsExecuted[2 * i + 1], Is.EqualTo(StatementExecuted.CriticalPoint));
            }
        }