Search in sources :

Example 21 with RequestProcessor

use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.

the class RequestProcessorTest method testCancelDoesNotInterruptTheRunningThread.

public void testCancelDoesNotInterruptTheRunningThread() throws Exception {
    RequestProcessor rp = new RequestProcessor("Not Cancellable", 1, false);
    class R implements Runnable {

        public boolean checkBefore;

        public boolean checkAfter;

        public boolean interrupted;

        public synchronized void run() {
            checkBefore = Thread.interrupted();
            notifyAll();
            try {
                wait();
                interrupted = false;
            } catch (InterruptedException ex) {
                interrupted = true;
            }
            notifyAll();
            try {
                wait();
            } catch (InterruptedException ex) {
            }
            checkAfter = Thread.interrupted();
            notifyAll();
        }
    }
    R r = new R();
    synchronized (r) {
        RequestProcessor.Task t = rp.post(r);
        r.wait();
        assertTrue("The task is already running", !t.cancel());
        r.notifyAll();
        r.wait();
        r.notifyAll();
        r.wait();
        assertFalse("The task has not been interrupted", r.interrupted);
        assertTrue("Not before", !r.checkBefore);
        assertTrue("Not after - as the notification was thru InterruptedException", !r.checkAfter);
    }
    // interrupt after the task has finished
    r = new R();
    synchronized (r) {
        RequestProcessor.Task t = rp.post(r);
        r.wait();
        r.notifyAll();
        r.wait();
        assertTrue("The task is already running", !t.cancel());
        r.notifyAll();
        r.wait();
        assertTrue("The task has not been interrupted by exception", !r.interrupted);
        assertFalse("Not interupted before", r.checkBefore);
        assertFalse("Not interupted after", r.checkAfter);
    }
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 22 with RequestProcessor

use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.

the class RequestProcessorTest method testTheCancelOfFinishedTask.

public void testTheCancelOfFinishedTask() {
    Counter x = new Counter();
    RequestProcessor rp = new RequestProcessor("testTheCancelOfFinishedTask");
    final RequestProcessor.Task task = rp.post(x);
    task.waitFinished();
    assertTrue("Finished", task.isFinished());
    assertFalse("Too late to cancel", task.cancel());
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 23 with RequestProcessor

use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.

the class RequestProcessorTest method testPriorityInversionOnFinishedTasks.

public void testPriorityInversionOnFinishedTasks() throws Exception {
    RequestProcessor rp = new RequestProcessor(getName());
    class R extends Handler implements Runnable {

        RequestProcessor.Task waitFor;

        boolean msgOk;

        public R(int i) {
        }

        public void run() {
            if (waitFor != null) {
                waitFor.waitFinished();
            }
        }

        @Override
        public void publish(LogRecord record) {
            if (record.getMessage().contains("not running it synchronously")) {
                msgOk = true;
                waitFor.schedule(100);
            }
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    }
    R snd = new R(2);
    snd.waitFor = rp.post(new R(1));
    RequestProcessor.logger().addHandler(snd);
    Level prev = RequestProcessor.logger().getLevel();
    RequestProcessor.logger().setLevel(Level.FINEST);
    try {
        snd.waitFor.waitFinished();
        assertTrue("Finished", snd.waitFor.isFinished());
        RequestProcessor.Task task = rp.post(snd);
        task.waitFinished();
        assertTrue("Finished as well", task.isFinished());
        assertTrue("Message arrived", snd.msgOk);
    } finally {
        RequestProcessor.logger().setLevel(prev);
        RequestProcessor.logger().removeHandler(snd);
    }
}
Also used : Task(org.openide.util.Task) LogRecord(java.util.logging.LogRecord) Handler(java.util.logging.Handler) Level(java.util.logging.Level) RequestProcessor(org.openide.util.RequestProcessor)

Example 24 with RequestProcessor

use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.

the class RequestProcessorTest method testStackTraceFillingDisabled.

public void testStackTraceFillingDisabled() throws InterruptedException {
    boolean ea = false;
    assert (ea = true);
    assertTrue("Test must be run with enabled assertions", ea);
    Logger l = RequestProcessor.logger();
    TestHandler handler = new TestHandler();
    l.addHandler(handler);
    try {
        RequestProcessor rp = new RequestProcessor("test rp #1", 1);
        Task t = rp.post(new Runnable() {

            public void run() {
                throw new RuntimeException("Testing filled stacktrace");
            }
        });
        // t.waitFinished(); // does not work, thread gets notified before the exception is logged
        int timeout = 0;
        while (!handler.exceptionCaught && timeout++ < 100) {
            Thread.sleep(50);
        }
        assertTrue("Waiting for task timed out", timeout < 100);
        assertTrue("Our testing method not found in stack trace", handler.stFilled);
        handler.clear();
        timeout = 0;
        rp = new RequestProcessor("test rp #2", 1, false, false);
        t = rp.post(new Runnable() {

            public void run() {
                throw new RuntimeException("Testing 'short' stacktrace");
            }
        });
        while (!handler.exceptionCaught && timeout++ < 100) {
            Thread.sleep(50);
        }
        assertTrue("Waiting for task timed out", timeout < 100);
        assertFalse("Our testing method found in stack trace", handler.stFilled);
    } finally {
        l.removeHandler(handler);
    }
}
Also used : Task(org.openide.util.Task) Logger(java.util.logging.Logger) RequestProcessor(org.openide.util.RequestProcessor)

Example 25 with RequestProcessor

use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.

the class RequestProcessorParallelTest method testParallelExecutionOnDefaultRequestProcessorReported.

// NB-Core-Build #8322: There shall be a warning about parallel execution(len=0): ''
@RandomlyFails
public void testParallelExecutionOnDefaultRequestProcessorReported() {
    final RequestProcessor rp = RequestProcessor.getDefault();
    Para p = new Para(rp, 3);
    CharSequence log = Log.enable("org.openide.util.RequestProcessor", Level.WARNING);
    rp.post(p).waitFinished();
    if (log.length() == 0) {
        fail("There shall be a warning about parallel execution(len=" + log.length() + "):\n'" + log + "'");
    }
}
Also used : RequestProcessor(org.openide.util.RequestProcessor) RandomlyFails(org.netbeans.junit.RandomlyFails)

Aggregations

RequestProcessor (org.openide.util.RequestProcessor)56 CountDownLatch (java.util.concurrent.CountDownLatch)20 Task (org.openide.util.Task)13 HashSet (java.util.HashSet)12 ArrayList (java.util.ArrayList)8 Callable (java.util.concurrent.Callable)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 RandomlyFails (org.netbeans.junit.RandomlyFails)7 Future (java.util.concurrent.Future)6 ScheduledFuture (java.util.concurrent.ScheduledFuture)6 CancellationException (java.util.concurrent.CancellationException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Logger (java.util.logging.Logger)2 TaskListener (org.openide.util.TaskListener)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 File (java.io.File)1 Enumeration (java.util.Enumeration)1 LinkedList (java.util.LinkedList)1