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