Exemplo n.º 1
0
        public void DoNotClaimExternalResourcesIfThePoolIsNotEmpty()
        {
            var pool = new SlowProviderPool(4);

            //this call will claim an external resource
            pool.Get();
            Assert.AreEqual(pool.NewResourceClaims, 1);

            //this one too
            var res2 = pool.Get();

            Assert.AreEqual(pool.NewResourceClaims, 2);

            //now we put a resource back to the pool
            pool.Put(res2);

            //this call should be served with the pooled resource
            var res3 = pool.Get();

            Assert.AreEqual(pool.NewResourceClaims, 2);

            //invalidate a resource and put it back to the pool
            res3.IsValid = false;
            pool.Put(res3);

            //this call will claim an external resource as the one available in the pool is
            //not valid any more
            pool.Get();
            Assert.AreEqual(pool.NewResourceClaims, 3);

            //the pool should be empty now
            Assert.AreEqual(pool.ResourcesInPool, 0);
        }
Exemplo n.º 2
0
        public void DoNotClaimUnlimitedNewResources()
        {
            var pool = new SlowProviderPool(4);

            long count = 0;

            for (var i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    var res = pool.Get();
                    Thread.Sleep(50);
                    pool.Put(res);
                    Interlocked.Increment(ref count);
                });
            }

            while (Interlocked.Read(ref count) < 10)
            {
                Thread.Sleep(10);
            }

            var claimCount = pool.NewResourceClaims;

            //as consumer threads are much faster than the resource provider, most of the connections
            //are recicled ones not new ones
            Assert.IsTrue(claimCount < 100);
        }
Exemplo n.º 3
0
        public void PreloadedResourcePool()
        {
            var pool = new SlowProviderPool(4, 3);

            Assert.AreEqual(pool.NewResourceClaims, 3);

            Assert.AreEqual(pool.ResourcesInPool, 3);

            //three requests should be served by the pool without claiming external resource
            var res1 = pool.Get();
            var res2 = pool.Get();
            var res3 = pool.Get();

            Assert.AreEqual(pool.NewResourceClaims, 3);

            //this one is claimed from outside
            var res4 = pool.Get();

            Assert.AreEqual(pool.NewResourceClaims, 4);

            //this one too
            var res5 = pool.Get();

            Assert.AreEqual(pool.NewResourceClaims, 5);

            //put back 5 resorces ( the first one should be released as capacity is exceeded)
            pool.Put(res1);
            pool.Put(res2);
            pool.Put(res3);
            pool.Put(res4);
            pool.Put(res5);

            Assert.AreEqual(pool.ResourcesInPool, 4);
            //res1 was relesed, the next returned one should be res2
            var res2Again = pool.Get();

            Assert.AreSame(res2, res2Again);
        }