Exemple #1
0
        public void Parallel_Load_On_Pool()
        {
            var policy = new PoolPolicy <ObjectPoolClassesTests.SomePoolObject>
            {
                FunctionOnCreate = () => new ObjectPoolClassesTests.SomePoolObject {
                    Value = "created"
                },
                ActionOnGet     = o => o.Value = "get",
                ActionOnReturn  = o => o.Value = "returned",
                ActionOnDestroy = o => o.Value = "destroyed",
                MaximumPoolSize = 100,
                InitialPoolSize = 1
            };

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = 10
            };
            var pool = new ObjectPoolConcurrent <ObjectPoolClassesTests.SomePoolObject>(policy)
            {
                IsPoolingEnabled = true
            };

            Assert.That(() =>
                        Parallel.For(0L, 1000, options, (i, loopState) =>
            {
                var someObject = pool.Get();
                pool.Return(someObject);
            }), Throws.Nothing);
            Assert.That(pool.CountActive, Is.EqualTo(0));
            Assert.That(pool.CountInactive, Is.GreaterThan(0));
        }
Exemple #2
0
        public void PooledObject_ReinitialiseOnTake_RasisesEvent_OnReinitialiseError()
        {
            var policy = new PoolPolicy <PooledObject <DisposableTestPoolItem> >()
            {
                Factory = (p) => new PooledObject <DisposableTestPoolItem>(p, new DisposableTestPoolItem()),
                InitializationPolicy = PooledItemInitialization.Take,
                MaximumPoolSize      = 1,
                ReinitializeObject   = (item) =>
                {
                    throw new OutOfMemoryException("Test");
                }
            };

            var eventRaised = false;

            using (var eventRaisedSignal = new System.Threading.ManualResetEvent(false))
            {
                var pool = new Pool <PooledObject <DisposableTestPoolItem> >(policy);
                pool.ReinitialiseError += (s, e) => { eventRaised = true; eventRaisedSignal.Set(); };

                var item = pool.Take();
                pool.Add(item);
                item = pool.Take();
                eventRaisedSignal.WaitOne(1000);
                Assert.IsTrue(eventRaised);
            }
        }
Exemple #3
0
        private IPool <DisposableTestPoolItem> GetDisposablePool(int maxSize, PooledItemInitialization reinitialisePolicy)
        {
            var policy = new PoolPolicy <DisposableTestPoolItem>()
            {
                Factory = (pool) => new DisposableTestPoolItem(),
                InitializationPolicy = reinitialisePolicy,
                MaximumPoolSize      = maxSize,
                ReinitializeObject   = (pi) =>
                {
                    pi.Id              = Guid.NewGuid();
                    pi.Date            = DateTime.Now;
                    pi.ResetByThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
                }
            };

            return(new Pool <DisposableTestPoolItem>(policy));
        }
Exemple #4
0
        private static Pool <PooledObject <DisposableTestPoolItem> > GetPoolForPooledObjectWrapper()
        {
            var policy = new PoolPolicy <PooledObject <DisposableTestPoolItem> >()
            {
                Factory = (p) => new PooledObject <DisposableTestPoolItem>(p, new DisposableTestPoolItem()),
                InitializationPolicy = PooledItemInitialization.Return,
                MaximumPoolSize      = 1,
                ReinitializeObject   = (item) =>
                {
                    item.Value.Id              = Guid.NewGuid();
                    item.Value.Date            = DateTime.Now;
                    item.Value.ResetByThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
                }
            };

            var pool = new Pool <PooledObject <DisposableTestPoolItem> >(policy);

            return(pool);
        }
Exemple #5
0
        private static Pool <PooledObject <RecyclableStringWriter> > CreatePoolInstance()
        {
            lock (s_Synchroniser)
            {
                if (s_TextWriterPool != null)
                {
                    return(s_TextWriterPool);
                }

                var policy = new PoolPolicy <PooledObject <RecyclableStringWriter> >()
                {
                    Factory = (pool) => new PooledObject <RecyclableStringWriter>(pool, new RecyclableStringWriter(System.Globalization.CultureInfo.CurrentCulture)),
                    InitializationPolicy = PooledItemInitialization.Return,
                    MaximumPoolSize      = 50,
                    ReinitializeObject   = (poolItem) => poolItem.Value.Dispose()
                };

                return(s_TextWriterPool = new Pool <PooledObject <RecyclableStringWriter> >(policy));
            }
        }