use of org.eclipse.scout.rt.platform.job.IBlockingCondition in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testThreePermitsAndBlocking.
/**
* Tests execution semaphore with 3 permits and with a blocking condition involved.
* <p>
* In total, 7 jobs are scheduled. Thereby, job-1 and job-3 never finish, and job-2 enters a blocking condition.
* <p>
* This test tests, that because job-2 enters a blocking condition, job-4 starts running. Once job-4 completed, job-5
* starts running. Then, job-5 unblocks the conditions, with lets job-2 to continue after job-5 finished. After job-2
* finished, job-6, and then job-7 start running.
* <p>
*/
@Test
// regression
@Times(500)
public void testThreePermitsAndBlocking() throws InterruptedException {
final IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(3);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(2);
final BlockingCountDownLatch latchJob2 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch latchJob5 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch latchJob6 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch latchJob7 = new BlockingCountDownLatch(1);
// job-1
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
setupLatch.countDownAndBlock();
finishLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-1").withExecutionSemaphore(semaphore));
// job-2
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-2-running (a)");
condition.waitFor(30, TimeUnit.SECONDS);
protocol.add("job-2-running (b)");
latchJob2.countDownAndBlock();
}
}, Jobs.newInput().withName("job-2").withExecutionSemaphore(semaphore));
// job-3
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-3-running");
setupLatch.countDownAndBlock();
finishLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-3").withExecutionSemaphore(semaphore));
// job-4
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-4-running");
setupLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-4").withExecutionSemaphore(semaphore));
// job-5
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-5-running");
condition.setBlocking(false);
// Wait until job-2 is competing for a permit anew.
// Otherwise, job-6 might get the permit before job-2.
// permit-owners: job-1, job-3, job-5, queue: job-2 (RE-ACQUIRE), job-6, job-7
JobTestUtil.waitForPermitCompetitors(semaphore, 6);
latchJob5.countDownAndBlock();
}
}, Jobs.newInput().withName("job-5").withExecutionSemaphore(semaphore));
// job-6
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-6-running");
latchJob6.countDownAndBlock();
}
}, Jobs.newInput().withName("job-6").withExecutionSemaphore(semaphore));
// job-7
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-7-running");
latchJob7.countDownAndBlock();
}
}, Jobs.newInput().withName("job-7").withExecutionSemaphore(semaphore));
// verify
assertTrue(setupLatch.await());
assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running (a)", "job-3-running", "job-4-running"), protocol);
protocol.clear();
setupLatch.unblock();
assertTrue(latchJob5.await());
assertEquals(CollectionUtility.hashSet("job-5-running"), protocol);
protocol.clear();
latchJob5.unblock();
try {
assertTrue(latchJob2.await());
} catch (AssertionError e) {
System.out.println(protocol);
throw e;
}
assertEquals(CollectionUtility.hashSet("job-2-running (b)"), protocol);
protocol.clear();
latchJob2.unblock();
assertTrue(latchJob6.await());
assertEquals(CollectionUtility.hashSet("job-6-running"), protocol);
protocol.clear();
latchJob6.unblock();
assertTrue(latchJob7.await());
assertEquals(CollectionUtility.hashSet("job-7-running"), protocol);
latchJob7.unblock();
finishLatch.unblock();
}
use of org.eclipse.scout.rt.platform.job.IBlockingCondition in project scout.rt by eclipse.
the class SmartFieldLookupTest method testTextLookupExceptions_InBackground.
@SuppressWarnings("unchecked")
@Test
public void testTextLookupExceptions_InBackground() throws InterruptedException {
m_field.setLookupCall(new TestLookupCall());
final String errorText = "lookup error";
when(m_mock_service.getDataByText(any(ILookupCall.class))).thenThrow(new PlatformException(errorText));
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
ILookupRowFetchedCallback callback = new ILookupRowFetchedCallback<Long>() {
@Override
public void onSuccess(List<? extends ILookupRow<Long>> rows) {
Assert.fail("no exception thrown");
bc.setBlocking(false);
}
@Override
public void onFailure(RuntimeException exception) {
assertTrue(exception instanceof PlatformException);
assertEquals(errorText, exception.getMessage());
bc.setBlocking(false);
}
};
m_field.callTextLookupInBackground("", 10, callback);
bc.waitFor();
}
use of org.eclipse.scout.rt.platform.job.IBlockingCondition in project scout.rt by eclipse.
the class SmartFieldLookupTest method awaitDoneAndGet.
/**
* await result while freeing model thread
*/
private <T> T awaitDoneAndGet(IFuture<T> futureRows) {
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
futureRows.whenDone(new IDoneHandler<T>() {
@Override
public void onDone(DoneEvent<T> event) {
bc.setBlocking(false);
}
}, ClientRunContexts.copyCurrent());
bc.waitFor();
return futureRows.awaitDoneAndGet();
}
use of org.eclipse.scout.rt.platform.job.IBlockingCondition in project scout.rt by eclipse.
the class SmartFieldLookupTest method testSubtreeLookupExceptions_InBackground.
@SuppressWarnings("unchecked")
@Test
public void testSubtreeLookupExceptions_InBackground() throws InterruptedException {
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
m_field.setLookupCall(new TestLookupCall());
when(m_mock_service.getDataByRec(any(ILookupCall.class))).thenThrow(new PlatformException("lookup error"));
IFuture<List<ILookupRow<Long>>> rows = m_field.callSubTreeLookupInBackground(1L, TriState.TRUE, false);
rows.whenDone(new IDoneHandler<List<ILookupRow<Long>>>() {
@Override
public void onDone(DoneEvent<List<ILookupRow<Long>>> event) {
assertTrue(event.getException() instanceof RuntimeException);
assertEquals("lookup error", event.getException().getMessage());
bc.setBlocking(false);
}
}, ClientRunContexts.copyCurrent());
bc.waitFor();
}
use of org.eclipse.scout.rt.platform.job.IBlockingCondition in project scout.rt by eclipse.
the class SmartFieldTest method waitForProposalResult.
private void waitForProposalResult(String property) {
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
IProposalChooser<?, ?> chooser = m_styleField.getProposalChooser();
chooser.addPropertyChangeListener(property, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
bc.setBlocking(false);
}
});
bc.waitFor();
}
Aggregations