public void OneThreadScenario() { const int iterations = 100; const int initialCount = 5; var pool = new ThirdPartyPool(initialCount, 50); var item = pool.TakeSlot(); pool.Release(item); Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount, pool.CurrentCount); Assert.Throws <ArgumentException>(() => pool.Release(new ThirdPartyPool(1, 1).TakeSlot())); Assert.Throws <InvalidOperationException>(() => pool.Release(item)); for (var i = 0; i < iterations; i++) { using (var slot = pool.TakeSlot()) { Assert.IsFalse(slot.Object.Flag); slot.Object.Flag = true; Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount - 1, pool.CurrentCount); } } Assert.AreEqual(initialCount, pool.TotalCount); Assert.AreEqual(initialCount, pool.CurrentCount); }
public void PoolFlushTimout() { var pool = new ThirdPartyPool(0, 2); pool.FlushTimeOut = 1; var insance1 = pool.TakeInstance(); var insance2 = pool.TakeInstance(); // вернули только один pool.Release(insance1); ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush()); var pool2 = new ThirdPartyPool(0, 2); pool.FlushTimeOut = 1; var insance = pool.TakeInstance(); pool.Release(insance1); Action <ThirdParty> longTimeDestroy = obj => { Thread.Sleep(35000); }; // слишком длительная очистка объекта ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush(longTimeDestroy)); }
public void PoolFlushing() { var pool = new ThirdPartyPool(0, 3); var insanceArray = new List <PoolInstance <ThirdParty> >(); insanceArray.Add(pool.TakeInstance()); insanceArray.Add(pool.TakeInstance()); insanceArray.Add(pool.TakeInstance()); Assert.AreEqual(3, pool.TotalCount); insanceArray.ForEach(insance => pool.Release(insance)); pool.TryFlush(); insanceArray.Clear(); Assert.IsTrue(insanceArray.All(inst => inst.Object.Disposed)); Assert.AreEqual(0, pool.TotalCount); // recycle pool and manual flush insanceArray.Add(pool.TakeInstance()); insanceArray.Add(pool.TakeInstance()); Assert.AreEqual(2, pool.TotalCount); insanceArray.ForEach(insance => pool.Release(insance)); Action <ThirdParty> destroyer = obj => { obj.ManualDisposed = true; }; pool.TryFlush(destroyer); Assert.IsTrue(insanceArray.All(inst => inst.Object.ManualDisposed)); Assert.AreEqual(0, pool.TotalCount); }
public void ReduceTotalCount() { var pool = new ThirdPartyPool(100, 100); Assert.IsTrue(pool.TryReduceTotal(10)); Assert.AreEqual(10, pool.TotalCount); var item = pool.TakeSlot(); Assert.IsFalse(pool.TryReduceTotal(0)); Assert.AreEqual(1, pool.TotalCount); }
public void MultiThreadsScenario() { const int iterations = 50; const int threadCount = 50; var pool = new ThirdPartyPool(10, 50); MultiThreadsScenario(threadCount, iterations, pool); Thread.Sleep(100); pool.WaitAll(); Assert.GreaterOrEqual(threadCount, pool.TotalCount); Debug.WriteLine(pool.TotalCount); }
public void Creation() { var pool = new ThirdPartyPool(0, 3); pool.TakeSlot(); pool.TakeSlot(); pool.TakeSlot(); Assert.AreEqual(3, pool.TotalCount); pool = new ThirdPartyPool(100, 100); Assert.AreEqual(100, pool.TotalCount); Assert.AreEqual(100, pool.CurrentCount); }
private static void ManyThreadsWithPool(int iterCount, int taskCount) { var tasks = new Task[taskCount]; var pool = new ThirdPartyPool(1024, 0, 10); for (var t = 0; t < taskCount; t++) { tasks[t] = Task.Factory.StartNew( () => { for (var i = 0; i < iterCount; i++) { using (var slot = pool.TakeSlot()) { } } }); } Task.WaitAll(tasks); }
public void MaxCapacity() { const int capacity0 = 1; const int capacity1 = 25; const int iterations = 100; const int taskCount = 25; var pool0 = new ThirdPartyPool(capacity0, capacity0); var sw = Stopwatch.StartNew(); MultiThreadsScenario(taskCount, iterations, pool0); pool0.WaitAll(); Debug.WriteLine(sw.Elapsed); Assert.AreEqual(capacity0, pool0.TotalCount); var pool1 = new ThirdPartyPool(capacity1, capacity1); sw.Restart(); MultiThreadsScenario(taskCount, iterations, pool1); pool1.WaitAll(); Debug.WriteLine(sw.Elapsed); Assert.AreEqual(capacity1, pool1.TotalCount); }
private static void OneThreadWithPool(int iterCount) { var pool = new ThirdPartyPool(1024, 0, 10); for (var i = 0; i < iterCount; i++) { using (var slot = pool.TakeSlot()) { } } }