Search in sources :

Example 6 with RequestProcessor

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

the class RequestProcessor180386Test method testAwaitTerminationWaitsForNewlyAddedThreads.

@RandomlyFails
public void testAwaitTerminationWaitsForNewlyAddedThreads() throws Exception {
    final RequestProcessor rp = new RequestProcessor("testAwaitTerminationWaitsForNewlyAddedThreads", 50, false);
    int count = 30;
    final CountDownLatch waitLock = new CountDownLatch(1);
    class R implements Runnable {

        boolean done;

        @Override
        public void run() {
            try {
                waitLock.await();
            } catch (InterruptedException ex) {
                done = true;
            } finally {
                done = true;
            }
        }
    }
    Set<R> rs = new HashSet<R>();
    for (int i = 0; i < count; i++) {
        R r = new R();
        rs.add(r);
        rp.submit(r);
    }
    final CountDownLatch shutdownBegun = new CountDownLatch(1);
    Runnable shutdowner = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                rp.shutdown();
                shutdownBegun.countDown();
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    };
    waitLock.countDown();
    new Thread(shutdowner).start();
    assertTrue(rp.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS));
    Thread.sleep(600);
    assertTrue(rp.isTerminated());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RequestProcessor(org.openide.util.RequestProcessor) HashSet(java.util.HashSet) RandomlyFails(org.netbeans.junit.RandomlyFails)

Example 7 with RequestProcessor

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

the class RequestProcessor180386Test method testSubmittedTasksExecutedBeforeShutdown.

public void testSubmittedTasksExecutedBeforeShutdown() throws InterruptedException {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch executedLatch = new CountDownLatch(2);
    Runnable dummyRunnable = new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.await();
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            } finally {
                executedLatch.countDown();
            }
        }
    };
    RequestProcessor rp = new RequestProcessor("X", 1);
    rp.submit(dummyRunnable);
    rp.submit(dummyRunnable);
    rp.shutdown();
    startLatch.countDown();
    assertTrue("Submitted tasks not executed", executedLatch.await(5, TimeUnit.SECONDS));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RequestProcessor(org.openide.util.RequestProcessor)

Example 8 with RequestProcessor

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

the class RequestProcessorHrebejkBugTest method testBug.

public void testBug() throws Exception {
    RequestProcessor rp = new RequestProcessor("TestProcessor", 3, true);
    R1 r1 = new R1();
    R2 r2 = new R2(r1);
    r1.submit(rp);
    r1.in.await();
    RequestProcessor.Task t = rp.post(r2);
    r1.goOn.countDown();
    t.waitFinished();
    if (r1.count != 1) {
        throw r1.wrong;
    }
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 9 with RequestProcessor

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

the class RequestProcessorMemoryTest method testRunOutOfMemory.

public void testRunOutOfMemory() throws Exception {
    RequestProcessor rp = new RequestProcessor("testRunOutOfMemory", 1, false, false);
    final int[] cnt = { 0 };
    RequestProcessor.Task task = rp.create(new Runnable() {

        @Override
        public void run() {
            cnt[0]++;
        }
    });
    for (int i = 0; i < 10000000; i++) {
        task.schedule(2000000);
    }
    assertEquals("Still zero", 0, cnt[0]);
}
Also used : RequestProcessor(org.openide.util.RequestProcessor)

Example 10 with RequestProcessor

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

the class RequestProcessorTest method testInterruptedStatusWorksInInversedTasks.

public void testInterruptedStatusWorksInInversedTasks() throws Exception {
    RequestProcessor rp = new RequestProcessor("testInterruptedStatusWorksInInversedTasks", 1, true);
    class Fail implements Runnable {

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

        private String name;

        public RequestProcessor.Task wait;

        public Object lock;

        public Exception ex;

        public volatile boolean executed;

        public volatile boolean checkBefore;

        public volatile boolean checkAfter;

        @Override
        public void run() {
            synchronized (this) {
                executed = true;
                checkBefore = Thread.interrupted();
                log("checkBefore: " + checkBefore);
                notifyAll();
            }
            if (lock != null) {
                synchronized (lock) {
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException interrex) {
                        this.ex = interrex;
                        interrex.printStackTrace();
                        fail("No InterruptedException");
                    }
                    log.info("wait for lock over");
                }
            }
            if (wait != null) {
                wait.schedule(100);
                wait.waitFinished();
            }
            synchronized (this) {
                checkAfter = Thread.interrupted();
                log.info("checkAfter: " + checkAfter);
                notifyAll();
            }
        }

        @Override
        public String toString() {
            return name;
        }
    }
    Object initLock = new Object();
    Fail smaller = new Fail("smaller");
    smaller.lock = initLock;
    Fail bigger = new Fail("BIGGER");
    RequestProcessor.Task smallerTask, biggerTask;
    smallerTask = rp.create(smaller);
    biggerTask = rp.create(bigger);
    bigger.wait = smallerTask;
    synchronized (initLock) {
        log.info("schedule 0");
        biggerTask.schedule(0);
        initLock.wait();
        initLock.notifyAll();
        log.info("doing cancel");
        assertFalse("Already running", biggerTask.cancel());
        log.info("biggerTask cancelled");
    }
    biggerTask.waitFinished();
    log.info("waitFinished over");
    assertTrue("Bigger executed", bigger.executed);
    assertTrue("Smaller executed", smaller.executed);
    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)

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