@Test(timeout = 10000, expected = TimeoutException.class) public void testGetTimeout() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(1, new ExpensiveTestObjectFactory()).build(); LOG.debug("pool = {}", pool); Future<ExpensiveTestObject> fut = DefaultExecutor.instance().submit(() -> pool.get()); fut.get(); LOG.debug("pool = {}", pool); try (ExecutionContext start = ExecutionContexts.start(1, TimeUnit.SECONDS)) { pool.get(); LOG.debug("pool = {}", pool); } }
@Test(timeout = 20000) public void testPoolUseNoFailures() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory(1000000, 1000000, 1, 5)).build(); runTest(pool, 0, 10000); pool.dispose(); }
@Test(timeout = 16000) public void testPoolUseNoFailuresStarvation() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(1, new ExpensiveTestObjectFactory(1000000, 1000000, 1, 5)).build(); runTest(pool, 0, 15000); pool.dispose(); }
@Test @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testBuild2() throws ObjectCreationException, InterruptedException, ObjectBorrowException, ExecutionException, TimeoutException { final RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()) .withMaintenance(DefaultScheduler.INSTANCE, 1L, true) .build(); LOG.debug("pool = {}", pool); final ExpensiveTestObject object = pool.get(); LOG.debug("pool = {}", pool); Future<Void> submit = DefaultExecutor.INSTANCE.submit(() -> { pool.recycle(object, null); return null; }); submit.get(); Thread.sleep(100); final ExpensiveTestObject object2 = pool.get(); LOG.debug("pool = {}", pool); Assert.assertNotNull(object2); }
@Test(expected = IllegalStateException.class) @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testBuildDisposeTimeout() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()).build(); LOG.debug("pool = {}", pool); pool.get(); pool.get(); LOG.debug("pool = {}", pool); try (ExecutionContext start = ExecutionContexts.start(1, TimeUnit.SECONDS)) { pool.dispose(); pool.get(); LOG.debug("pool = {}", pool); } }
@Test @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testBuild3() throws ObjectCreationException, InterruptedException, ObjectBorrowException, ExecutionException, TimeoutException { final RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()) .withMaintenance(DefaultScheduler.INSTANCE, 1L, true) .build(); LOG.debug("pool = {}", pool); final ExpensiveTestObject object = pool.get(); LOG.debug("pool = {}", pool); Future<Void> submit = DefaultExecutor.INSTANCE.submit(() -> { pool.recycle(object, null); return null; }); submit.get(); final ExpensiveTestObject object2 = pool.get(); LOG.debug("pool = {}", pool); Assert.assertSame(object, object2); }
/** * Test of build method, of class RecyclingSupplierBuilder. */ @Test @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") public void testBuild() throws ObjectCreationException, InterruptedException, ObjectBorrowException, TimeoutException, ObjectDisposeException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()).build(); LOG.debug("pool = {}", pool); ExpensiveTestObject object = pool.get(); LOG.debug("pool = {}", pool); pool.recycle(object, null); LOG.debug("pool = {}", pool); ExpensiveTestObject object2 = pool.get(); Assert.assertSame(object2, object); pool.dispose(); }
@Test(timeout = 20000) public void testPoolUseWithMaintenance() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { final RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder<>(10, new ExpensiveTestObjectFactory()) .withMaintenance(DefaultScheduler.INSTANCE, 10, true).build(); runTest(pool, 5, 20000); try { pool.dispose(); } catch (ObjectDisposeException ex) { Throwables.writeTo(ex, System.err, Throwables.PackageDetail.SHORT); } }
@Test(timeout = 20000) public void testPoolUse() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { final RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory()).build(); runTest(pool, 0, 10000); try { ExpensiveTestObject.setFailAll(true); LogAssert expect = TestLoggers.sys().expect("", Level.WARN, LogMatchers.hasFormat("Cannot dispose object {}")); pool.dispose(); expect.assertObservation(); } finally { ExpensiveTestObject.setFailAll(false); } }