Search in sources :

Example 66 with Timer

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();
        }
    }
}
Also used : Timer(java.util.Timer)

Example 67 with Timer

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();
    }
}
Also used : Timer(java.util.Timer)

Example 68 with Timer

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();
    }
}
Also used : Timer(java.util.Timer) Date(java.util.Date)

Example 69 with Timer

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());
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 70 with Timer

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) {
    }
}
Also used : Timer(java.util.Timer)

Aggregations

Timer (java.util.Timer)448 TimerTask (java.util.TimerTask)262 IOException (java.io.IOException)33 Date (java.util.Date)22 Test (org.junit.Test)15 File (java.io.File)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 JobExecutor (org.activiti.engine.impl.jobexecutor.JobExecutor)9 Intent (android.content.Intent)8 Location (android.location.Location)8 LocationListener (android.location.LocationListener)8 Bundle (android.os.Bundle)8 ActivitiException (org.activiti.engine.ActivitiException)8 View (android.view.View)7 InputMethodManager (android.view.inputmethod.InputMethodManager)7 ImageView (android.widget.ImageView)7 BufferedReader (java.io.BufferedReader)7 InputStreamReader (java.io.InputStreamReader)7