public void TestRentOnDisposedPoolWithoutThrowOnUnavail() { using (TestDynamicPool testInst = new TestDynamicPool(0, 1)) { testInst.Dispose(); using (var item = testInst.Rent(false)) { Assert.IsFalse(item.IsValid); } } }
public void TestRentOnDisposedPoolThrowsException() { using (TestDynamicPool testInst = new TestDynamicPool(0, 1)) { testInst.Dispose(); using (var item = testInst.Rent(true)) { Assert.IsFalse(item.IsValid); } } }
public void ComplexTest2() { using (TestDynamicPool testInst = new TestDynamicPool(20, 100)) { RunComplexTest(testInst, 1, 100000, 4, false); RunComplexTest(testInst, Environment.ProcessorCount, 2000000, 4, false); RunComplexTest(testInst, Environment.ProcessorCount, 1000000, 12, false); testInst.Dispose(); Assert.AreEqual(testInst.ElementsCreated, testInst.ElementsDestroyed, "ElementsCreated != ElementsDestroyed"); } }
public void WaitForElementReleaseNotBlockAfterStop() { using (TestDynamicPool testInst = new TestDynamicPool(0, 10)) { using (var item = testInst.Rent()) { Assert.IsTrue(item.IsValid); } testInst.Dispose(DisposeFlags.WaitForElementsRelease); } }
public void ComplexConcurrentUseWithTimeout() { using (TestDynamicPool testInst = new TestDynamicPool(0, 1)) { RunConcurrentUseWithTimeout(testInst, 2, 2000, 1, 1, 1000, true); RunConcurrentUseWithTimeout(testInst, Environment.ProcessorCount, 2000, 2, 1, 1000, true); RunConcurrentUseWithTimeout(testInst, 2 * Environment.ProcessorCount, 3000, 2, 1, 1000, true); RunConcurrentUseWithTimeout(testInst, 4 * Environment.ProcessorCount, 4000, 2, 1, 1000, true); RunConcurrentUseWithTimeout(testInst, 4 * Environment.ProcessorCount, 2000, 4, 4, 4, true); testInst.Dispose(); Assert.AreEqual(testInst.ElementsCreated, testInst.ElementsDestroyed, "ElementsCreated != ElementsDestroyed"); } }
public void TestDisposeCancelWaiters() { using (TestDynamicPool testInst = new TestDynamicPool(0, 1)) { Task.Delay(400).ContinueWith(t => testInst.Dispose()); using (var item1 = testInst.Rent()) { using (var item2 = testInst.Rent()) { } } } }
public void TestDisposeCancelWaitersWithoutException() { using (TestDynamicPool testInst = new TestDynamicPool(0, 1)) { Task.Delay(400).ContinueWith(t => testInst.Dispose()); using (var item1 = testInst.Rent(false)) { using (var item2 = testInst.Rent(false)) { Assert.IsTrue(item1.IsValid); Assert.IsFalse(item2.IsValid); } } } }
public void WaitForElementReleaseWaits() { using (TestDynamicPool testInst = new TestDynamicPool(0, 10)) { int elementRented = 0; int elementReleased = 0; Task.Run(() => { using (var item = testInst.Rent()) { Interlocked.Increment(ref elementRented); Thread.Sleep(250); } Interlocked.Increment(ref elementReleased); }); SpinWait.SpinUntil(() => Volatile.Read(ref elementRented) == 1); testInst.Dispose(DisposeFlags.WaitForElementsRelease); TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref elementReleased)); } }
private void RunConcurrentUseWithDispose(int maxPoolElemCount, int threadCount, int opCount, bool faultElements = true) { using (TestDynamicPool testInst = new TestDynamicPool(0, maxPoolElemCount)) { Thread[] threads = new Thread[threadCount]; Barrier startBar = new Barrier(threadCount + 1); int totalExecutedOpCount = 0; Action thAct = () => { Random localRand = new Random(Thread.CurrentThread.ManagedThreadId + Environment.TickCount); startBar.SignalAndWait(); try { while (true) { int curSpinTime = localRand.Next(0, 2000); Interlocked.Increment(ref totalExecutedOpCount); using (var el = testInst.Rent(60 * 1000, throwOnUnavail: true)) { if (faultElements && localRand.Next(10) == 0) { el.Element.MakeInvalid(); } if (curSpinTime > 1000) { Thread.Sleep(0); } else { SpinWaitHelper.SpinWait(curSpinTime); } } } } catch (ObjectDisposedException) { } catch (CantRetrieveElementException) { } }; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(new ThreadStart(thAct)); } for (int i = 0; i < threads.Length; i++) { threads[i].Start(); } startBar.SignalAndWait(); while (Volatile.Read(ref totalExecutedOpCount) < opCount) { Thread.Sleep(1); } testInst.Dispose(); for (int i = 0; i < threads.Length; i++) { threads[i].Join(); } Assert.AreEqual(testInst.ElementCount, testInst.FreeElementCount, "testInst.ElementCount != testInst.FreeElementCount"); Assert.AreEqual(testInst.ElementsCreated, testInst.ElementsDestroyed, "ElementsCreated != ElementsDestroyed"); } }