use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testCancelDoesNotInterruptTheRunningThread.
public void testCancelDoesNotInterruptTheRunningThread() throws Exception {
RequestProcessor rp = new RequestProcessor("Not Cancellable", 1, false);
class R implements Runnable {
public boolean checkBefore;
public boolean checkAfter;
public boolean interrupted;
public synchronized void run() {
checkBefore = Thread.interrupted();
notifyAll();
try {
wait();
interrupted = false;
} catch (InterruptedException ex) {
interrupted = true;
}
notifyAll();
try {
wait();
} catch (InterruptedException ex) {
}
checkAfter = Thread.interrupted();
notifyAll();
}
}
R r = new R();
synchronized (r) {
RequestProcessor.Task t = rp.post(r);
r.wait();
assertTrue("The task is already running", !t.cancel());
r.notifyAll();
r.wait();
r.notifyAll();
r.wait();
assertFalse("The task has not been interrupted", r.interrupted);
assertTrue("Not before", !r.checkBefore);
assertTrue("Not after - as the notification was thru InterruptedException", !r.checkAfter);
}
// interrupt after the task has finished
r = new R();
synchronized (r) {
RequestProcessor.Task t = rp.post(r);
r.wait();
r.notifyAll();
r.wait();
assertTrue("The task is already running", !t.cancel());
r.notifyAll();
r.wait();
assertTrue("The task has not been interrupted by exception", !r.interrupted);
assertFalse("Not interupted before", r.checkBefore);
assertFalse("Not interupted after", r.checkAfter);
}
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testTheCancelOfFinishedTask.
public void testTheCancelOfFinishedTask() {
Counter x = new Counter();
RequestProcessor rp = new RequestProcessor("testTheCancelOfFinishedTask");
final RequestProcessor.Task task = rp.post(x);
task.waitFinished();
assertTrue("Finished", task.isFinished());
assertFalse("Too late to cancel", task.cancel());
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testPriorityInversionOnFinishedTasks.
public void testPriorityInversionOnFinishedTasks() throws Exception {
RequestProcessor rp = new RequestProcessor(getName());
class R extends Handler implements Runnable {
RequestProcessor.Task waitFor;
boolean msgOk;
public R(int i) {
}
public void run() {
if (waitFor != null) {
waitFor.waitFinished();
}
}
@Override
public void publish(LogRecord record) {
if (record.getMessage().contains("not running it synchronously")) {
msgOk = true;
waitFor.schedule(100);
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}
R snd = new R(2);
snd.waitFor = rp.post(new R(1));
RequestProcessor.logger().addHandler(snd);
Level prev = RequestProcessor.logger().getLevel();
RequestProcessor.logger().setLevel(Level.FINEST);
try {
snd.waitFor.waitFinished();
assertTrue("Finished", snd.waitFor.isFinished());
RequestProcessor.Task task = rp.post(snd);
task.waitFinished();
assertTrue("Finished as well", task.isFinished());
assertTrue("Message arrived", snd.msgOk);
} finally {
RequestProcessor.logger().setLevel(prev);
RequestProcessor.logger().removeHandler(snd);
}
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testStackTraceFillingDisabled.
public void testStackTraceFillingDisabled() throws InterruptedException {
boolean ea = false;
assert (ea = true);
assertTrue("Test must be run with enabled assertions", ea);
Logger l = RequestProcessor.logger();
TestHandler handler = new TestHandler();
l.addHandler(handler);
try {
RequestProcessor rp = new RequestProcessor("test rp #1", 1);
Task t = rp.post(new Runnable() {
public void run() {
throw new RuntimeException("Testing filled stacktrace");
}
});
// t.waitFinished(); // does not work, thread gets notified before the exception is logged
int timeout = 0;
while (!handler.exceptionCaught && timeout++ < 100) {
Thread.sleep(50);
}
assertTrue("Waiting for task timed out", timeout < 100);
assertTrue("Our testing method not found in stack trace", handler.stFilled);
handler.clear();
timeout = 0;
rp = new RequestProcessor("test rp #2", 1, false, false);
t = rp.post(new Runnable() {
public void run() {
throw new RuntimeException("Testing 'short' stacktrace");
}
});
while (!handler.exceptionCaught && timeout++ < 100) {
Thread.sleep(50);
}
assertTrue("Waiting for task timed out", timeout < 100);
assertFalse("Our testing method found in stack trace", handler.stFilled);
} finally {
l.removeHandler(handler);
}
}
use of org.openide.util.RequestProcessor 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 + "'");
}
}
Aggregations