@Override public void run() { for (int i = 0; i < 100000; i++) { try { Buffer b = mm.allocate(random.nextInt(9000) + 1); Buffer b1 = mm.allocate(random.nextInt(33000) + 1); assertNotNull(b); assertNotNull(b1); assertTrue("Buffer=" + b, b.tryDispose()); assertTrue("Buffer=" + b1, b1.tryDispose()); } catch (Throwable t) { errorsSeen.set(true); System.out.println("Failed at iteration: " + i); t.printStackTrace(); errors[thread] = t; break; } } latch.countDown(); } });
@Test public void testIllegalAllocationArgument() throws Exception { PooledMemoryManager mm = new PooledMemoryManager(DEFAULT_BASE_BUFFER_SIZE, 1, 0, 1, DEFAULT_HEAP_USAGE_PERCENTAGE, DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE, isDirect); // allocation request must be greater than zero try { mm.allocate(-1); fail(); } catch (IllegalArgumentException iae) { // expected } catch (Exception e) { fail(); } try { mm.allocateAtLeast(-1); fail(); } catch (IllegalArgumentException iae) { // expected } catch (Exception e) { fail(); } }
Buffer b = mm.allocate(6666); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); assertEquals(2, probe.bufferAllocatedFromPool.get()); b = mm.allocate(1023); assertEquals(0, probe.bufferAllocated.get()); assertEquals(2, probe.bufferReleasedToPool.get());
@Test public void testBufferShrink() throws Exception { PooledMemoryManager mm = new PooledMemoryManager(DEFAULT_BASE_BUFFER_SIZE, 1, 0, 1, DEFAULT_HEAP_USAGE_PERCENTAGE, DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE, isDirect); final TestProbe probe = new TestProbe(); mm.getMonitoringConfig().addProbes(probe); Buffer b = mm.allocate(13000); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); assertEquals(4, probe.bufferAllocatedFromPool.get()); b.position(6666); b.limit(7000); b.shrink(); assertEquals(334, b.remaining()); assertEquals(0, probe.bufferAllocated.get()); assertEquals(3, probe.bufferReleasedToPool.get()); assertEquals(4, probe.bufferAllocatedFromPool.get()); b.tryDispose(); assertEquals(0, probe.bufferAllocated.get()); assertEquals(4, probe.bufferReleasedToPool.get()); assertEquals(4, probe.bufferAllocatedFromPool.get()); }
@Test public void testSimpleAllocationAndDispose() throws Exception { PooledMemoryManager mm = new PooledMemoryManager(DEFAULT_BASE_BUFFER_SIZE, 1, 0, 1, DEFAULT_HEAP_USAGE_PERCENTAGE, DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE, isDirect); final TestProbe probe = new TestProbe(); mm.getMonitoringConfig().addProbes(probe); // allocate a buffer and validate the configuration of said buffer Buffer b = mm.allocate(4096); assertEquals(4096, b.remaining()); assertTrue(!b.isComposite()); assertTrue(b.allowBufferDispose()); assertEquals(DEFAULT_BASE_BUFFER_SIZE, b.capacity()); // validate that pool returned a buffer. assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); assertEquals(1, probe.bufferAllocatedFromPool.get()); // dispose the buffer and validate the pool has returned to it's // original size b.tryDispose(); // validate that buffer was returned to the pool. assertEquals(0, probe.bufferAllocated.get()); assertEquals(1, probe.bufferReleasedToPool.get()); assertEquals(1, probe.bufferAllocatedFromPool.get()); }
mm.allocate(4096); // allocate a single buffer assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); b = (PooledMemoryManager.PoolBuffer) mm.allocate(4096); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); b = (PooledMemoryManager.PoolBuffer) mm.allocate(4096); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); b = (PooledMemoryManager.PoolBuffer) mm.allocate(4096); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get()); b = (PooledMemoryManager.PoolBuffer) mm.allocate(4096); assertEquals(0, probe.bufferAllocated.get()); assertEquals(0, probe.bufferReleasedToPool.get());
Buffer b = mm.allocate(2048); Buffer nb = mm.reallocate(b, 1024); assertEquals(1024, nb.limit()); b = mm.allocate(2048); nb = mm.reallocate(b, 4096); assertEquals(4096, nb.limit()); b = mm.allocate(2048); nb = mm.reallocate(b, 9999); assertEquals(9999, nb.limit()); b = mm.allocate(4096); nb = mm.reallocate(b, 4097); assertEquals(nb.limit(), 4097);
Buffer b = mm.allocate(6000); assertEquals(6000, b.remaining()); assertTrue(b.allowBufferDispose());