예제 #1
0
        public void ShouldCorrectlyAggregateAndCollectExceptions()
        {
            var threadConfig = new ConcurrentTestCaseThreadConfig(16, 16);
            var testCase     = new MockConcurrentTestCase <DummyImmutableRef> {
                ThreadConfig         = threadConfig,
                OnGetExecutionAction = (threadType, threadIndex) => _ => throw new ApplicationException(threadType.ToString() + threadIndex + "/")
            };

            try {
                _runner.ExecuteCustomTestCase(testCase);
                Assert.Fail("No exception was thrown.");
            }
            catch (AggregateException e) {
                Assert.GreaterOrEqual(e.InnerExceptions.Count, threadConfig.TotalThreadCount);

                for (var w = 0; w < threadConfig.WriterThreadCount; ++w)
                {
                    Assert.True(e.InnerExceptions.Count(ex => ex.ToString().Contains(ThreadType.WriterThread.ToString() + w + "/", StringComparison.InvariantCultureIgnoreCase)) == 1);
                }
                for (var r = 0; r < threadConfig.ReaderThreadCount; ++r)
                {
                    Assert.True(e.InnerExceptions.Count(ex => ex.ToString().Contains(ThreadType.ReaderThread.ToString() + r + "/", StringComparison.InvariantCultureIgnoreCase)) == 1);
                }
            }
        }

        #endregion Tests
    }
예제 #2
0
 static IConcurrentTestCase <DummyImmutableRef> CreateNewTestCase(ConcurrentTestCaseThreadConfig threadConfig)
 {
     return(new StandardConcurrentTestCase <DummyImmutableRef>(
                "No-Operation Test Case",
                null,
                null,
                threadConfig
                ));
 }
예제 #3
0
        public void ShouldCreateCorrectNumberOfThreads()
        {
            var writerThreadBag = new ConcurrentBag <Thread>();
            var readerThreadBag = new ConcurrentBag <Thread>();
            var threadConfig    = new ConcurrentTestCaseThreadConfig(8, 16);
            var testCase        = CreateNewTestCase(_ => writerThreadBag.Add(Thread.CurrentThread), _ => readerThreadBag.Add(Thread.CurrentThread), threadConfig);

            _runner.ExecuteCustomTestCase(testCase);

            Assert.AreEqual(threadConfig.WriterThreadCount, writerThreadBag.Distinct().Count());
            Assert.AreEqual(threadConfig.ReaderThreadCount, readerThreadBag.Distinct().Count());
        }
예제 #4
0
        public void ShouldCorrectlySerializeTestCase()
        {
            var setUpThreadBag    = new ConcurrentBag <Thread>();
            var execThreadBag     = new ConcurrentBag <Thread>();
            var tearDownThreadBag = new ConcurrentBag <Thread>();

            var setUpEvent = new ManualResetEvent(false);
            var execEvent  = new ManualResetEvent(false);

            var threadConfig = new ConcurrentTestCaseThreadConfig(16, 0);

            _runner.AllThreadsSetUp = _ => {
                setUpThreadBag.Add(Thread.CurrentThread);
                // Force one thread (#11) to wait for the sake of the test
                if (Thread.CurrentThread.Name?.Contains("11", StringComparison.InvariantCultureIgnoreCase) ?? false)
                {
                    setUpEvent.WaitOne();
                }
            };
            var testCase = CreateNewTestCase(
                _ => {
                execThreadBag.Add(Thread.CurrentThread);
                // Force one thread (#11) to wait for the sake of the test
                if (Thread.CurrentThread.Name?.Contains("11", StringComparison.InvariantCultureIgnoreCase) ?? false)
                {
                    execEvent.WaitOne();
                }
            },
                null,
                threadConfig
                );

            _runner.AllThreadsTearDown = _ => {
                tearDownThreadBag.Add(Thread.CurrentThread);
            };

            var runTask = Task.Run(() => _runner.ExecuteCustomTestCase(testCase));

            SpinWait.SpinUntil(() => setUpThreadBag.Count == threadConfig.TotalThreadCount);
            Thread.Sleep(250);             // Just to make sure we're not getting a false positive by beating other threads to adding themselves to the bag
            Assert.AreEqual(0, execThreadBag.Count);
            setUpEvent.Set();

            SpinWait.SpinUntil(() => execThreadBag.Count == threadConfig.TotalThreadCount);
            Thread.Sleep(250);             // Just to make sure we're not getting a false positive by beating other threads to adding themselves to the bag
            Assert.AreEqual(0, tearDownThreadBag.Count);
            execEvent.Set();

            runTask.Wait();
            Assert.AreEqual(threadConfig.TotalThreadCount, tearDownThreadBag.Count);
        }
예제 #5
0
        public void ShouldInvokeTearDownFunctionsOnRelevantThreads()
        {
            var allThreadsTearDownInvokedThreads    = new ConcurrentBag <Thread>();
            var writerThreadsTearDownInvokedThreads = new ConcurrentBag <Thread>();
            var readerThreadsTearDownInvokedThreads = new ConcurrentBag <Thread>();
            var threadConfig = new ConcurrentTestCaseThreadConfig(8, 16);
            var testCase     = CreateNewTestCase(threadConfig);

            _runner.AllThreadsTearDown   = _ => allThreadsTearDownInvokedThreads.Add(Thread.CurrentThread);
            _runner.WriterThreadTearDown = _ => writerThreadsTearDownInvokedThreads.Add(Thread.CurrentThread);
            _runner.ReaderThreadTearDown = _ => readerThreadsTearDownInvokedThreads.Add(Thread.CurrentThread);
            _runner.ExecuteCustomTestCase(testCase);

            Assert.AreEqual(threadConfig.TotalThreadCount, allThreadsTearDownInvokedThreads.Count);
            Assert.AreEqual(threadConfig.WriterThreadCount, writerThreadsTearDownInvokedThreads.Count);
            Assert.AreEqual(threadConfig.ReaderThreadCount, readerThreadsTearDownInvokedThreads.Count);
        }
예제 #6
0
 static IConcurrentTestCase <DummyImmutableRef> CreateNewTestCase(Action <DummyImmutableRef> writerAction, Action <DummyImmutableRef> readerAction, ConcurrentTestCaseThreadConfig threadConfig)
 {
     return(new StandardConcurrentTestCase <DummyImmutableRef>(
                "Custom Test Case",
                writerAction,
                readerAction,
                threadConfig
                ));
 }