use of org.openide.util.Task in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testPriorityInversionProblemAndItsDiagnosis.
/**
* Test priority inversion and whether it is properly notified
*/
public void testPriorityInversionProblemAndItsDiagnosis() throws Exception {
RequestProcessor rp = new RequestProcessor("testPriorityInversionProblemAndItsDiagnosis");
final Runnable[] arr = new Runnable[3];
class R implements Runnable {
public int index;
public Task t;
public R(int i) {
index = i;
}
public synchronized void run() {
for (int i = 0; ; /*i < arr.length*/
i++) {
if (arr[i] == null) {
arr[i] = this;
break;
}
}
if (t != null) {
t.waitFinished();
}
}
@Override
public String toString() {
return " R index " + index;
}
}
R r1 = new R(1);
R r2 = new R(2);
R r3 = new R(3);
Task t1;
Task t2;
Task t3;
synchronized (r1) {
t1 = rp.post(r1);
t2 = rp.post(r2);
// r1 will call the waitFinished of r3
r1.t = t3 = rp.post(r3);
}
t1.waitFinished();
t2.waitFinished();
t3.waitFinished();
assertEquals("First started is t1", r1, arr[0]);
assertEquals("Second started is t3", r3, arr[1]);
assertEquals("Last started is t2", r2, arr[2]);
// now we should ensure that the RP warned everyone about the
// priority inheritance and all its possible complications (t2 running
// later than t3)
}
use of org.openide.util.Task in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testInterruptedStatusIsClearedBetweenTwoTaskExecution.
public void testInterruptedStatusIsClearedBetweenTwoTaskExecution() throws Exception {
RequestProcessor rp = new RequestProcessor("testInterruptedStatusIsClearedBetweenTwoTaskExecution", 1, true);
final RequestProcessor.Task[] task = new RequestProcessor.Task[1];
// test interrupted status is cleared after task ends
class Fail implements Runnable {
public boolean checkBefore;
public Thread runIn;
public boolean goodThread;
public synchronized void run() {
if (runIn == null) {
runIn = Thread.currentThread();
task[0].schedule(0);
// wait to make sure the task is scheduled
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else {
goodThread = Thread.currentThread() == runIn;
}
checkBefore = runIn.isInterrupted();
// set the flag for next execution
runIn.interrupt();
notifyAll();
}
}
Fail f = new Fail();
synchronized (f) {
task[0] = rp.post(f);
// wait for the first execution
f.wait();
}
// wait for the second
task[0].waitFinished();
if (f.goodThread) {
assertTrue("Interrupted state has been cleared between two executions of the task", !f.checkBefore);
}
}
use of org.openide.util.Task in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testCancel.
/**
* Make sure that successfully canceled task is not performed.
*/
public void testCancel() throws Exception {
class X implements Runnable {
public boolean performed = false;
public void run() {
performed = true;
}
}
X x = new X();
final boolean[] finished = new boolean[1];
finished[0] = false;
// post task with some delay
RequestProcessor.Task task = RequestProcessor.postRequest(x, 1000);
task.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task t) {
finished[0] = true;
}
});
boolean canceled = task.cancel();
assertTrue("Task is canceled now", canceled);
assertTrue("Cancelling actually means finished", finished[0]);
// wait longer than task delay
Thread.sleep(1500);
assertFalse("Task should not be performed", x.performed);
}
use of org.openide.util.Task in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testBug31906_SimulateDataFolderTest.
/**
* Test bug http://www.netbeans.org/issues/show_bug.cgi?id=31906
*/
public void testBug31906_SimulateDataFolderTest() {
RequestProcessor rp = new RequestProcessor("dataFolderTest");
class X implements Runnable {
private RequestProcessor.Task wait;
private int cnt;
public synchronized void run() {
if (wait != null) {
wait.waitFinished();
cnt++;
} else {
cnt++;
}
}
public synchronized void assertCnt(String msg, int cnt) {
assertEquals(msg, cnt, this.cnt);
this.cnt = 0;
}
public synchronized void waitFor(RequestProcessor.Task t) {
wait = t;
}
}
X[] arr = { new X(), new X() };
RequestProcessor.Task[] tasks = { rp.create(arr[0]), rp.create(arr[1]) };
tasks[0].setPriority(Thread.NORM_PRIORITY - 1);
tasks[1].setPriority(Thread.NORM_PRIORITY + 1);
tasks[0].schedule(0);
tasks[1].schedule(0);
tasks[0].waitFinished();
arr[0].assertCnt(" Once", 1);
tasks[1].waitFinished();
arr[1].assertCnt(" Once as well", 1);
tasks[0].schedule(100);
tasks[1].schedule(100);
tasks[0].schedule(10);
tasks[1].schedule(10);
tasks[0].waitFinished();
tasks[1].waitFinished();
arr[0].assertCnt(" 1a", 1);
arr[1].assertCnt(" 1b", 1);
arr[0].waitFor(tasks[1]);
tasks[1].schedule(100);
tasks[0].schedule(10);
tasks[0].waitFinished();
arr[0].assertCnt(" task 0 is executed", 1);
arr[1].assertCnt(" but it also executes task 1", 1);
tasks[0].schedule(10);
tasks[0].waitFinished();
arr[0].assertCnt(" task O is executed", 1);
arr[1].assertCnt(" but it does not execute 1", 0);
}
use of org.openide.util.Task in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testWaitFinishedFromNotification.
/**
* Make sure it is safe to call waitFinished() on a task from within
* a task listener.
*/
public void testWaitFinishedFromNotification() throws Exception {
class X implements Runnable {
private Task task;
private int cnt;
public synchronized Task start() {
if (task == null) {
task = RequestProcessor.postRequest(this);
}
return task;
}
public void run() {
cnt++;
}
public int getCount() {
return cnt;
}
public void block() {
start().waitFinished();
}
}
final X x = new X();
final Object lock = "wait for task to finish";
final boolean[] finished = new boolean[1];
x.start().addTaskListener(new TaskListener() {
public void taskFinished(Task t) {
x.block();
finished[0] = true;
synchronized (lock) {
lock.notify();
}
}
});
synchronized (lock) {
lock.wait(5000);
}
assertTrue(finished[0]);
assertEquals(1, x.getCount());
}
Aggregations