示例#1
0
        public void TestExecuteWithObjectAllItemsTaken()
        {
            var op = new ObjectPool <PoolItem>(
                () => new PoolItem(42, "The Hitchhiker's Guide to the Galaxy"),
                100,
                10);

            op.AcquireAllObjects();
            op.ExecuteWithObject(item => item);
        }
示例#2
0
        public void TestNoAvailableItem()
        {
            var op = new ObjectPool <PoolItem>(
                () => new PoolItem(42, "The Hitchhiker's Guide to the Galaxy"),
                100,
                10);

            op.AcquireAllObjects();

            Assert.AreEqual(0, op.AvailableTokenCount);

            op.AcquireObject();
        }
示例#3
0
        public void TestAcquireAllAndRelease()
        {
            var op = new ObjectPool <PoolItem>(
                () => new PoolItem(42, "The Hitchhiker's Guide to the Galaxy"),
                100,
                10);

            var os = op.AcquireAllObjects().ToList();

            Assert.AreEqual(0, op.AvailableTokenCount);

            Thread.Sleep(new Random().Next(10, 200));
            os.ForEach(o => op.ReleaseObject(o));

            Assert.AreEqual(100, op.AvailableTokenCount);
        }
示例#4
0
        public void TestReleaseObjectWithoutAcquiring()
        {
            var op = new ObjectPool <PoolItem>(
                () => new PoolItem(42, "The Hitchhiker's Guide to the Galaxy"),
                100,
                10);
            InvalidOperationException exception = null;

            try
            {
                op.ReleaseObject(new PoolItem(20, "ERROR ITEM"));
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            var os = op.AcquireAllObjects().ToList();

            Assert.IsNotNull(exception);

            Assert.AreEqual(100, os.Count);
            Assert.AreEqual(0, os.Count(i => i.Number == 20 || i.Text == "ERROR ITEM"));
        }