use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class AbstractPageWithTableTest method testModelContextOfInitPageAndInitTable.
@Test
public void testModelContextOfInitPageAndInitTable() {
IForm mockForm = Mockito.mock(IForm.class);
IOutline mockOutline = Mockito.mock(IOutline.class);
ClientRunContexts.copyCurrent().withOutline(mockOutline, true).withForm(mockForm).run(new IRunnable() {
@Override
public void run() throws Exception {
IDesktop desktop = TestEnvironmentClientSession.get().getDesktop();
assertNotNull(desktop);
desktop.setAvailableOutlines(CollectionUtility.arrayList(new PageWithTableOutline()));
desktop.setOutline(PageWithTableOutline.class);
desktop.activateFirstPage();
IOutline outline = desktop.getOutline();
assertNotNull(outline);
assertSame(PageWithTableOutline.class, outline.getClass());
IPage<?> page = outline.getActivePage();
assertNotNull(page);
assertSame(ParentItemTablePage.class, page.getClass());
ParentItemTablePage tablePage = (ParentItemTablePage) page;
// init page
ModelContext initPageContext = tablePage.getInitPageContext();
assertNotNull(initPageContext);
assertSame(desktop, initPageContext.getDesktop());
assertSame(outline, initPageContext.getOutline());
// no context form must be set
assertNull(initPageContext.getForm());
// init table
ModelContext initTableContext = tablePage.getInitTableContext();
assertNotNull(initTableContext);
assertSame(desktop, initTableContext.getDesktop());
assertSame(outline, initTableContext.getOutline());
// no context form must be set
assertNull(initTableContext.getForm());
}
});
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testChangePermits1.
/**
* Tests no permit available upon completion
*/
@Test
public void testChangePermits1() {
final IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final String executionHint = UUID.randomUUID().toString();
IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-2-running");
}
}, Jobs.newInput().withName("job-2").withExecutionHint(executionHint).withExceptionHandling(null, false).withExecutionSemaphore(semaphore));
// Change the permits to 0
semaphore.withPermits(0);
}
}, Jobs.newInput().withName("job-1").withExecutionHint(executionHint).withExecutionSemaphore(semaphore));
future.awaitDone(1, TimeUnit.SECONDS);
assertEquals(CollectionUtility.hashSet("job-1-running"), protocol);
IFilter<IFuture<?>> job2Filter = Jobs.newFutureFilterBuilder().andMatchExecutionHint(executionHint).toFilter();
try {
Jobs.getJobManager().awaitDone(job2Filter, 1, TimeUnit.SECONDS);
fail("timeout expected because no permit available");
} catch (TimedOutError e) {
// NOOP
}
// Change permits to 1 --> job-2 should run
protocol.clear();
semaphore.withPermits(1);
Jobs.getJobManager().awaitDone(job2Filter, 1, TimeUnit.SECONDS);
assertEquals(CollectionUtility.hashSet("job-2-running"), protocol);
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testThreePermits.
/**
* Tests execution semaphore with 3 permits.
*/
@Test
// regression
@Times(500)
public void testThreePermits() throws InterruptedException {
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(3);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch latchGroup1 = new BlockingCountDownLatch(3);
final BlockingCountDownLatch latchGroup2 = new BlockingCountDownLatch(3);
final BlockingCountDownLatch latchGroup3 = new BlockingCountDownLatch(2);
// job-1
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
latchGroup1.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");
latchGroup1.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");
latchGroup1.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");
latchGroup2.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");
latchGroup2.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");
latchGroup2.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");
latchGroup3.countDownAndBlock();
}
}, Jobs.newInput().withName("job-7").withExecutionSemaphore(semaphore));
// job-8
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-8-running");
latchGroup3.countDownAndBlock();
}
}, Jobs.newInput().withName("job-8").withExecutionSemaphore(semaphore));
// verify group-1
assertTrue(latchGroup1.await());
assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running", "job-3-running"), protocol);
// verify group-2
protocol.clear();
latchGroup1.unblock();
assertTrue(latchGroup2.await());
assertEquals(CollectionUtility.hashSet("job-4-running", "job-5-running", "job-6-running"), protocol);
// verify group-3
protocol.clear();
latchGroup2.unblock();
assertTrue(latchGroup3.await());
assertEquals(CollectionUtility.hashSet("job-7-running", "job-8-running"), protocol);
// job-9
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-9-running");
}
}, Jobs.newInput().withName("job-9").withExecutionSemaphore(semaphore)).awaitDone();
assertEquals(CollectionUtility.hashSet("job-7-running", "job-8-running", "job-9-running"), protocol);
// cleanup
latchGroup3.unblock();
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testInternalDeadlock.
/**
* Tests an internal of {@link ExecutionSemaphore}, that {@link AcquisitionTask#notifyPermitAcquired()} is invoked
* outside the {@link ExecutionSemaphore} lock.
* <p>
* Otherwise, a deadlock might occur, once the resuming job-1 tries to re-acquire the permit, namely exactly the time
* when owning acquisitionLock in {@link ExecutionSemaphore#acquire(IFuture, QueuePosition)} and querying
* 'isPermitOwner'. Thereto, job-1 must compete for the semaphore lock, while job-2 (owning semaphore lock) tries to
* notify the resuming job-1 via {@link AcquisitionTask#notifyPermitAcquired()}, but cannot get monitor of
* acquisitionLock.
*/
@Test
// regression; do not remove
@Times(1_000)
public void testInternalDeadlock() {
final IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final AtomicReference<IFuture<?>> future2Ref = new AtomicReference<>();
IFuture<Void> future1 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
future2Ref.set(Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
condition.setBlocking(false);
}
}, Jobs.newInput().withName("job-2").withExecutionSemaphore(semaphore)));
condition.waitFor();
}
}, Jobs.newInput().withName("job-1").withExecutionSemaphore(semaphore));
try {
future1.awaitDoneAndGet(5, TimeUnit.SECONDS);
} catch (TimedOutError e) {
fail(String.format("Deadlock while passing permit from 'job-2' to 'job-1' [job-1-state=%s, job-2-state=%s", future1.getState(), future2Ref.get().getState()));
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable 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();
}
Aggregations