public void TestSimpleProcessWork() { using (StaticThreadPool testInst = new StaticThreadPool(Environment.ProcessorCount, 100, "name")) { Assert.AreEqual(Environment.ProcessorCount, testInst.ThreadCount); Assert.AreEqual(100, testInst.QueueCapacity); Assert.IsTrue(testInst.IsWork); int expectedWork = 100000; int executedWork = 0; for (int i = 0; i < expectedWork; i++) { testInst.Run(() => { Interlocked.Increment(ref executedWork); }); } testInst.Dispose(true, true, true); Assert.AreEqual(0, testInst.ThreadCount); Assert.IsFalse(testInst.IsWork); Assert.AreEqual(expectedWork, executedWork); testInst.CompleteAdding(); testInst.Dispose(); } }
public void TestTaskSchedulerWork() { using (StaticThreadPool testInst = new StaticThreadPool(4, -1, "name", false, new StaticThreadPoolOptions() { UseOwnTaskScheduler = true, UseOwnSyncContext = true })) { bool firstTaskInPool = false; bool secondTaskInPool = false; var task = testInst.RunAsTask(() => { firstTaskInPool = testInst.IsThreadPoolThread; }).ContinueWith(t => { secondTaskInPool = testInst.IsThreadPoolThread; }, testInst.TaskScheduler); task.Wait(); Assert.IsTrue(firstTaskInPool); Assert.IsTrue(secondTaskInPool); testInst.Dispose(); } }
public void WaitUntilStopWorkCorrect() { using (StaticThreadPool testInst = new StaticThreadPool(Environment.ProcessorCount, -1, "name")) { Assert.AreEqual(-1, testInst.QueueCapacity); int expectedWork = 100; int executedWork = 0; for (int i = 0; i < expectedWork; i++) { testInst.Run(() => { Thread.Sleep(200); Interlocked.Increment(ref executedWork); }); } testInst.Dispose(false, true, false); Assert.IsTrue(executedWork < expectedWork); Assert.IsTrue(testInst.State == ThreadPoolState.StopRequested); testInst.WaitUntilStop(); Assert.IsTrue(testInst.State == ThreadPoolState.Stopped); Assert.AreEqual(expectedWork, executedWork); } }
public void TestQueueCapacityBoundExtends() { using (StaticThreadPool testInst = new StaticThreadPool(Environment.ProcessorCount, 10, "name")) { int expectedWork = 25; int executedWork = 0; ManualResetEventSlim tracker = new ManualResetEventSlim(); for (int i = 0; i < expectedWork; i++) { testInst.Run(() => { tracker.Wait(); Interlocked.Increment(ref executedWork); }); } tracker.Set(); testInst.Dispose(true, true, true); Assert.AreEqual(expectedWork, executedWork); } }
public void TestQueueCapacityBound() { using (StaticThreadPool testInst = new StaticThreadPool(Environment.ProcessorCount, 10, "name")) { Assert.AreEqual(10, testInst.QueueCapacity); int tryRunWorkItem = 100 * testInst.QueueCapacity; int runWorkCount = 0; int executedWork = 0; for (int i = 0; i < tryRunWorkItem; i++) { if (testInst.TryRun(() => { Thread.Sleep(500); Interlocked.Increment(ref executedWork); })) { runWorkCount++; } } testInst.Dispose(true, true, true); Assert.IsTrue(runWorkCount > 0); Assert.IsTrue(runWorkCount < tryRunWorkItem); Assert.AreEqual(runWorkCount, executedWork); } }
public void TestNotAddAfterDispose() { using (StaticThreadPool testInst = new StaticThreadPool(Environment.ProcessorCount, -1, "name")) { Assert.IsTrue(testInst.TryRun(() => { })); testInst.Dispose(); Assert.IsFalse(testInst.TryRun(() => { })); testInst.Run(() => { }); } }
public void StaticThreadPoolChangeThreadCount() { using (StaticThreadPool testInst = new StaticThreadPool(0, -1, "name")) { int expectedWork = 100; int executedWork = 0; for (int i = 0; i < expectedWork; i++) { if (i == 30) { testInst.AddThreads(10); } else if (i == 50) { testInst.RemoveThreads(5); } else if (i == 80) { testInst.SetThreadCount(2); } testInst.Run(() => { Thread.Sleep(200); Interlocked.Increment(ref executedWork); }); Thread.Sleep(10); } SpinWait.SpinUntil(() => executedWork == expectedWork); Assert.AreEqual(2, testInst.ThreadCount); testInst.Dispose(true, true, true); Assert.AreEqual(0, testInst.ThreadCount); Assert.IsFalse(testInst.IsWork); Assert.AreEqual(expectedWork, executedWork); } }