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"));
}
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);
}
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();
}
}
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();
}
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();
}
}
Aggregations