Exemplo n.º 1
0
        public void BoundedObjectPoolDoesNotExceedMaxSizeForMultiThreadedCallsWithBlockingOperations()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var loopResult = Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);

                //simulate a blocking operation
                if (i % 1000 == 0)
                {
                    Thread.Sleep(100);
                }

                pool.ReturnObjectToPool(item);
            });

            while (!loopResult.IsCompleted ||
                   pool.PoolCount > 10)
            {
                Thread.Sleep(10);
            }

            Assert.IsTrue(pool.PoolCount <= 10);
        }
Exemplo n.º 2
0
        public void BoundedObjectPoolGetsObjectFromPool()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var item = pool.GetObject();

            Assert.IsNotNull(item);
        }
Exemplo n.º 3
0
        public void BoundedObjectPoolResetsObjectStateWhenReturningObjectToPool()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var item = pool.GetObject();

            pool.ReturnObjectToPool(item);

            Assert.IsTrue(item.StateReset);
        }
Exemplo n.º 4
0
        public void BoundedObjectPoolReturnObjectToPoolDoesNotExceedMaxSize()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var item = pool.GetObject();

            pool.ReturnObjectToPool(item);

            Assert.IsTrue(pool.PoolCount <= 10);
        }
Exemplo n.º 5
0
        public void BoundedObjectPoolGetsObjectFromPoolMultiThreaded()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);
                pool.ReturnObjectToPool(item);
            });

            Assert.IsTrue(values.All(i => i.HasValue));
        }
Exemplo n.º 6
0
        public void BoundedObjectPoolGetsObjectFromPoolMultiThreadedWithBlockingOperations()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);

                //simulate a blocking operation
                if (i % 1000 == 0)
                {
                    Thread.Sleep(100);
                }

                pool.ReturnObjectToPool(item);
            });

            Assert.IsTrue(values.All(i => i.HasValue));
        }