ObjectPool { public void recycle(T object) { factory.recycle(object); synchronized (pool) { if (pool.size() < poolSize && !pool.contains(object)) { pool.add(object); } } } ObjectPool(ObjectFactory<T> factory, int poolSize); T getObject(); void recycle(T object);  }
@Test public void testPoolDoesntOverFill() throws Exception { ObjectFactory<MirageRequest> factory = Mockito.mock(ObjectFactory.class); ObjectPool<MirageRequest> pool = new ObjectPool<>(factory, 3); pool.recycle(new MirageRequest()); pool.recycle(new MirageRequest()); pool.recycle(new MirageRequest()); pool.recycle(new MirageRequest()); pool.recycle(new MirageRequest()); Assert.assertEquals(3, getPoolSize(pool)); }