* Testing basic functionality
*/
public void testFunctionality() {
System.out.println("***TimeoutTest: testFunctionality ...");
Timeout timeout = new Timeout();
counter = 0;
// Test to remove invalid keys
timeout.removeTimeoutListener(null);
timeout.removeTimeoutListener(new Timestamp(12));
// We have the internal knowledge that the key is the scheduled timeout in millis since 1970
// so we use it here for testing ...
final Timestamp[] keyArr = new Timestamp[4];
class Dummy1 implements I_Timeout {
private String ME = "Dummy1";
public void timeout(Object userData) {
long time = System.currentTimeMillis();
long diff = time - keyArr[counter].getMillis();
if (Math.abs(diff) < 40)
// Allow 40 millis wrong notification (for code execution etc.) ...
log.info("Timeout occurred for " + userData.toString() + " at " + time + " millis, real time failure=" + diff + " millis.");
else {
System.err.println("*****ERROR: Wrong timeout occurred for " + userData.toString() + " at " + time + " millis, scheduled was " + keyArr[counter] + " , real time failure=" + diff + " millis.");
fail("*****ERROR: Wrong timeout occurred for " + userData.toString() + " at " + time + " millis, scheduled was " + keyArr[counter] + " , real time failure=" + diff + " millis.");
}
counter++;
}
}
Dummy1 dummy = new Dummy1();
keyArr[2] = timeout.addTimeoutListener(dummy, 4000L, "timer-4000");
keyArr[3] = timeout.addTimeoutListener(dummy, 2000L, "timer-5500");
try { keyArr[3] = timeout.refreshTimeoutListener(keyArr[3], 5500L); } catch (XmlBlasterException e) { fail("Refresh failed: " + e.getMessage()); }
long diffT = keyArr[3].getMillis() - System.currentTimeMillis();
assertTrue("ERROR: refresh failed", (Math.abs(5500L - diffT) <= 30));
keyArr[0] = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
keyArr[1] = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
long span = timeout.spanToTimeout(keyArr[2]);
assertTrue("*****ERROR: This short span to timeout = " + span + " is probably wrong, or you have a very slow computer.", span >= 3000L);
Timestamp key = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
timeout.removeTimeoutListener(key);
try { key = timeout.refreshTimeoutListener(key, 1500L); } catch (XmlBlasterException e) { log.info("Refresh failed which is OK (it is a test): " + e.getMessage()); }
assertEquals("Should not be expired", false, timeout.isExpired(keyArr[2]));
try { Thread.sleep(7000L); } catch (Exception e) { fail("*****ERROR: main interrupt: " + e.toString()); }
assertEquals("Should be expired", true, timeout.isExpired(keyArr[2]));
System.out.println("***TimeoutTest: testFunctionality [SUCCESS]");
}