use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_ConstructorS.
/**
* java.util.Timer#Timer(String)
*/
public void test_ConstructorS() throws Exception {
Timer t = null;
try {
// Ensure a task is run
t = new Timer("test_ConstructorSThread");
TimerTestTask testTask = new TimerTestTask();
t.schedule(testTask, 200);
awaitRun(testTask);
t.cancel();
} finally {
if (t != null)
t.cancel();
}
try {
new Timer(null);
fail();
} catch (NullPointerException expected) {
}
}
use of java.util.Timer in project robovm by robovm.
the class TimerTest method test_scheduleLjava_util_TimerTaskJJ.
/**
* java.util.Timer#schedule(java.util.TimerTask, long, long)
*/
public void test_scheduleLjava_util_TimerTaskJJ() throws Exception {
Timer t = null;
try {
// Ensure a Timer throws an IllegalStateException after cancelled
t = new Timer();
TimerTestTask testTask = new TimerTestTask();
t.cancel();
try {
t.schedule(testTask, 100, 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();
testTask = new TimerTestTask();
testTask.cancel();
try {
t.schedule(testTask, 100, 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();
testTask = new TimerTestTask();
try {
t.schedule(testTask, -100, 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();
testTask = new TimerTestTask();
try {
t.schedule(testTask, 100, -100);
fail("Scheduling a task with negative period should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws an IllegalArgumentException if period is
// zero
t = new Timer();
testTask = new TimerTestTask();
try {
t.schedule(testTask, 100, 0);
fail("Scheduling a task with 0 period should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws a NullPointerException if the task is null
t = new Timer();
try {
t.schedule(null, 10, 10);
fail("Scheduling a null task should throw NullPointerException");
} catch (NullPointerException expected) {
}
t.cancel();
// Ensure proper sequence of exceptions
t = new Timer();
try {
t.schedule(null, -10, -10);
fail("Scheduling a null task with negative delays should throw IllegalArgumentException first");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a task is run at least twice
t = new Timer();
testTask = new TimerTestTask();
t.schedule(testTask, 100, 100);
Thread.sleep(400);
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);
// at least 9 times
t.schedule(testTask, 100, 100);
testTask = new TimerTestTask();
testTask.incrementCount(true);
// at least 7 times
t.schedule(testTask, 200, 100);
testTask = new TimerTestTask();
testTask.incrementCount(true);
// at least 4 times
t.schedule(testTask, 300, 200);
testTask = new TimerTestTask();
testTask.incrementCount(true);
// at least 4 times
t.schedule(testTask, 100, 200);
// Allowed more room for error
Thread.sleep(1200);
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 spring-boot by spring-projects.
the class SnakeTimer method startTimer.
public static void startTimer() {
gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer");
gameTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
tick();
} catch (Throwable ex) {
log.error("Caught to prevent timer from shutting down", ex);
}
}
}, TICK_DELAY, TICK_DELAY);
}
use of java.util.Timer in project spring-boot by spring-projects.
the class SnakeTimer method startTimer.
public static void startTimer() {
gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer");
gameTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
tick();
} catch (Throwable ex) {
log.error("Caught to prevent timer from shutting down", ex);
}
}
}, TICK_DELAY, TICK_DELAY);
}
use of java.util.Timer in project socket.io-client-java by socketio.
the class ConnectionTest method reconnectByDefault.
@Test(timeout = TIMEOUT)
public void reconnectByDefault() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = client();
socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.close();
values.offer("done");
}
});
socket.open();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
socket.io().engine.close();
}
}, 500);
values.take();
}
Aggregations