use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_purge.
/**
* java.util.Timer#purge()
*/
public void test_purge() throws Exception {
Timer t = null;
try {
t = new Timer();
assertEquals(0, t.purge());
TimerTestTask[] tasks = new TimerTestTask[100];
int[] delayTime = { 50, 80, 20, 70, 40, 10, 90, 30, 60 };
int j = 0;
for (int i = 0; i < 100; i++) {
tasks[i] = new TimerTestTask();
t.schedule(tasks[i], delayTime[j++], 200);
if (j == 9) {
j = 0;
}
}
for (int i = 0; i < 50; i++) {
tasks[i].cancel();
}
assertTrue(t.purge() <= 50);
assertEquals(0, t.purge());
} finally {
if (t != null) {
t.cancel();
}
}
}
use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_Constructor.
/**
* java.util.Timer#Timer()
*/
public void test_Constructor() throws Exception {
Timer t = null;
try {
// Ensure a task is run
t = new Timer();
TimerTestTask testTask = new TimerTestTask();
t.schedule(testTask, 200);
awaitRun(testTask);
t.cancel();
} finally {
if (t != null)
t.cancel();
}
}
use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_scheduleLjava_util_TimerTaskLjava_util_DateJ.
/**
* java.util.Timer#schedule(java.util.TimerTask, java.util.Date,
* long)
*/
public void test_scheduleLjava_util_TimerTaskLjava_util_DateJ() throws Exception {
Timer t = null;
try {
// Ensure a Timer throws an IllegalStateException after cancelled
t = new Timer();
TimerTestTask testTask = new TimerTestTask();
Date d = new Date(System.currentTimeMillis() + 100);
t.cancel();
try {
t.schedule(testTask, d, 100);
fail("Scheduling a task after Timer.cancel() should throw exception");
} catch (IllegalStateException expected) {
}
// Ensure a Timer throws an IllegalStateException if task already
// cancelled
t = new Timer();
d = new Date(System.currentTimeMillis() + 100);
testTask = new TimerTestTask();
testTask.cancel();
try {
t.schedule(testTask, d, 100);
fail("Scheduling a task after cancelling it should throw exception");
} catch (IllegalStateException expected) {
}
t.cancel();
// Ensure a Timer throws an IllegalArgumentException if delay is
// negative
t = new Timer();
d = new Date(-100);
testTask = new TimerTestTask();
try {
t.schedule(testTask, d, 100);
fail("Scheduling a task with negative delay should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws an IllegalArgumentException if period is
// negative
t = new Timer();
d = new Date(System.currentTimeMillis() + 100);
testTask = new TimerTestTask();
try {
t.schedule(testTask, d, -100);
fail("Scheduling a task with negative period should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws a NullPointerException if the task is null
t = new Timer();
d = new Date(System.currentTimeMillis() + 100);
try {
t.schedule(null, d, 10);
fail("Scheduling a null task should throw NullPointerException");
} catch (NullPointerException expected) {
}
t.cancel();
// Ensure a Timer throws a NullPointerException if the date is null
t = new Timer();
testTask = new TimerTestTask();
try {
t.schedule(testTask, null, 10);
fail("Scheduling a null task should throw NullPointerException");
} catch (NullPointerException expected) {
}
t.cancel();
// Ensure proper sequence of exceptions
t = new Timer();
d = new Date(-100);
try {
t.schedule(null, d, 10);
fail("Scheduling a null task with negative dates should throw IllegalArgumentException first");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a task is run at least twice
t = new Timer();
d = new Date(System.currentTimeMillis() + 100);
testTask = new TimerTestTask();
t.schedule(testTask, d, 100);
Thread.sleep(800);
assertTrue("TimerTask.run() method should have been called at least twice (" + testTask.wasRun() + ")", testTask.wasRun() >= 2);
t.cancel();
// Ensure multiple tasks are run
t = new Timer();
testTask = new TimerTestTask();
testTask.incrementCount(true);
d = new Date(System.currentTimeMillis() + 100);
// at least 9 times
t.schedule(testTask, d, 100);
testTask = new TimerTestTask();
testTask.incrementCount(true);
d = new Date(System.currentTimeMillis() + 200);
// at least 7 times
t.schedule(testTask, d, 100);
testTask = new TimerTestTask();
testTask.incrementCount(true);
d = new Date(System.currentTimeMillis() + 300);
// at least 4 times
t.schedule(testTask, d, 200);
testTask = new TimerTestTask();
testTask.incrementCount(true);
d = new Date(System.currentTimeMillis() + 100);
// at least 4 times
t.schedule(testTask, d, 200);
Thread.sleep(3000);
assertTrue("Multiple tasks should have incremented counter 24 times not " + timerCounter, timerCounter >= 24);
t.cancel();
} finally {
if (t != null)
t.cancel();
}
}
use of java.util.Timer in project robovm by robovm.
the class TimerTest method testThrowingTaskKillsTimerThread.
/**
* We used to swallow RuntimeExceptions thrown by tasks. Instead, we need to
* let those exceptions bubble up, where they will both notify the thread's
* uncaught exception handler and terminate the timer's thread.
*/
public void testThrowingTaskKillsTimerThread() throws Exception {
final AtomicReference<Thread> threadRef = new AtomicReference<Thread>();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
}
});
threadRef.set(Thread.currentThread());
throw new RuntimeException("task failure!");
}
}, 1);
Thread.sleep(400);
Thread timerThread = threadRef.get();
assertFalse(timerThread.isAlive());
}
use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_ConstructorSZ.
/**
* java.util.Timer#Timer(String, boolean)
*/
public void test_ConstructorSZ() throws Exception {
Timer t = null;
try {
// Ensure a task is run
t = new Timer("test_ConstructorSZThread", true);
TimerTestTask testTask = new TimerTestTask();
t.schedule(testTask, 200);
awaitRun(testTask);
t.cancel();
} finally {
if (t != null)
t.cancel();
}
try {
new Timer(null, true);
fail();
} catch (NullPointerException expected) {
}
try {
new Timer(null, false);
fail();
} catch (NullPointerException expected) {
}
}
Aggregations