Search in sources :

Example 16 with RequestProcessor

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

the class RequestProcessorTest method testCancelInterruptsTheRunningThread.

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

        private String name;

        public boolean checkBefore;

        public boolean checkAfter;

        public boolean interrupted;

        public R(String n) {
            this.name = n;
        }

        public synchronized void run() {
            checkBefore = Thread.interrupted();
            log.info("in runnable " + name + " check before: " + checkBefore);
            notifyAll();
            log.info("in runnable " + name + " after notify");
            try {
                wait();
                log.info("in runnable " + name + " after wait, not interrupted");
                interrupted = false;
            } catch (InterruptedException ex) {
                interrupted = true;
                log.info("in runnable " + name + " after wait, interrupted");
            }
            notifyAll();
            log.info("in runnable " + name + " after notifyAll");
            try {
                wait();
                log.info("in runnable " + name + " after second wait, not interrupted");
                checkAfter = Thread.interrupted();
            } catch (InterruptedException ex) {
                log.info("in runnable " + name + " after second wait, interrupted");
                checkAfter = true;
            }
            log.info("in runnable " + name + " checkAfter: " + checkAfter);
            notifyAll();
        }
    }
    R r = new R("First");
    RequestProcessor.Task t;
    synchronized (r) {
        t = rp.post(r);
        r.wait();
        assertTrue("The task is already running", !t.cancel());
        log.info("Main checkpoint1");
        r.wait();
        log.info("Main checkpoint2");
        r.notifyAll();
        log.info("Main checkpoint3");
        r.wait();
        log.info("Main checkpoint4");
        assertTrue("The task has been interrupted", r.interrupted);
        assertTrue("Not before", !r.checkBefore);
        assertTrue("Not after - as the notification was thru InterruptedException", !r.checkAfter);
    }
    log.info("Main checkpoint5");
    t.waitFinished();
    log.info("Main checkpoint6");
    /*
        try {
            assertGC("no", new java.lang.ref.WeakReference(this));
        } catch (Error e) {
            // ok
        }
         */
    // interrupt after the task has finished
    r = new R("Second");
    synchronized (r) {
        t = rp.post(r);
        log.info("Second checkpoint1");
        r.wait();
        r.notifyAll();
        log.info("Second checkpoint2");
        r.wait();
        log.info("Second checkpoint3");
        assertTrue("The task is already running", !t.cancel());
        log.info("Second checkpoint4");
        r.notifyAll();
        log.info("Second checkpoint5");
        r.wait();
        assertTrue("The task has not been interrupted by exception", !r.interrupted);
        assertTrue("Not interupted before", !r.checkBefore);
        assertTrue("But interupted after", r.checkAfter);
    }
    log.info("Second checkpoint6");
    t.waitFinished();
    log.info("Second checkpoint7");
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 17 with RequestProcessor

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

the class RequestProcessorTest method testUseAsInCND.

public void testUseAsInCND() throws Exception {
    final RequestProcessor processor = new RequestProcessor("testUseAsInCND");
    final AtomicReference<String> threadName = new AtomicReference<String>();
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            threadName.set(Thread.currentThread().getName());
        }
    };
    final String taskName = "MyTask";
    final RequestProcessor.Task rpTask = processor.create(new Runnable() {

        @Override
        public void run() {
            String oldName = Thread.currentThread().getName();
            Thread.currentThread().setName(taskName);
            try {
                task.run();
            } finally {
                Thread.currentThread().setName(oldName);
            }
        }
    });
    processor.post(rpTask);
    rpTask.waitFinished();
    assertEquals("Executed and named OK", taskName, threadName.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestProcessor(org.openide.util.RequestProcessor)

Example 18 with RequestProcessor

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

the class RequestProcessorTest method testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon.

// NB-Core-Build #1211
@RandomlyFails
public void testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon() throws Exception {
    log.info("starting testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon");
    RequestProcessor rp = new RequestProcessor("testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon", 1, true);
    log.info("rp created: " + rp);
    class Fail implements Runnable {

        public Fail(String n) {
            name = n;
        }

        private String name;

        public RequestProcessor.Task wait;

        public Object lock;

        public boolean checkBefore;

        public boolean checkAfter;

        public volatile boolean alreadyCanceled;

        public void run() {
            synchronized (this) {
                checkBefore = Thread.interrupted();
                log.info(name + " checkBefore: " + checkBefore);
                notifyAll();
            }
            if (lock != null) {
                synchronized (lock) {
                    lock.notify();
                }
            }
            if (wait != null) {
                // we cannot call Thread.sleep, so lets slow things own
                // in other way
                log(name + " do waitFinished");
                wait.waitFinished();
                log(name + " waitFinished in task is over");
                log.info(name + " slowing by using System.gc");
                while (!alreadyCanceled) {
                    System.gc();
                }
                log.info(name + " ended slowing");
            }
            synchronized (this) {
                checkAfter = Thread.interrupted();
                log.info(name + " checkAfter: " + checkAfter);
                notifyAll();
            }
        }
    }
    Object initLock = new Object();
    Fail smaller = new Fail("smaller");
    Fail bigger = new Fail("bigger");
    RequestProcessor.Task smallerTask, biggerTask;
    smallerTask = rp.create(smaller);
    biggerTask = rp.create(bigger);
    log.info("tasks created. small: " + smallerTask + " big: " + biggerTask);
    bigger.lock = initLock;
    bigger.wait = smallerTask;
    synchronized (initLock) {
        log.info("Do schedule");
        biggerTask.schedule(0);
        initLock.wait();
        log.info("do cancel");
        assertFalse("Already running", biggerTask.cancel());
        bigger.alreadyCanceled = true;
        log.info("cancel done");
    }
    biggerTask.waitFinished();
    log.info("waitFinished is over");
    assertFalse("bigger not interrupted at begining", bigger.checkBefore);
    assertFalse("smaller not interrupted at all", smaller.checkBefore);
    assertFalse("smaller not interrupted at all2", smaller.checkAfter);
    assertTrue("bigger interrupted at end", bigger.checkAfter);
}
Also used : Task(org.openide.util.Task) RequestProcessor(org.openide.util.RequestProcessor)

Example 19 with RequestProcessor

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

the class RequestProcessorTest method testWaitFinishedOnNotStartedTaskFromRPThread.

/**
 * Test to check the waiting in request processor.
 */
public void testWaitFinishedOnNotStartedTaskFromRPThread() throws Exception {
    Counter x = new Counter();
    RequestProcessor rp = new RequestProcessor("testWaitFinishedOnNotStartedTaskFromRPThread");
    final RequestProcessor.Task task = rp.post(x, Integer.MAX_VALUE);
    // 
    class WaitTask implements Runnable {

        public boolean finished;

        public synchronized void run() {
            task.waitFinished();
            finished = true;
            notifyAll();
        }

        public synchronized void w(int timeOut) throws Exception {
            if (!finished) {
                wait(timeOut);
            }
        }
    }
    WaitTask wt = new WaitTask();
    rp.post(wt);
    wt.w(0);
    assertTrue("The task.waitFinished has to finish, otherwise the RequestProcessor thread will stay occupied forever", wt.finished);
    x.assertCnt("The task has been executed - wait from RP made it start", 1);
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 20 with RequestProcessor

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

the class RequestProcessorTest method testTheCancelOfNonStartedTask.

public void testTheCancelOfNonStartedTask() {
    Counter x = new Counter();
    RequestProcessor rp = new RequestProcessor("testTheCancelOfNonStartedTask");
    final RequestProcessor.Task task = rp.create(x);
    assertFalse("Not started tasks cannot be cancelled", task.cancel());
    assertFalse("But not finished", task.isFinished());
    assertFalse("Can be cancelled only once", task.cancel());
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

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