public void TestStrictAndRuined() { int created = 0; int rehabed = 0; int destroyed = 0; Pool<object> op = new StrictPool<object>(1, 1, delegate() { created++; return new object(); }, o => rehabed++, o => destroyed++); Assert.AreEqual(1, op.Count, "strict pools should immediately meet low-water"); Assert.AreEqual(1, created); Assert.IsTrue(op.Available); object allocated = op.Obtain(); Assert.IsNotNull(allocated); Assert.IsFalse(op.Available); Assert.IsNull(op.TryObtain()); object obtained = null; // ensure Ruined works too Thread obtainer = new Thread(new ThreadStart(delegate { obtained = op.Obtain(); })); obtainer.IsBackground = true; obtainer.Start(); Thread.Sleep(200); // ensure obtainer has a chance to start and block Assert.IsNull(obtained); Assert.IsTrue(obtainer.IsAlive); Assert.IsFalse(obtainer.ThreadState == ThreadState.Running); Assert.IsFalse(op.Available); Assert.AreEqual(0, rehabed); Assert.AreEqual(0, destroyed); op.Ruined(allocated); Assert.AreEqual(2, created, "returning a ruined object should create a new one"); Assert.AreEqual(0, rehabed); Assert.AreEqual(1, destroyed); // ensure obtainer has a chance to start and block obtainer.Join(500); // ensure obtainer has a chance to start and block Assert.IsFalse(obtainer.IsAlive, "waiting thread should be dead"); Assert.IsNotNull(obtained); Assert.IsFalse(allocated == obtained, "should not be the same object"); op.Dispose(); Assert.AreEqual(0, rehabed); Assert.AreEqual(2, destroyed); Assert.AreEqual(0, op.Count); }
public void TestBasicsStrict() { List<object> allocated = new List<object>(); int created = 0; int rehabed = 0; int destroyed = 0; Pool<object> op = new StrictPool<object>(2, 3, delegate() { created++; return new object(); }, o => rehabed++, o => destroyed++); Assert.AreEqual(2, op.Count, "strict pools should immediately meet low-water"); Assert.AreEqual(2, created); Assert.IsTrue(op.Available); allocated.Add(op.Obtain()); Assert.IsTrue(op.Available); Assert.AreEqual(2, op.Count); Assert.AreEqual(2, created); allocated.Add(op.Obtain()); Assert.IsTrue(op.Available); Assert.AreEqual(2, op.Count); Assert.AreEqual(2, created); allocated.Add(op.Obtain()); Assert.IsFalse(op.Available); Assert.AreEqual(3, op.Count); Assert.AreEqual(3, created); Assert.IsNull(op.TryObtain()); object obtained = null; Thread obtainer = new Thread(new ThreadStart(delegate { obtained = op.Obtain(); })); obtainer.IsBackground = true; obtainer.Start(); Thread.Sleep(200); // ensure obtainer has a chance to start and block Assert.IsTrue(obtainer.IsAlive); Assert.IsNull(obtained); Assert.IsFalse(obtainer.ThreadState == ThreadState.Running); Assert.AreEqual(3, created, "shouldn't have created a new object"); Assert.IsFalse(op.Available); op.Return(allocated[0]); allocated.RemoveAt(0); Assert.AreEqual(3, created, "shouldn't have created a new object"); Assert.AreEqual(1, rehabed); obtainer.Join(500); // ensure obtainer has a chance to start and block Assert.IsFalse(obtainer.IsAlive); Assert.IsNotNull(obtained); ; Assert.AreEqual(3, created, "shouldn't have created a new object"); op.Dispose(); Assert.AreEqual(3, destroyed); Assert.AreEqual(0, op.Out); Assert.AreEqual(0, op.Count); }