public void OverstepRemainTimeWrapper()
        {
            PoolWrapper <object> pool = new PoolWrapper <object>(() => { return(new object()); },
                                                                 (obj, phase) => { return(true); },
                                                                 new PoolConfig {
                MaxRemainTime = 3
            });

            var item0 = pool.Borrow();

            Thread.Sleep(1000); //使用一秒
            pool.Return(item0);

            Thread.Sleep(1000);//在池中停留一秒
            var item1 = pool.Borrow();

            Assert.AreEqual(item1, item0);
            pool.Return(item1);

            //在池中停留两秒
            Thread.Sleep(2000);
            var item2 = pool.Borrow();

            Assert.AreEqual(item2, item1);
            pool.Return(item2);

            //在池中停留4秒
            Thread.Sleep(4000);
            var item3 = pool.Borrow();

            Assert.AreNotEqual(item3, item2); //超时,所以新建,并销毁原有的项
            pool.Return(item3);
        }
Beispiel #2
0
        private static TimeoutMonitor CreateMonitor(Guid eventId)
        {
            TimeoutMonitor monitor = _pool.Borrow();

            _monitors.TryAdd(eventId, monitor);
            return(monitor);
        }
Beispiel #3
0
        public static CallIdentity BorrowIdentity()
        {
            var identity = _pool.Borrow();

            _borrowedIdentities.TryAdd(identity.RequestId, identity);
            return(identity);
        }
        public void TenItemByWrapper()
        {
            List <object>        array = new List <object>();
            PoolWrapper <object> pool  = new PoolWrapper <object>(() => { return(new object()); },
                                                                  (obj, phase) => { return(true); },
                                                                  new PoolConfig {
                LoanCapacity = 10
            });                                                                        //最多有10个可以借出
            int maxBorrowedCount = 0;
            int maxWaiterCount   = 0;

            //18个异步任务,访问池
            Parallel.For(0, 100, (i) =>
            {
                var obj = pool.Borrow();
                Thread.Sleep(5);
                lock (this) //List<object>不是线程安全的
                {
                    array.Add(obj);
                }
                maxBorrowedCount = pool.BorrowedCount > maxBorrowedCount ? pool.BorrowedCount : maxBorrowedCount;
                maxWaiterCount   = pool.WaiterCount > maxWaiterCount ? pool.WaiterCount : maxWaiterCount;
                pool.Return(obj);
            });
            Assert.AreEqual(100, array.Count);     //执行了100次操作
            Assert.IsTrue(maxBorrowedCount <= 10); //最多同时进行10个操作
            Assert.IsTrue(maxWaiterCount < 90);    //最多有90个操作等待池
        }
Beispiel #5
0
        public static ScheduledAction Borrow(IAggregateRoot target, IPersistRepository repository, ScheduledActionType type)
        {
            var action = _pool.Borrow();

            action.Target     = target;
            action.Repository = repository;
            action.Type       = type;
            action.Expired    = false;
            return(action);
        }
Beispiel #6
0
        /// <summary>
        /// 打开共生器
        /// </summary>
        public static Symbiosis Open()
        {
            Symbiosis sym;

            if (ExistsCurrent())
            {
                sym = Current;
            }
            else
            {
                sym     = _pool.Borrow();
                Current = sym;
            }
            sym.OpenTimes++;
            return(sym);
        }