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);
        }
        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个操作等待池
        }
 /// <summary>
 /// 结束等待
 /// </summary>
 /// <param name="eventId"></param>
 public static void End(EventKey key)
 {
     if (_monitors.TryRemove(key.EventId, out var monitor))
     {
         monitor.TryComplete();
         _pool.Return(monitor);
     }
 }
Beispiel #4
0
 /// <summary>
 /// 结束等待
 /// </summary>
 /// <param name="eventId"></param>
 public static bool End(EventKey key)
 {
     if (_monitors.TryRemove(key.EventId, out var monitor))
     {
         monitor.TryComplete();
         _pool.Return(monitor);
         return(true);
     }
     return(false);
 }
Beispiel #5
0
        /// <summary>
        /// 关闭当前共生器
        /// </summary>
        public static void Close()
        {
            Symbiosis sym = Current;

            sym.OpenTimes--;
            if (sym.OpenTimes == 0)
            {
                _pool.Return(sym);
                Current = null;
            }
        }
Beispiel #6
0
 public static void Return(ScheduledAction action)
 {
     _pool.Return(action);
 }
Beispiel #7
0
 public static void ReturnIdentity(CallIdentity identity)
 {
     _borrowedIdentities.TryRemove(identity.RequestId, out identity);
     _pool.Return(identity);
 }