@Override public ExpensiveTestObject create() { return new ExpensiveTestObject(maxIdleMillis, nrUsesToFailAfter, minOperationMillis, maxOperationMillis); }
@Override public void dispose(final ExpensiveTestObject object) { try { object.close(); } catch (ClosedChannelException ex) { // connection is already closed. } catch (IOException ex) { LOG.warn("Cannot dispose object {}", this, ex); } }
@Override public void close() throws IOException { doStuff(); }
@SuppressFBWarnings("STT_TOSTRING_STORED_IN_FIELD") public ExpensiveTestObject(final long maxIdleMillis, final int nrUsesToFailAfter, final long minOperationMillis, final long maxOperationMillis) { this.maxIdleMillis = maxIdleMillis; this.nrUsesToFailAfter = nrUsesToFailAfter; this.minOperationMillis = minOperationMillis; this.maxOperationMillis = maxOperationMillis; lastTouchedTimeMillis = System.currentTimeMillis(); nrUses = 0; simulateDoStuff(maxOperationMillis - minOperationMillis); id = "Test Object " + OBJ_COUNT.getAndIncrement(); }
@Override public boolean validate(final ExpensiveTestObject object, final Exception e) throws IOException { if (e instanceof IOException) { return false; } else { object.testObject(); return true; } }
@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); } }
public void testObject() throws IOException { LOG.debug("Testing object {}", id); long currentTime = System.currentTimeMillis(); if (currentTime - lastTouchedTimeMillis > maxIdleMillis) { throw new IOException("Connection closed " + id); } if (nrUses > nrUsesToFailAfter) { throw new IOExceptionImpl("Simulated random crap " + id); } simulateDoStuff(0); nrUses++; lastTouchedTimeMillis = System.currentTimeMillis(); }
public void doStuff() throws IOException { if (failAll) { throw new IOExceptionImpl("Failall " + id); } long currentTime = System.currentTimeMillis(); if (currentTime - lastTouchedTimeMillis > maxIdleMillis) { throw new ClosedChannelException(); } if (nrUses > nrUsesToFailAfter) { throw new IOExceptionImpl("Simulated random crap " + id); } simulateDoStuff(maxOperationMillis - minOperationMillis); nrUses++; lastTouchedTimeMillis = System.currentTimeMillis(); }
@Override @SuppressFBWarnings("BED_BOGUS_EXCEPTION_DECLARATION") public Integer call() throws IOException, InterruptedException, TimeoutException { Template.doOnSupplied((final ExpensiveTestObject object, final long deadline) -> { object.doStuff(); return null; }, 1, TimeUnit.MINUTES, pool, RetryPolicy.defaultPolicy(), IOException.class); return this.testNr; }