XpipeNettyClientKeyedObjectPool extends AbstractLifecycle implements TopElement, SimpleKeyedObjectPool<Endpoint, NettyClient> { @Override public NettyClient borrowObject(Endpoint key) throws BorrowObjectException { try { logger.debug("[borrowObject][begin]{}", key); NettyClient value = getOrCreate(key).borrowObject(); logger.debug("[borrowObject][end]{}, {}", key, value); return value; } catch (Exception e) { logger.error("[borrowObject]" + key, e); throw new BorrowObjectException("borrow " + key, e); } } XpipeNettyClientKeyedObjectPool();  XpipeNettyClientKeyedObjectPool(int maxPerKey);  XpipeNettyClientKeyedObjectPool(GenericObjectPoolConfig config);  XpipeNettyClientKeyedObjectPool(NettyKeyedPoolClientFactory pooledObjectFactory); final void setKeyPooConfig(int minIdlePerKey, long softMinEvictableIdleTimeMillis, long minEvictableIdleTimeMillis, long timeBetweenEvictionRunsMillis); @Override SimpleObjectPool<NettyClient> getKeyPool(Endpoint key); @Override NettyClient borrowObject(Endpoint key); @Override void returnObject(Endpoint key, NettyClient value); @Override void clear(); @Override void clear(Endpoint key); @Override int getOrder(); @VisibleForTesting ObjectPool getObjectPool(Endpoint endpoint); static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS; static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; }
@Test public void testDispose() throws Exception{ Endpoint key = new DefaultEndPoint("localhost", echoServer.getPort()); Assert.assertEquals(0, echoServer.getConnected()); for(int i=0; i < maxPerKey; i++){ pool.borrowObject(key); int finalI = i; waitConditionUntilTimeOut(() -> echoServer.getConnected() == finalI +1); } LifecycleHelper.stopIfPossible(pool); LifecycleHelper.disposeIfPossible(pool); waitConditionUntilTimeOut(() -> echoServer.getConnected() == 0); }
@Test public void testMax() throws Exception{ Endpoint key = new DefaultEndPoint("localhost", echoServer.getPort()); Assert.assertEquals(0, echoServer.getConnected()); for(int i=0; i < maxPerKey; i++){ pool.borrowObject(key); sleep(100); Assert.assertEquals(i + 1, echoServer.getConnected()); } try{ pool.borrowObject(key); Assert.fail(); }catch(BorrowObjectException e){ } }