public void TakeDestroyReleaseWorkAsRemove() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 10; i++) { testInst.Add(i, new PoolOperations(), true); } for (int i = 0; i < 10; i++) { var item = testInst.Take(); item.MarkElementDestroyed(); testInst.Release(item); Assert.IsTrue(item.IsRemoved); Assert.IsTrue(item.IsRemoved); Assert.AreEqual(10 - i - 1, testInst.AvailableCount); Assert.AreEqual(10 - i - 1, testInst.Count); } } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
public void TestRescanWorks() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 10; i++) { testInst.Add(i, new PoolOperations(), true); } testInst.ProcessAllElements(o => o.MarkElementDestroyed()); testInst.RescanContainer(); Assert.AreEqual(0, testInst.Count); Assert.AreEqual(0, testInst.AvailableCount); } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
public void TestRemoveOnTake() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 10; i++) { testInst.Add(i, new PoolOperations(), true); } testInst.ProcessAllElements(o => o.MarkElementDestroyed()); PoolElementWrapper <int> item; bool takeResult = testInst.TryTake(out item, 0, new CancellationToken()); Assert.IsFalse(takeResult); Assert.AreEqual(0, testInst.Count); } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
/// <summary> /// Pool clean-up (core method) /// </summary> /// <param name="waitForRelease">Should it wait for full completion</param> private void DisposePool(bool waitForRelease) { if (!_disposeCancellation.IsCancellationRequested) { #if DEBUG _elementsContainer.ProcessAllElements(o => o.SetPoolDisposed()); #endif _disposeCancellation.Cancel(); try { } finally { int count = _elementsContainer.Count; while (TakeDestroyAndRemoveElement()) { Debug.Assert(--count >= 0); } if (_elementsContainer.Count == 0) { _stoppedEvent.Set(); } } Profiling.Profiler.ObjectPoolDisposed(this.Name, false); } else { if (_elementsContainer.Count == 0) { _stoppedEvent.Set(); } } if (waitForRelease) { this.WaitUntilStop(); } }
public void ComplexTest() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 1; i++) { testInst.Add(i, new PoolOperations(), true); } TestContext.WriteLine("ComplexTest started"); RunComplexTest(testInst, Environment.ProcessorCount, 100000, 10); TestContext.WriteLine("ComplexTest phase 1 finished"); for (int i = testInst.Count; i < Environment.ProcessorCount; i++) { testInst.Add(i, new PoolOperations(), true); } RunComplexTest(testInst, Environment.ProcessorCount, 1000000, 10); TestContext.WriteLine("ComplexTest phase 2 finished"); for (int i = testInst.Count; i < 2 * Environment.ProcessorCount; i++) { testInst.Add(i, new PoolOperations(), true); } RunComplexTest(testInst, Environment.ProcessorCount, 1000000, 10); TestContext.WriteLine("ComplexTest phase 3 finished"); } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
public void TestAddElementAndNotMakeAvailable() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { Assert.AreEqual(0, testInst.Count); Assert.AreEqual(0, testInst.AvailableCount); var wrapper = testInst.Add(100, new PoolOperations(), false); Assert.IsNotNull(wrapper); Assert.IsTrue(wrapper.IsBusy); Assert.AreEqual(100, wrapper.Element); Assert.AreEqual(1, testInst.Count); Assert.AreEqual(0, testInst.AvailableCount); } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
public void TestTakeUntilEmpty() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 10; i++) { testInst.Add(i, new PoolOperations(), true); } List <PoolElementWrapper <int> > takenElems = new List <PoolElementWrapper <int> >(); PoolElementWrapper <int> item; for (int i = 0; i < 10; i++) { bool takeRes = testInst.TryTake(out item, 0, new CancellationToken()); takenElems.Add(item); Assert.IsTrue(takeRes); Assert.IsNotNull(item); Assert.IsTrue(item.IsBusy); } Assert.AreEqual(0, testInst.AvailableCount); bool takeResO = testInst.TryTake(out item, 0, new CancellationToken()); Assert.IsFalse(takeResO); for (int i = 0; i < takenElems.Count; i++) { testInst.Release(takenElems[i]); } } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }
public void TestSimpleTakeRelease() { SimpleElementsContainer <int> testInst = new SimpleElementsContainer <int>(); try { for (int i = 0; i < 10; i++) { testInst.Add(i, new PoolOperations(), true); } Assert.AreEqual(10, testInst.Count); Assert.AreEqual(10, testInst.AvailableCount); var item = testInst.Take(); Assert.IsNotNull(item); Assert.IsTrue(item.Element >= 0 && item.Element < 10); Assert.IsTrue(item.IsBusy); Assert.IsFalse(item.IsElementDestroyed); Assert.AreEqual(10, testInst.Count); Assert.AreEqual(9, testInst.AvailableCount); testInst.Release(item); Assert.IsFalse(item.IsBusy); Assert.AreEqual(10, testInst.Count); Assert.AreEqual(10, testInst.AvailableCount); } finally { testInst.ProcessAllElements(o => o.MarkElementDestroyed()); } }