Search in sources :

Example 71 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobCancelTest method testCancelMultipleJobsByName.

/**
 * Cancel multiple jobs with the same job-id.
 */
@Test
public void testCancelMultipleJobsByName() throws Exception {
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final String commonJobName = "777";
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(6);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(4);
    // Job-1 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-1-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null).withExceptionHandling(null, false));
    // Job-2 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-2-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null).withExceptionHandling(null, false));
    // Job-3 (common-id) => CANCEL
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            // Job-3a (other name, same re-used runMonitor => CANCEL AS WELL)
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-3a-interrupted");
                    }
                    verifyLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("otherName").withExecutionSemaphore(null).withExceptionHandling(null, false));
            // Job-3b (other name, other runMonitor => NO CANCEL)
            Jobs.getJobManager().schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        setupLatch.countDownAndBlock();
                    } catch (InterruptedException e) {
                        protocol.add("job-3b-interrupted");
                    }
                }
            }, Jobs.newInput().withRunContext(RunContexts.copyCurrent().withRunMonitor(new RunMonitor())).withName("otherName").withExecutionSemaphore(null).withExceptionHandling(null, false));
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-3-interrupted");
            }
            verifyLatch.countDown();
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(null));
    // Job-4 (common-id, but not-null mutex)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("job-4-interrupted");
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName(commonJobName).withExecutionSemaphore(Jobs.newExecutionSemaphore(1)).withExceptionHandling(null, false));
    assertTrue(setupLatch.await());
    Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchName(commonJobName).andMatchExecutionSemaphore(null).toFilter(), true);
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-interrupted", "job-2-interrupted", "job-3-interrupted", "job-3a-interrupted"), protocol);
    // release not cancelled jobs
    setupLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 72 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobCancelTest method testCancelledWithEmptyRunContext.

@Test
public void testCancelledWithEmptyRunContext() {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch assertLatch = new BlockingCountDownLatch(1);
    final AtomicBoolean cancelled = new AtomicBoolean();
    // Run test within RunContext to ensure a current RunMonitor
    RunContexts.empty().run(new IRunnable() {

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

                @Override
                public void run() throws Exception {
                    setupLatch.countDownAndBlock();
                    cancelled.set(RunMonitor.CURRENT.get().isCancelled());
                    assertLatch.countDown();
                }
            }, Jobs.newInput().withRunContext(RunContexts.empty()));
            setupLatch.await();
            RunMonitor.CURRENT.get().cancel(false);
            setupLatch.unblock();
            assertLatch.await();
            // RunMonitor of context is not associated with the current monitor
            assertFalse("no nested cancellation expected", cancelled.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 73 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobFutureVisitTest method before.

@Before
public void before() throws InterruptedException {
    m_jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
    });
    m_mutex1 = Jobs.newExecutionSemaphore(1);
    m_mutex2 = Jobs.newExecutionSemaphore(1);
    m_mutex3 = Jobs.newExecutionSemaphore(1);
    // prepare the test-case
    // synchronized because modified/read by different threads.
    protocol = Collections.synchronizedSet(new HashSet<String>());
    bc1 = Jobs.newBlockingCondition(true);
    bc2 = Jobs.newBlockingCondition(true);
    m_latch = new BlockingCountDownLatch(3);
    // SESSION 1 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc1.waitFor();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job1").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 1 (JOB-2)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job2").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 1 (JOB-3)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job3").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex1));
    // =========
    // SESSION 2 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job1").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
    // SESSION 2 (JOB-2)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc2.waitFor();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job2").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 2  (JOB-3)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            bc2.setBlocking(false);
            // Wait until job 'mutex2_job2' is re-acquiring the mutex. [3=job-2, job-3, job-4]
            JobTestUtil.waitForPermitCompetitors(m_mutex2, 3);
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job3").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    // SESSION 2  (JOB-4)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job4").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
    // =========
    // SESSION 3 (JOB-1)
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(IFuture.CURRENT.get().getJobInput().getName());
            m_latch.countDownAndBlock();
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex3_job1").withExecutionSemaphore(m_mutex3).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
    assertTrue(m_latch.await());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobManager(org.eclipse.scout.rt.platform.job.internal.JobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 74 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobListenerTest method testLocalListener1.

@Test
public void testLocalListener1() throws InterruptedException {
    // schedule job, and install listener once started running
    final BlockingCountDownLatch jobRunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            jobRunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(jobRunningLatch.await());
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.DONE).andMatchFuture(future).toFilter(), captureListener);
    jobRunningLatch.unblock();
    future.awaitDone();
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 75 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobListenerTest method testLocalListener2b.

@Test
public void testLocalListener2b() throws InterruptedException {
    IFuture<Void> future1 = Jobs.getJobManager().schedule(mock(IRunnable.class), Jobs.newInput());
    future1.awaitDone();
    final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job2RunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(job2RunningLatch.await());
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchFuture(future2).andMatchFuture(future1).toFilter(), captureListener);
    job2RunningLatch.unblock();
    future2.awaitDone();
    // verify events
    assertTrue(captureListener.getCapturedEvents().isEmpty());
    assertTrue(captureListener.getCapturedFutureStates().isEmpty());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)93 Test (org.junit.Test)89 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)66 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)30 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)14 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)12 ArrayList (java.util.ArrayList)10 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)10 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)9 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)9 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)7 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)7 Times (org.eclipse.scout.rt.testing.platform.runner.Times)7 Holder (org.eclipse.scout.rt.platform.holders.Holder)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IMessage (org.eclipse.scout.rt.mom.api.IMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)3 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)3