Search in sources :

Example 1 with RequestProcessor

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

the class RequestProcessor180386Test method testInvokeAny.

// NB-Core-Build #4165: res==null
@RandomlyFails
public void testInvokeAny() throws Exception {
    int count = 20;
    final RequestProcessor rp = new RequestProcessor("TestRP", count + 1);
    class C implements Callable<String> {

        private final String result;

        C(String result) {
            this.result = result;
        }

        @Override
        public String call() throws Exception {
            if (Thread.interrupted()) {
                return null;
            }
            return result;
        }
    }
    List<C> l = new ArrayList<C>(count);
    Set<String> names = new HashSet<String>(count);
    for (int i = 0; i < count; i++) {
        String name = "R" + i;
        names.add(name);
        C c = new C(name);
        l.add(c);
    }
    String res = rp.invokeAny(l);
    assertNotNull(res);
    assertTrue(res.startsWith("R"));
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RequestProcessor(org.openide.util.RequestProcessor) Callable(java.util.concurrent.Callable) HashSet(java.util.HashSet) RandomlyFails(org.netbeans.junit.RandomlyFails)

Example 2 with RequestProcessor

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

the class RequestProcessor180386Test method testInvokeAllCancellation.

public void testInvokeAllCancellation() throws Exception {
    int count = 20;
    final CountDownLatch waitAll = new CountDownLatch(count);
    final RequestProcessor rp = new RequestProcessor("TestRP", count);
    class C implements Callable<String>, Cancellable {

        private final String result;

        volatile boolean cancelCalled;

        C(String result) {
            this.result = result;
        }

        @Override
        public String call() throws Exception {
            waitAll.countDown();
            return cancelCalled ? null : result;
        }

        @Override
        public boolean cancel() {
            cancelCalled = true;
            return false;
        }
    }
    List<C> l = new ArrayList<C>(count);
    List<Future<String>> fs;
    Set<String> names = new HashSet<String>(count);
    for (int i = 0; i < count; i++) {
        String name = "R" + i;
        names.add(name);
        C c = new C(name);
        l.add(c);
    }
    fs = rp.invokeAll(l);
    assertNotNull(fs);
    Set<String> s = new HashSet<String>(count);
    for (Future<String> f : fs) {
        s.add(f.get());
    }
    assertEquals(names, s);
}
Also used : Cancellable(org.openide.util.Cancellable) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) RequestProcessor(org.openide.util.RequestProcessor) HashSet(java.util.HashSet)

Example 3 with RequestProcessor

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

the class RequestProcessor180386Test method testInvokeAllWithTimeout.

public void testInvokeAllWithTimeout() throws Exception {
    int count = 20;
    final CountDownLatch blocker = new CountDownLatch(1);
    final RequestProcessor rp = new RequestProcessor("TestRP", count);
    try {
        class C implements Callable<String> {

            volatile boolean iAmSpecial;

            private final String result;

            volatile boolean ran;

            C(String result) {
                this.result = result;
            }

            @Override
            public String call() throws Exception {
                // will be cancelled
                if (!iAmSpecial) {
                    blocker.await();
                }
                ran = true;
                return result;
            }
        }
        List<C> callables = new ArrayList<C>(count);
        C special = new C("Special");
        special.iAmSpecial = true;
        callables.add(special);
        List<Future<String>> fs;
        Set<String> names = new HashSet<String>(count);
        for (int i = 0; i < count; i++) {
            String name = "R" + i;
            names.add(name);
            C c = new C(name);
            callables.add(c);
        }
        fs = rp.invokeAll(callables, 1000, TimeUnit.MILLISECONDS);
        assertNotNull(fs);
        for (Future<String> f : fs) {
            assertTrue(f.isDone());
        }
        for (C c : callables) {
            if (c == special) {
                assertTrue(c.ran);
            } else {
                assertFalse(c.ran);
            }
        }
    } finally {
        rp.stop();
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) RequestProcessor(org.openide.util.RequestProcessor) HashSet(java.util.HashSet)

Example 4 with RequestProcessor

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

the class RequestProcessor180386Test method testScheduleFixedRateOnMultiThreadPoolDoesNotCauseConcurrentExecution.

public void testScheduleFixedRateOnMultiThreadPoolDoesNotCauseConcurrentExecution() throws Exception {
    final AtomicInteger val = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(10);
    class C implements Runnable {

        boolean failed;

        @Override
        public void run() {
            try {
                int now = val.incrementAndGet();
                if (now > 1) {
                    failed = true;
                    fail(now + " threads simultaneously in run()");
                }
                try {
                    // Intentionally sleep *longer* than the interval
                    // between executions.  We *want* to pile up all of the
                    // RP threads entering run() - synchronization should
                    // serialize them.  This test is to prove that this
                    // method will never be called concurrently from two threads
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            } finally {
                val.decrementAndGet();
                latch.countDown();
            }
        }
    }
    C c = new C();
    long initialDelay = 2000;
    long period = 10;
    RequestProcessor rp = new RequestProcessor("testScheduleFixedRateOnMultiThreadPoolDoesNotCauseConcurrentExecution", 10, true);
    rp.scheduleAtFixedRate(c, initialDelay, period, TimeUnit.MILLISECONDS);
    latch.await();
    assertFalse(c.failed);
    rp.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) RequestProcessor(org.openide.util.RequestProcessor)

Example 5 with RequestProcessor

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

the class RequestProcessor180386Test method testScheduleFixedRateAreRoughlyCorrect.

@RandomlyFails
public void testScheduleFixedRateAreRoughlyCorrect() throws Exception {
    if (!TaskTest.canWait1s()) {
        LOG.warning("Skipping testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll, as the computer is not able to wait 1s!");
        return;
    }
    int runCount = 5;
    final CountDownLatch latch = new CountDownLatch(runCount);
    final List<Long> intervals = Collections.synchronizedList(new ArrayList<Long>(runCount));
    // long initialDelay = 30000;
    // long period = 20000;
    // long fudgeFactor = 4000;
    long initialDelay = 3000;
    long period = 2000;
    long fudgeFactor = 400;
    long expectedInitialDelay = initialDelay - fudgeFactor;
    long expectedPeriod = period - fudgeFactor;
    class C implements Runnable {

        volatile long start = System.currentTimeMillis();

        private int runCount;

        @Override
        public void run() {
            runCount++;
            try {
                synchronized (this) {
                    long end = System.currentTimeMillis();
                    intervals.add(end - start);
                    start = end;
                }
            } finally {
                latch.countDown();
            }
        }
    }
    C c = new C();
    RequestProcessor rp = new RequestProcessor("testScheduleFixedRateAreRoughlyCorrect", 5, true);
    try {
        Future<?> f = rp.scheduleAtFixedRate(c, initialDelay, period, TimeUnit.MILLISECONDS);
        latch.await();
        f.cancel(true);
        StringBuilder failures = new StringBuilder();
        failures.append("Expected at least ").append(expectedInitialDelay).append(" milliseconds before run:\n");
        boolean fail = false;
        for (int i = 0; i < Math.min(runCount, intervals.size()); i++) {
            long expect = i == 0 ? expectedInitialDelay : expectedPeriod;
            failures.append("Round ").append(i).append(" expected delay ").append(expect).append(" but was ").append(intervals.get(i)).append("\n");
            if (intervals.get(i) < expect) {
                fail = true;
            }
        }
        if (fail) {
            fail(failures.toString());
        }
        // Ensure we have really exited
        try {
            f.get();
            fail("CancellationException should have been thrown");
        } catch (CancellationException e) {
        }
        assertTrue(f.isCancelled());
        assertTrue(f.isDone());
    } finally {
        rp.stop();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) 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