@Test public void testSize() throws InterruptedException { HeapSizeManager underTest = new HeapSizeManager(10l, 1000); long id = underTest.registerOperationWithHeapSize(5l); assertTrue(underTest.hasInflightRequests()); assertFalse(underTest.isFull()); assertEquals(5l, underTest.getHeapSize()); long id2 = underTest.registerOperationWithHeapSize(4l); assertTrue(underTest.hasInflightRequests()); assertFalse(underTest.isFull()); assertEquals(9l, underTest.getHeapSize()); long id3 = underTest.registerOperationWithHeapSize(1l); assertTrue(underTest.hasInflightRequests()); assertTrue(underTest.isFull()); assertEquals(10l, underTest.getHeapSize()); underTest.markCanBeCompleted(id); assertFalse(underTest.isFull()); assertEquals(5l, underTest.getHeapSize()); underTest.markCanBeCompleted(id2); underTest.markCanBeCompleted(id3); assertFalse(underTest.hasInflightRequests()); }
@Test public void testRpcCount() throws InterruptedException { HeapSizeManager underTest = new HeapSizeManager(100l, 2); assertFalse(underTest.isFull()); assertFalse(underTest.hasInflightRequests()); long id = underTest.registerOperationWithHeapSize(1); assertFalse(underTest.isFull()); assertTrue(underTest.hasInflightRequests()); long id2 = underTest.registerOperationWithHeapSize(1); assertTrue(underTest.hasInflightRequests()); assertTrue(underTest.isFull()); underTest.markCanBeCompleted(id); assertFalse(underTest.isFull()); assertTrue(underTest.hasInflightRequests()); underTest.markCanBeCompleted(id2); assertFalse(underTest.isFull()); assertFalse(underTest.hasInflightRequests()); }
@Test /** * Test to make sure that HeapSizeManager does not register an operation if the HeapSizeManager * size limit was reached. */ public void testSizeLimitReachWaits() throws InterruptedException { ExecutorService pool = Executors.newCachedThreadPool(); try { final HeapSizeManager underTest = new HeapSizeManager(1l, 1); long id = underTest.registerOperationWithHeapSize(5l); assertTrue(underTest.hasInflightRequests()); assertTrue(underTest.isFull()); final AtomicBoolean secondRequestRegistered = new AtomicBoolean(); pool.submit( new Runnable() { @Override public void run() { try { underTest.registerOperationWithHeapSize(5l); secondRequestRegistered.set(true); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); // Give some time for the Runnable to be executed. Thread.sleep(10); assertFalse(secondRequestRegistered.get()); underTest.markCanBeCompleted(id); // Give more time for the Runnable to be executed. Thread.sleep(10); assertTrue(secondRequestRegistered.get()); } finally { pool.shutdownNow(); } }