@Test public void testSuccess() throws Exception { final long initialTS = System.currentTimeMillis(); IOTimeoutAction action = new IOTimeoutAction(500) { @Override public boolean condition() throws IOException { return System.currentTimeMillis() - initialTS > 300; } }; try { action.execute(); } catch (IOException e) { fail(); } }
void execute() throws IOException { if( timeout > 0 ) { long timeSpentWaiting = 0; long currentTS = System.currentTimeMillis(); while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } long newTS = System.currentTimeMillis(); timeSpentWaiting += newTS - currentTS; currentTS = newTS; if( timeSpentWaiting >= timeout ) { throw new IOException("Timeout after "+timeSpentWaiting+" ms"); } } } else { while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } } } }
IOTimeoutAction { void execute() throws IOException { if( timeout > 0 ) { long timeSpentWaiting = 0; long currentTS = System.currentTimeMillis(); while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } long newTS = System.currentTimeMillis(); timeSpentWaiting += newTS - currentTS; currentTS = newTS; if( timeSpentWaiting >= timeout ) { throw new IOException("Timeout after "+timeSpentWaiting+" ms"); } } } else { while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } } } } }
IOTimeoutAction { void execute() throws IOException { if( timeout > 0 ) { long timeSpentWaiting = 0; long currentTS = System.currentTimeMillis(); while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } long newTS = System.currentTimeMillis(); timeSpentWaiting += newTS - currentTS; currentTS = newTS; if( timeSpentWaiting >= timeout ) { throw new IOException("Timeout after "+timeSpentWaiting+" ms"); } } } else { while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } } } } IOTimeoutAction(long timeout); }
IOTimeoutAction { void execute() throws IOException { if( timeout > 0 ) { long timeSpentWaiting = 0; long currentTS = System.currentTimeMillis(); while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } long newTS = System.currentTimeMillis(); timeSpentWaiting += newTS - currentTS; currentTS = newTS; if( timeSpentWaiting >= timeout ) { throw new IOException("Timeout after "+timeSpentWaiting+" ms"); } } } else { while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } } } } IOTimeoutAction(long timeout); abstract boolean condition(); }
IOTimeoutAction { void execute() throws IOException { if( timeout > 0 ) { long timeSpentWaiting = 0; long currentTS = System.currentTimeMillis(); while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } long newTS = System.currentTimeMillis(); timeSpentWaiting += newTS - currentTS; currentTS = newTS; if( timeSpentWaiting >= timeout ) { throw new IOException("Timeout after "+timeSpentWaiting+" ms"); } } } else { while( !condition() ) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new IOException(e); } } } } IOTimeoutAction(long timeout); abstract boolean condition();  }