Search in sources :

Example 11 with IBlockingCondition

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();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 12 with IBlockingCondition

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();
}
Also used : TestLookupCall(org.eclipse.scout.rt.client.ui.form.fields.smartfield.fixture.TestLookupCall) ILookupRow(org.eclipse.scout.rt.shared.services.lookup.ILookupRow) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ILookupRowFetchedCallback(org.eclipse.scout.rt.shared.services.lookup.ILookupRowFetchedCallback) ArrayList(java.util.ArrayList) List(java.util.List) ILookupCall(org.eclipse.scout.rt.shared.services.lookup.ILookupCall) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 13 with IBlockingCondition

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();
}
Also used : IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition)

Example 14 with IBlockingCondition

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();
}
Also used : TestLookupCall(org.eclipse.scout.rt.client.ui.form.fields.smartfield.fixture.TestLookupCall) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ArrayList(java.util.ArrayList) List(java.util.List) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ILookupCall(org.eclipse.scout.rt.shared.services.lookup.ILookupCall) Test(org.junit.Test)

Example 15 with IBlockingCondition

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();
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition)

Aggregations

IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)32 Test (org.junit.Test)24 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)12 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)10 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)9 ArrayList (java.util.ArrayList)8 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)4 IFuture (org.eclipse.scout.rt.platform.job.IFuture)4 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 Times (org.eclipse.scout.rt.testing.platform.runner.Times)4 ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)3 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 Serializable (java.io.Serializable)2 List (java.util.List)2 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)2