use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testCancelInterruptsTheRunningThread.
public void testCancelInterruptsTheRunningThread() throws Exception {
RequestProcessor rp = new RequestProcessor("Cancellable", 1, true);
class R implements Runnable {
private String name;
public boolean checkBefore;
public boolean checkAfter;
public boolean interrupted;
public R(String n) {
this.name = n;
}
public synchronized void run() {
checkBefore = Thread.interrupted();
log.info("in runnable " + name + " check before: " + checkBefore);
notifyAll();
log.info("in runnable " + name + " after notify");
try {
wait();
log.info("in runnable " + name + " after wait, not interrupted");
interrupted = false;
} catch (InterruptedException ex) {
interrupted = true;
log.info("in runnable " + name + " after wait, interrupted");
}
notifyAll();
log.info("in runnable " + name + " after notifyAll");
try {
wait();
log.info("in runnable " + name + " after second wait, not interrupted");
checkAfter = Thread.interrupted();
} catch (InterruptedException ex) {
log.info("in runnable " + name + " after second wait, interrupted");
checkAfter = true;
}
log.info("in runnable " + name + " checkAfter: " + checkAfter);
notifyAll();
}
}
R r = new R("First");
RequestProcessor.Task t;
synchronized (r) {
t = rp.post(r);
r.wait();
assertTrue("The task is already running", !t.cancel());
log.info("Main checkpoint1");
r.wait();
log.info("Main checkpoint2");
r.notifyAll();
log.info("Main checkpoint3");
r.wait();
log.info("Main checkpoint4");
assertTrue("The task has been interrupted", r.interrupted);
assertTrue("Not before", !r.checkBefore);
assertTrue("Not after - as the notification was thru InterruptedException", !r.checkAfter);
}
log.info("Main checkpoint5");
t.waitFinished();
log.info("Main checkpoint6");
/*
try {
assertGC("no", new java.lang.ref.WeakReference(this));
} catch (Error e) {
// ok
}
*/
// interrupt after the task has finished
r = new R("Second");
synchronized (r) {
t = rp.post(r);
log.info("Second checkpoint1");
r.wait();
r.notifyAll();
log.info("Second checkpoint2");
r.wait();
log.info("Second checkpoint3");
assertTrue("The task is already running", !t.cancel());
log.info("Second checkpoint4");
r.notifyAll();
log.info("Second checkpoint5");
r.wait();
assertTrue("The task has not been interrupted by exception", !r.interrupted);
assertTrue("Not interupted before", !r.checkBefore);
assertTrue("But interupted after", r.checkAfter);
}
log.info("Second checkpoint6");
t.waitFinished();
log.info("Second checkpoint7");
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testUseAsInCND.
public void testUseAsInCND() throws Exception {
final RequestProcessor processor = new RequestProcessor("testUseAsInCND");
final AtomicReference<String> threadName = new AtomicReference<String>();
final Runnable task = new Runnable() {
@Override
public void run() {
threadName.set(Thread.currentThread().getName());
}
};
final String taskName = "MyTask";
final RequestProcessor.Task rpTask = processor.create(new Runnable() {
@Override
public void run() {
String oldName = Thread.currentThread().getName();
Thread.currentThread().setName(taskName);
try {
task.run();
} finally {
Thread.currentThread().setName(oldName);
}
}
});
processor.post(rpTask);
rpTask.waitFinished();
assertEquals("Executed and named OK", taskName, threadName.get());
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon.
// NB-Core-Build #1211
@RandomlyFails
public void testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon() throws Exception {
log.info("starting testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon");
RequestProcessor rp = new RequestProcessor("testInterruptedStatusWorksInInversedTasksWhenInterruptedSoon", 1, true);
log.info("rp created: " + rp);
class Fail implements Runnable {
public Fail(String n) {
name = n;
}
private String name;
public RequestProcessor.Task wait;
public Object lock;
public boolean checkBefore;
public boolean checkAfter;
public volatile boolean alreadyCanceled;
public void run() {
synchronized (this) {
checkBefore = Thread.interrupted();
log.info(name + " checkBefore: " + checkBefore);
notifyAll();
}
if (lock != null) {
synchronized (lock) {
lock.notify();
}
}
if (wait != null) {
// we cannot call Thread.sleep, so lets slow things own
// in other way
log(name + " do waitFinished");
wait.waitFinished();
log(name + " waitFinished in task is over");
log.info(name + " slowing by using System.gc");
while (!alreadyCanceled) {
System.gc();
}
log.info(name + " ended slowing");
}
synchronized (this) {
checkAfter = Thread.interrupted();
log.info(name + " checkAfter: " + checkAfter);
notifyAll();
}
}
}
Object initLock = new Object();
Fail smaller = new Fail("smaller");
Fail bigger = new Fail("bigger");
RequestProcessor.Task smallerTask, biggerTask;
smallerTask = rp.create(smaller);
biggerTask = rp.create(bigger);
log.info("tasks created. small: " + smallerTask + " big: " + biggerTask);
bigger.lock = initLock;
bigger.wait = smallerTask;
synchronized (initLock) {
log.info("Do schedule");
biggerTask.schedule(0);
initLock.wait();
log.info("do cancel");
assertFalse("Already running", biggerTask.cancel());
bigger.alreadyCanceled = true;
log.info("cancel done");
}
biggerTask.waitFinished();
log.info("waitFinished is over");
assertFalse("bigger not interrupted at begining", bigger.checkBefore);
assertFalse("smaller not interrupted at all", smaller.checkBefore);
assertFalse("smaller not interrupted at all2", smaller.checkAfter);
assertTrue("bigger interrupted at end", bigger.checkAfter);
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testWaitFinishedOnNotStartedTaskFromRPThread.
/**
* Test to check the waiting in request processor.
*/
public void testWaitFinishedOnNotStartedTaskFromRPThread() throws Exception {
Counter x = new Counter();
RequestProcessor rp = new RequestProcessor("testWaitFinishedOnNotStartedTaskFromRPThread");
final RequestProcessor.Task task = rp.post(x, Integer.MAX_VALUE);
//
class WaitTask implements Runnable {
public boolean finished;
public synchronized void run() {
task.waitFinished();
finished = true;
notifyAll();
}
public synchronized void w(int timeOut) throws Exception {
if (!finished) {
wait(timeOut);
}
}
}
WaitTask wt = new WaitTask();
rp.post(wt);
wt.w(0);
assertTrue("The task.waitFinished has to finish, otherwise the RequestProcessor thread will stay occupied forever", wt.finished);
x.assertCnt("The task has been executed - wait from RP made it start", 1);
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testTheCancelOfNonStartedTask.
public void testTheCancelOfNonStartedTask() {
Counter x = new Counter();
RequestProcessor rp = new RequestProcessor("testTheCancelOfNonStartedTask");
final RequestProcessor.Task task = rp.create(x);
assertFalse("Not started tasks cannot be cancelled", task.cancel());
assertFalse("But not finished", task.isFinished());
assertFalse("Can be cancelled only once", task.cancel());
}
Aggregations