use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDoneAndGetWithTimeout_Interrupted.
@Test(timeout = 5000)
public void testAwaitDoneAndGetWithTimeout_Interrupted() throws java.lang.InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Init
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test in a separate thread
IFuture<Void> controller = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
Thread.currentThread().interrupt();
try {
future.awaitDoneAndGet(10, TimeUnit.SECONDS);
fail("interruption expected");
} catch (ThreadInterruptedError e) {
assertTrue(Thread.currentThread().isInterrupted());
}
}
}, Jobs.newInput());
controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
setupLatch.unblock();
future.awaitDone(10, TimeUnit.SECONDS);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class CompletionPromiseTest method test.
@SuppressWarnings("unchecked")
@Test
public void test() throws InterruptedException, ExecutionException, TimeoutException {
final List<String> protocol = new ArrayList<>();
final Thread callingThread = Thread.currentThread();
// Latch to wait until ready
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
// Latch to wait until done-handling is done
final BlockingCountDownLatch doneLatch = new BlockingCountDownLatch(3);
final JobFutureTask<String> future = mock(JobFutureTask.class);
when(future.isDone()).thenReturn(false);
final CompletionPromise<String> promise = new CompletionPromise<>(future, m_executor);
// Schedule job-1
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("1");
setupLatch.countDownAndBlock();
// Simulate the Future to be in done state
when(future.isDone()).thenReturn(true);
// Run the test
promise.done();
}
}, Jobs.newInput().withName("job-1"));
// Schedule job-2
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDown();
promise.awaitDoneAndGet();
doneLatch.countDown();
}
}, Jobs.newInput().withName("job-2"));
// Schedule job-3
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDown();
promise.awaitDoneAndGet(10, TimeUnit.SECONDS);
doneLatch.countDown();
}
}, Jobs.newInput().withName("job-3"));
// Wait until all jobs are running
assertTrue(setupLatch.await());
// Asynchronous callback invocation
promise.whenDone(new IDoneHandler<String>() {
@Override
public void onDone(DoneEvent<String> event) {
protocol.add("3");
if (callingThread != Thread.currentThread()) {
protocol.add("4");
}
doneLatch.countDown();
}
}, RunContexts.copyCurrent());
protocol.add("2");
// Let all jobs finish
setupLatch.unblock();
// Wait until done-handling is done
assertTrue(doneLatch.await());
// Synchronous callback invocation
promise.whenDone(new IDoneHandler<String>() {
@Override
public void onDone(DoneEvent<String> event) {
protocol.add("5");
if (callingThread == Thread.currentThread()) {
protocol.add("6");
}
doneLatch.countDown();
}
}, RunContexts.copyCurrent());
protocol.add("7");
// Any get-call should not block, because Future is in done-state.
promise.awaitDoneAndGet(10, TimeUnit.SECONDS);
promise.awaitDoneAndGet();
assertEquals(Arrays.asList("1", "2", "3", "4", "5", "6", "7"), protocol);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class SerialFutureExecutionTest method testWithFixedDelay.
/**
* Tests that a future is not run concurrently.
* <p>
* For that, we schedule a job periodically with a fixed delay (every millisecond), but sleep in the Runnable of the
* first round, so it does not complete. Upon continuation, we expect all other rounds to be made.
*/
@Test
public void testWithFixedDelay() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final String jobIdentifier = UUID.randomUUID().toString();
final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("before");
latch.countDownAndBlock();
protocol.add("after");
}
}, Jobs.newInput().withExecutionHint(jobIdentifier).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(FixedDelayScheduleBuilder.repeatForTotalCount(100, 1, TimeUnit.MILLISECONDS))));
latch.await();
// Wait some time until trigger fired for all rounds
Thread.sleep(2000);
// Verify no concurrent execution
List<String> expectedProtocol = new ArrayList<>();
// first round started
expectedProtocol.add("before");
assertEquals(expectedProtocol, protocol);
latch.unblock();
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(jobIdentifier).toFilter(), 10, TimeUnit.SECONDS);
// first round completed
expectedProtocol.add("after");
for (int i = 0; i < 99; i++) {
// other 99 rounds
expectedProtocol.add("before");
// other 99 rounds
expectedProtocol.add("after");
}
assertEquals(expectedProtocol, protocol);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class SerialFutureExecutionTest method testAtFixedRate_NowWithRemainingCount.
/**
* Tests that a future is not run concurrently.
* <p>
* For that, we schedule a job periodically at a fixed rate (every millisecond), but sleep in the Runnable of the
* first round, so it does not complete. Upon continuation, we expect one last consolidated round to be executed.
* <p>
* Misfire policy: {@link SimpleTrigger#MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT}
*/
@Test
public void testAtFixedRate_NowWithRemainingCount() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final String jobIdentifier = UUID.randomUUID().toString();
final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("before");
latch.countDownAndBlock();
protocol.add("after");
}
}, Jobs.newInput().withExecutionHint(jobIdentifier).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(1).withMisfireHandlingInstructionNowWithRemainingCount().withRepeatCount(100))));
latch.await();
// Wait some time until trigger fired for all rounds
Thread.sleep(2000);
// Verify no concurrent execution
List<String> expectedProtocol = new ArrayList<>();
// first round started
expectedProtocol.add("before");
assertEquals(expectedProtocol, protocol);
latch.unblock();
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(jobIdentifier).toFilter(), 10, TimeUnit.SECONDS);
// first round completed
expectedProtocol.add("after");
// consolidated round
expectedProtocol.add("before");
// consolidated round
expectedProtocol.add("after");
assertEquals(expectedProtocol, protocol);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class ThreadNameDecoratorTest method testThreadName.
@Test
public void testThreadName() throws InterruptedException {
final AtomicReference<Thread> workerThread = new AtomicReference<>();
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(1);
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
workerThread.set(Thread.currentThread());
latch1.countDownAndBlock();
condition.waitFor(10, TimeUnit.SECONDS);
latch2.countDownAndBlock();
}
}, Jobs.newInput().withExecutionSemaphore(semaphore).withThreadName("test-thread").withName("job-1"));
// Test while running
assertTrue(latch1.await());
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
latch1.unblock();
// Test while blocked
JobTestUtil.waitForPermitCompetitors(semaphore, 0);
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
// Test while waiting for permit
semaphore.withPermits(0);
condition.setBlocking(false);
JobTestUtil.waitForPermitCompetitors(semaphore, 1);
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
// Test while running
semaphore.withPermits(1);
assertTrue(latch2.await());
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
latch2.unblock();
}
Aggregations