Search in sources :

Example 1 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class MutualExclusionTest method testAwaitDoneWithSameMutex.

@Test(expected = AssertionException.class, timeout = 5000)
public void testAwaitDoneWithSameMutex() {
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Jobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                // NOOP
                }
            }, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDone();
        }
    }, Jobs.newInput().withExecutionSemaphore(mutex)).awaitDoneAndGet();
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 2 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ModelJobs method isModelThread.

/**
 * Returns <code>true</code> if the current thread represents the model thread for the given client session. At any
 * given time, there is only one model thread active per client session.
 */
public static boolean isModelThread(final IClientSession clientSession) {
    final IFuture<?> currentFuture = IFuture.CURRENT.get();
    if (!ModelJobs.isModelJob(currentFuture)) {
        return false;
    }
    final IExecutionSemaphore semaphore = currentFuture.getExecutionSemaphore();
    return semaphore != null && semaphore.isPermitOwner(currentFuture);
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore)

Example 3 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore 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);
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IFuture(org.eclipse.scout.rt.platform.job.IFuture) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 4 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore 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();
}
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) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 5 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore 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()));
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IFuture(org.eclipse.scout.rt.platform.job.IFuture) 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)

Aggregations

IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)28 Test (org.junit.Test)26 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)23 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)11 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)8 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)6 IFuture (org.eclipse.scout.rt.platform.job.IFuture)5 Times (org.eclipse.scout.rt.testing.platform.runner.Times)5 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JMSException (javax.jms.JMSException)1 NamingException (javax.naming.NamingException)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1