/** * Get a session from the pool (or block if none available). */ @Override public Session<F> getSession() { return new CachedSession(this.pool.getItem(), this.sharedSessionEpoch); }
@Override public TcpConnectionSupport obtainConnection() throws Exception { return new CachedConnection(this.pool.getItem(), getListener()); }
@Override public final byte[] deserialize(InputStream inputStream) throws IOException { byte[] buffer = this.pool == null ? new byte[getMaxMessageSize()] : this.pool.getItem(); try { return doDeserialize(inputStream, buffer); } finally { if (this.pool != null) { this.pool.releaseItem(buffer); } } }
@Test(expected = IllegalArgumentException.class) public void testForeignObject() { final Set<String> strings = new HashSet<String>(); final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); pool.getItem(); pool.releaseItem("Hello, world!"); }
@Test public void testReuseAndStale() { final Set<String> strings = new HashSet<String>(); final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); String s1 = pool.getItem(); String s2 = pool.getItem(); assertNotSame(s1, s2); pool.releaseItem(s1); String s3 = pool.getItem(); assertSame(s1, s3); stale.set(true); pool.releaseItem(s3); s3 = pool.getItem(); assertNotSame(s1, s3); assertFalse(strings.remove(s1)); assertEquals(2, pool.getAllocatedCount()); }
@Test public void testDoubleReturn() { final Set<String> strings = new HashSet<String>(); final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); Semaphore permits = TestUtils.getPropertyValue(pool, "permits", Semaphore.class); assertEquals(2, permits.availablePermits()); String s1 = pool.getItem(); assertEquals(1, permits.availablePermits()); pool.releaseItem(s1); assertEquals(2, permits.availablePermits()); pool.releaseItem(s1); assertEquals(2, permits.availablePermits()); }
@Override public final byte[] deserialize(InputStream inputStream) throws IOException { byte[] buffer = this.pool == null ? new byte[this.maxMessageSize] : this.pool.getItem(); try { return doDeserialize(inputStream, buffer); } finally { if (this.pool != null) { this.pool.releaseItem(buffer); } } }
@Override public TcpConnectionSupport obtainConnection() throws Exception { return new CachedConnection(this.pool.getItem(), getListener()); }
final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); String s1 = pool.getItem(); assertEquals(0, pool.getIdleCount()); assertEquals(1, pool.getActiveCount()); assertEquals(0, pool.getActiveCount()); assertEquals(1, pool.getAllocatedCount()); s1 = pool.getItem(); assertEquals(0, pool.getIdleCount()); assertEquals(1, pool.getActiveCount()); assertEquals(1, pool.getAllocatedCount()); String s2 = pool.getItem(); assertNotSame(s1, s2); pool.setWaitTimeout(1); assertEquals(2, pool.getAllocatedCount()); try { pool.getItem(); fail("Expected exception"); assertEquals(2, pool.getActiveCount()); assertEquals(2, pool.getAllocatedCount()); String s3 = pool.getItem(); String s4 = pool.getItem(); assertEquals(0, pool.getIdleCount()); assertEquals(4, pool.getActiveCount());