Java Code Examples for org.openide.util.RequestProcessor#stop()

The following examples show how to use org.openide.util.RequestProcessor#stop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testScheduleOneShot() throws Exception {
    RequestProcessor rp = new RequestProcessor ("testScheduleOneShot", 5, true, true);
    try {
        class C implements Callable<String> {
            volatile long start = System.currentTimeMillis();
            private volatile long end;

            @Override
            public String call() throws Exception {
                synchronized(this) {
                    end = System.currentTimeMillis();
                }
                return "Hello";
            }

            synchronized long elapsed() {
                return end - start;
            }
        }
        C c = new C();
        long delay = 5000;
        //Use a 20 second timeout to have a reasonable chance of accuracy
        ScheduledFuture<String> f = rp.schedule(c, delay * 1000, TimeUnit.MICROSECONDS);
        assertEquals (5000, f.getDelay(TimeUnit.MILLISECONDS));
        assertNotNull(f.get());
        //Allow 4 seconds fudge-factor
        assertTrue (c.elapsed() > 4600);
        assertTrue (f.isDone());
    } finally {
        rp.stop();
    }
}
 
Example 2
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 3
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@RandomlyFails
public void testScheduleFixedRateWithShorterIntervalThanRunMethodTimeAreNotDelayed() throws Exception {
    final CountDownLatch latch = new CountDownLatch(10);
    final List<Long> intervals = new CopyOnWriteArrayList<Long>();
    class C implements Runnable {
        long start = Long.MIN_VALUE;

        @Override
        public void run() {
            long end = System.currentTimeMillis();
            if (start != Long.MIN_VALUE) {
                intervals.add(end - start);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                
            }
            start = System.currentTimeMillis();
            latch.countDown();
        }
    }
    C c = new C();
    long initialDelay = 100;
    long period = 100;
    RequestProcessor rp = new RequestProcessor("testScheduleFixedRateWithShorterIntervalThanRunMethodTimeAreNotDelayed", 10, true);
    ScheduledFuture<?> f = rp.scheduleAtFixedRate(c, initialDelay, period, TimeUnit.MILLISECONDS);
    latch.await();
    f.cancel(true);
    rp.stop();
    int max = intervals.size();
    for (int i= 0; i < max; i++) {
        long iv = intervals.get(i);
        assertFalse ("Interval " + i + " should have been at least less than requested interval * 1.5 with fixed rate" + iv, iv > 150);
    }
}
 
Example 4
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails // NB-Core-Build #8322: hung
    public void testScheduleRepeatingIntervalsAreRoughlyCorrect() throws Exception {
        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 ("testScheduleRepeating", 5, true);
        try {
            Future<?> f = rp.scheduleWithFixedDelay(c, initialDelay, period, TimeUnit.MILLISECONDS);
    //        latch.await(initialDelay + fudgeFactor + ((runCount - 1) * (period + fudgeFactor)), TimeUnit.MILLISECONDS); //XXX
            latch.await();
            f.cancel(true);
            for (int i= 0; i < Math.min(runCount, intervals.size()); i++) {
                long expect = i == 0 ? expectedInitialDelay : expectedPeriod;
                assertTrue ("Expected at least " + expect + " milliseconds before run " + i + " but was " + intervals.get(i), intervals.get(i) >= expect);
            }
            //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();
        }
    }
 
Example 5
Source File: RequestProcessor180386Test.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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();
        }
    }