Ejemplo n.º 1
0
        public void Test_Dispose_4()
        {
            Disposable disposable;
            PoolResourceHolder<Disposable> poolResourceHolder;
            var pool = new Pool<Disposable>(() => new Disposable(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
            pool.Dispose();

            Assert.Throws<ObjectDisposedException>(() => pool.Acquire());
            Assert.Throws<ObjectDisposedException>(() => pool.AcquireHolder());
            Assert.Throws<ObjectDisposedException>(() => pool.TryAcquire(TimeSpan.FromDays(1), out disposable));
            Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolder(TimeSpan.FromDays(1), out poolResourceHolder));
            Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireImmediately(out disposable));
            Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolderImmediately(out poolResourceHolder));

            // Асинхронные методы.
            try
            {
                pool.AcquireAsync().Wait();
                Assert.Fail("Must throw exception.");
            }
            catch (AggregateException error)
            {
                Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
            }
            try
            {
                pool.AcquireHolderAsync().Wait();
                Assert.Fail("Must throw exception.");
            }
            catch (AggregateException error)
            {
                Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
            }
        }
Ejemplo n.º 2
0
 public void Test_HolderWorks()
 {
     var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
     using (pool.AcquireHolder())
     {
         Assert.AreEqual(1, pool.Allocated);
         Assert.AreEqual(0, pool.Available);
     }
     Assert.AreEqual(1, pool.Allocated);
     Assert.AreEqual(1, pool.Available);
 }
Ejemplo n.º 3
0
 public void Test_Speed_Holder()
 {
     var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
     long cycles = 0;
     TimeSpan testTime = TimeSpan.FromSeconds(1);
     var sw = Stopwatch.StartNew();
     while (sw.Elapsed < testTime)
         using (pool.AcquireHolder())
             cycles++;
     Console.Out.WriteLine("Done {0} Acquire-Release cycles in 1 second (using resource holder).", cycles);
 }
Ejemplo n.º 4
0
 public void Test_HolderMultipleDispose()
 {
     var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
     PoolResourceHolder<object> poolResourceHolder = pool.AcquireHolder();
     Assert.AreEqual(1, pool.Allocated);
     Assert.AreEqual(0, pool.Available);
     poolResourceHolder.Dispose();
     poolResourceHolder.Dispose();
     poolResourceHolder.Dispose();
     Assert.AreEqual(1, pool.Allocated);
     Assert.AreEqual(1, pool.Available);
 }