use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class JobStateTest method testWaitingForMutexAndBlockingCondition.
@Test
// regression
@Times(100)
public void testWaitingForMutexAndBlockingCondition() throws ThreadInterruptedError, java.lang.InterruptedException {
JobEventCaptureListener captureListener = new JobEventCaptureListener();
Jobs.getJobManager().addListener(captureListener);
final BlockingCountDownLatch job1RunningLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch job2UnblockedLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch job3RunningLatch = new BlockingCountDownLatch(1);
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
// Schedule job-1
IFuture<Void> future1 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
job1RunningLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-1").withExecutionSemaphore(mutex));
// wait until running (for idempotent event assertion)
assertTrue(job1RunningLatch.await());
// Schedule job-2
IFuture<Void> future2 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
job2RunningLatch.countDownAndBlock();
condition.waitFor();
job2UnblockedLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-2").withExecutionSemaphore(mutex));
// Wait until competing for a permit.
// That is for idempotent event assertion, because permit is acquired asynchronously in another thread.
// However, permit acquisition is guaranteed to be in the 'as-scheduled' order. Nevertheless, the SCHEDULING and PENDING event of the next job would possibly interfere.
JobTestUtil.waitForPermitCompetitors(mutex, 2);
// Schedule job-3
IFuture<Void> future3 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
job3RunningLatch.countDownAndBlock();
JobTestUtil.waitForPermitCompetitors(mutex, 2);
}
}, Jobs.newInput().withName("job-3").withExecutionSemaphore(mutex));
// Wait until competing for a permit.
// That is for idempotent event assertion, because permit is acquired asynchronously in another thread.
// However, permit acquisition is guaranteed to be in the 'as-scheduled' order. Nevertheless, the SCHEDULING and PENDING event of the next job would possibly interfere.
JobTestUtil.waitForPermitCompetitors(mutex, 3);
assertEquals(JobState.RUNNING, future1.getState());
assertEquals(JobState.WAITING_FOR_PERMIT, future2.getState());
assertEquals(JobState.WAITING_FOR_PERMIT, future3.getState());
job1RunningLatch.unblock();
job2RunningLatch.await();
assertEquals(JobState.DONE, future1.getState());
assertEquals(JobState.RUNNING, future2.getState());
assertEquals(JobState.WAITING_FOR_PERMIT, future3.getState());
job2RunningLatch.unblock();
job3RunningLatch.await();
assertEquals(JobState.DONE, future1.getState());
assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, future2.getState());
assertEquals(JobState.RUNNING, future3.getState());
condition.setBlocking(false);
// job-2 and job-3
JobTestUtil.waitForPermitCompetitors(mutex, 2);
assertEquals(JobState.DONE, future1.getState());
assertEquals(JobState.WAITING_FOR_PERMIT, future2.getState());
assertEquals(JobState.RUNNING, future3.getState());
job3RunningLatch.unblock();
job2UnblockedLatch.await();
assertEquals(JobState.DONE, future1.getState());
assertEquals(JobState.RUNNING, future2.getState());
assertEquals(JobState.DONE, future3.getState());
job2UnblockedLatch.unblock();
future2.awaitDoneAndGet(5, TimeUnit.SECONDS);
future3.awaitDoneAndGet(5, TimeUnit.SECONDS);
assertEquals(JobState.DONE, future1.getState());
assertEquals(JobState.DONE, future2.getState());
assertEquals(JobState.DONE, future3.getState());
// verify events
int i = -1;
List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
i++;
assertStateChangedEvent(future1, JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future3, JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future3, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future1, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
i++;
assertBlockedStateEvent(future2, condition, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future3, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future3, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future2, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
assertEquals(i + 1, capturedEvents.size());
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testSemaphore.
@Test
@Times(20)
public void testSemaphore() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1).seal();
// Schedule arbitrary job with same semaphore
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
}
}, Jobs.newInput().withExecutionSemaphore(mutex));
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return "abc";
}
}, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
true));
// Schedule function with same semaphore
final AtomicBoolean functionExecuted = new AtomicBoolean(false);
IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, Void>() {
@Override
public Void apply(String result, Throwable error) {
functionExecuted.set(true);
return null;
}
}, Jobs.newInput().withExecutionSemaphore(mutex));
assertEquals("abc", future.awaitDoneAndGet(5, TimeUnit.SECONDS));
JobTestUtil.waitForPermitCompetitors(mutex, 2);
// Function future must not have commenced execution yet.
try {
functionFuture.awaitDone(100, TimeUnit.MILLISECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
// NOOP
}
assertFalse(functionExecuted.get());
setupLatch.unblock();
functionFuture.awaitDone(5, TimeUnit.SECONDS);
assertTrue(functionExecuted.get());
assertFalse(future.isCancelled());
assertTrue(future.isDone());
assertFalse(functionFuture.isCancelled());
assertTrue(functionFuture.isDone());
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class MutualExclusionTest method testBlockingCondition_InterruptedWhileReAcquiringTheMutex.
/**
* We have 3 jobs that are scheduled simultaneously. Thereby, job1 enters a blocking condition which in turn lets job2
* run. Job2 unblocks job1 so that job1 is trying to re-acquire the mutex. While waiting for the mutex to be
* available, job1 is interrupted due to a cancel-request of job2.<br/>
* This test verifies, that job1 is interrupted, competes for the mutex anew and only continues once a permit becomes
* available. Also, job3 must not start running as long as job1 did not complete.
*/
@Test
// regression
@Times(100)
public void testBlockingCondition_InterruptedWhileReAcquiringTheMutex() throws java.lang.InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final BlockingCountDownLatch latchJob2 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch job1FinishLatch = new BlockingCountDownLatch(1);
final IFuture<Void> future1 = ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-1");
try {
protocol.add("before-blocking-1");
condition.waitFor();
} catch (ThreadInterruptedError e) {
protocol.add("interrupted-1 (a)");
} catch (RuntimeException e) {
protocol.add("jobException-1");
}
if (Thread.currentThread().isInterrupted()) {
protocol.add("interrupted-1 (b)");
}
if (ModelJobs.isModelThread()) {
protocol.add("model-thread-1");
}
protocol.add("done-1");
job1FinishLatch.countDown();
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-1").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
final IFuture<Void> future2 = ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-2a");
if (future1.getState() == JobState.WAITING_FOR_BLOCKING_CONDITION) {
protocol.add("job2: job-1-waiting-for-blocking-condition");
}
if (!future1.getExecutionSemaphore().isPermitOwner(future1)) {
protocol.add("job2: job-1-not-permit-owner");
}
protocol.add("unblocking condition");
condition.setBlocking(false);
// job-1 (interrupted acquisition task), job-2 (latch), job-3 (waiting for mutex)
JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 3);
if (future1.getState() == JobState.WAITING_FOR_PERMIT) {
protocol.add("job2: job-1-waiting-for-mutex");
}
protocol.add("before-cancel-job1-2");
// interrupt job1 while acquiring the mutex
future1.cancel(true);
// job-1 (interrupted acquisition task), job-1 (re-acquiring a permit), job-2 (latch), job-3 (waiting for mutex)
JobTestUtil.waitForPermitCompetitors(m_clientSession.getModelJobSemaphore(), 4);
// cancelled, but still running
JobTestUtil.waitForState(future1, JobState.DONE);
protocol.add("running-2b");
latchJob2.countDownAndBlock();
protocol.add("done-2");
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-2").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
final IFuture<Void> future3 = ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("done-3");
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("job-3").withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
assertTrue(latchJob2.await());
assertEquals(future1.getState(), JobState.DONE);
assertEquals(future2.getState(), JobState.RUNNING);
assertEquals(future3.getState(), JobState.WAITING_FOR_PERMIT);
latchJob2.unblock();
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 5, TimeUnit.SECONDS);
List<String> expectedProtocol = new ArrayList<>();
expectedProtocol.add("running-1");
expectedProtocol.add("before-blocking-1");
expectedProtocol.add("running-2a");
expectedProtocol.add("job2: job-1-waiting-for-blocking-condition");
expectedProtocol.add("job2: job-1-not-permit-owner");
expectedProtocol.add("unblocking condition");
expectedProtocol.add("job2: job-1-waiting-for-mutex");
expectedProtocol.add("before-cancel-job1-2");
expectedProtocol.add("running-2b");
expectedProtocol.add("done-2");
expectedProtocol.add("interrupted-1 (b)");
expectedProtocol.add("model-thread-1");
expectedProtocol.add("done-1");
expectedProtocol.add("done-3");
assertEquals(expectedProtocol, protocol);
assertEquals(future1.getState(), JobState.DONE);
assertEquals(future2.getState(), JobState.DONE);
assertEquals(future3.getState(), JobState.DONE);
assertTrue(future1.isCancelled());
future1.awaitDone(1, TimeUnit.NANOSECONDS);
assertFalse(future2.isCancelled());
future2.awaitDone(1, TimeUnit.NANOSECONDS);
assertFalse(future3.isCancelled());
future3.awaitDone(1, TimeUnit.NANOSECONDS);
}
use of org.eclipse.scout.rt.testing.platform.runner.Times 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.testing.platform.runner.Times 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()));
}
}
Aggregations