public boolean existAny (Object[] 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 ignored) { } } } return false; } public synchronized void put (K key, V value, long timeout) {
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" }) ); } public void testExistWithTimeout() {
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); assertNotNull ("Entry should not be null", sp.inp ("KA")); } public void testPut () {