Search in sources :

Example 1 with RandomlyFails

use of org.netbeans.junit.RandomlyFails 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 RandomlyFails

use of org.netbeans.junit.RandomlyFails 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)

Example 3 with RandomlyFails

use of org.netbeans.junit.RandomlyFails 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 4 with RandomlyFails

use of org.netbeans.junit.RandomlyFails 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)

Example 5 with RandomlyFails

use of org.netbeans.junit.RandomlyFails in project netbeans-rcp-lite by outersky.

the class WeakListenersTest method testStaticRemoveMethod.

// NB-Core-Build #1651
@RandomlyFails
public void testStaticRemoveMethod() throws Exception {
    ChangeListener l = new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
        }
    };
    Singleton.addChangeListener(WeakListeners.change(l, Singleton.class));
    assertEquals(1, Singleton.listeners.size());
    Reference<?> r = new WeakReference<Object>(l);
    l = null;
    assertGC("could collect listener", r);
    assertEquals("called remove method", 0, Singleton.listeners.size());
}
Also used : NodeChangeEvent(java.util.prefs.NodeChangeEvent) PropertyChangeEvent(java.beans.PropertyChangeEvent) PreferenceChangeEvent(java.util.prefs.PreferenceChangeEvent) ChangeEvent(javax.swing.event.ChangeEvent) WeakReference(java.lang.ref.WeakReference) NodeChangeListener(java.util.prefs.NodeChangeListener) VetoableChangeListener(java.beans.VetoableChangeListener) ChangeListener(javax.swing.event.ChangeListener) PreferenceChangeListener(java.util.prefs.PreferenceChangeListener) PropertyChangeListener(java.beans.PropertyChangeListener) RandomlyFails(org.netbeans.junit.RandomlyFails)

Aggregations

RandomlyFails (org.netbeans.junit.RandomlyFails)9 RequestProcessor (org.openide.util.RequestProcessor)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 HashSet (java.util.HashSet)3 CancellationException (java.util.concurrent.CancellationException)3 WeakReference (java.lang.ref.WeakReference)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 VetoableChangeListener (java.beans.VetoableChangeListener)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 NodeChangeEvent (java.util.prefs.NodeChangeEvent)1 NodeChangeListener (java.util.prefs.NodeChangeListener)1 PreferenceChangeEvent (java.util.prefs.PreferenceChangeEvent)1 PreferenceChangeListener (java.util.prefs.PreferenceChangeListener)1 ChangeEvent (javax.swing.event.ChangeEvent)1