@Override public boolean existAny (K[] keys, long timeout) { long now = System.currentTimeMillis(); long end = now + timeout; while ((now = System.currentTimeMillis()) < end) { if (existAny (keys)) return true; synchronized (this) { try { wait (end - now); } catch (InterruptedException e) { } } } return false; }
@Test public void testExist() { sp.out("KEYA", Boolean.TRUE); sp.out("KEYB", Boolean.TRUE); assertTrue("existAny ([KEYA])", sp.existAny(new String[] { "KEYA" })); assertTrue("existAny ([KEYB])", sp.existAny(new String[] { "KEYB" })); assertTrue("existAny ([KEYA,KEYB])", sp.existAny(new String[] { "KEYA", "KEYB" })); assertFalse("existAny ([KEYC,KEYD])", sp.existAny(new String[] { "KEYC", "KEYD" })); }
@Test public void testExistWithTimeout() { assertFalse("existAnyWithTimeout ([KA,KB])", sp.existAny(new String[] { "KA", "KB" })); assertFalse("existAnyWithTimeout ([KA,KB], delay)", sp.existAny(new String[] { "KA", "KB" }, 1000L)); new Thread() { public void run() { ISOUtil.sleep(1000L); sp.out("KA", Boolean.TRUE); } }.start(); long now = System.currentTimeMillis(); assertTrue("existAnyWithTimeout ([KA,KB], delay)", sp.existAny(new String[] { "KA", "KB" }, 2000L)); long elapsed = System.currentTimeMillis() - now; assertTrue("delay was > 1000", elapsed > 900L); }