use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testNonParallelReSchedule.
public void testNonParallelReSchedule() throws Exception {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger peek = new AtomicInteger();
peek.set(1);
class R implements Runnable {
@Override
public void run() {
try {
int cnt = counter.incrementAndGet();
Thread.sleep(200);
int now = counter.get();
if (now > peek.get()) {
peek.set(now);
}
counter.decrementAndGet();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
R run = new R();
RequestProcessor RP = new RequestProcessor("testNonParallelReSchedule", 20);
RequestProcessor.Task task = RP.create(run);
for (int i = 0; i < 20; i++) {
task.schedule(0);
Thread.sleep(10);
}
for (int i = 0; i < 50; i++) {
task.waitFinished();
}
assertEquals("At most one task at once", 1, peek.get());
}
use of org.openide.util.RequestProcessor in project netbeans-rcp-lite by outersky.
the class RequestProcessorTest method testStartCreatedJob.
public void testStartCreatedJob() throws Exception {
final RequestProcessor rp = new RequestProcessor("testStartCreatedJob");
final boolean[] executed = new boolean[1];
rp.post(new Runnable() {
@Override
public void run() {
RequestProcessor.Task t = rp.create(new Runnable() {
@Override
public void run() {
executed[0] = true;
}
});
t.waitFinished();
}
}).waitFinished();
assertTrue("Inner created task finished", executed[0]);
}
use of org.openide.util.RequestProcessor 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.RequestProcessor 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.RequestProcessor 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);
}
Aggregations