use of org.eclipse.scout.rt.testing.platform.runner.Times 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();
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class MisfireTest method testFinalRunAfterMisfire.
/**
* Tests that a last round is scheduled upon a 'misfire' with the end-time arrived.
*/
@Test
// regression
@Times(10)
public void testFinalRunAfterMisfire() {
final int endsIn = 20;
final int schedulingInterval = 1;
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final AtomicInteger roundCounter = new AtomicInteger();
IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
int round = roundCounter.incrementAndGet();
writeProtocol("BEFORE-SLEEP", (JobFutureTask<?>) IFuture.CURRENT.get(), round, protocol);
if (round == 1) {
SleepUtil.sleepSafe(endsIn + 50, TimeUnit.MILLISECONDS);
}
writeProtocol("AFTER-SLEEP", (JobFutureTask<?>) IFuture.CURRENT.get(), round, protocol);
}
}, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withEndIn(endsIn, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(schedulingInterval).repeatForever())));
// Wait until done
try {
future.awaitDone(10, TimeUnit.SECONDS);
} catch (TimedOutError e) {
future.cancel(true);
fail("Job is hanging because no last round scheduled upon misfire with end-time arrived");
}
// Verify
List<String> expectedProtocol = new ArrayList<>();
expectedProtocol.add("BEFORE-SLEEP: has-next-execution [round=1]");
expectedProtocol.add("AFTER-SLEEP: has-next-execution [round=1]");
expectedProtocol.add("BEFORE-SLEEP: final-run [round=2]");
expectedProtocol.add("AFTER-SLEEP: final-run [round=2]");
assertEquals(expectedProtocol, protocol);
assertTrue(((JobFutureTask<?>) future).isFinalRun());
assertFalse(((JobFutureTask<?>) future).hasNextExecution());
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class JmsMomImplementorTest method testQueueRequestReplyRequestFirst.
@Test(timeout = 200_000)
// regression
@Times(10)
public void testQueueRequestReplyRequestFirst() throws InterruptedException {
final IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testQueueRequestReplyRequestFirst", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
// 1. Initiate 'request-reply' communication
IFuture<String> requestFuture = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return MOM.request(JmsTestMom.class, queue, "hello world");
}
}, Jobs.newInput().withName("requester (Q)").withExecutionHint(m_testJobExecutionHint));
// Wait some time to give request message publisher time to send message
// If we wait not long enough, we get a false positive
Thread.sleep(300);
// 2. Subscribe for reply
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
// Subscribe replier
m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {
@Override
public String onRequest(IMessage<String> request) {
return request.getTransferObject().toUpperCase();
}
}));
}
}, Jobs.newInput().withName("replier (Q)").withExecutionHint(m_testJobExecutionHint));
String testee = requestFuture.awaitDoneAndGet(10, TimeUnit.SECONDS);
// Verify
assertEquals("HELLO WORLD", testee);
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class JmsMomImplementorTest method testTopicPublishFirst.
@Test
// regression
@Times(10)
public void testTopicPublishFirst() throws InterruptedException {
IDestination<String> topic = MOM.newDestination("test/mom/testTopicPublishFirst", DestinationType.TOPIC, ResolveMethod.DEFINE, null);
// Publish a message
MOM.publish(JmsTestMom.class, topic, "hello world");
// Subscribe for the destination
final CountDownLatch latch = new CountDownLatch(1);
m_disposables.add(MOM.subscribe(JmsTestMom.class, topic, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
latch.countDown();
}
}));
// Verify
assertFalse(latch.await(200, TimeUnit.MILLISECONDS));
}
use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.
the class JmsMomImplementorTest method testTimeToLive.
@Test
@Times(10)
public void testTimeToLive() throws InterruptedException {
IDestination<String> queue = MOM.newDestination("test/mom/testTimeToLive", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
final CountDownLatch latch = new CountDownLatch(1);
// Publish a message
MOM.publish(JmsTestMom.class, queue, "hello world", MOM.newPublishInput().withTimeToLive(1, TimeUnit.MILLISECONDS));
Thread.sleep(100);
// Subscribe for the destination
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
latch.countDown();
}
}));
// Verify
// expect the message not to be received
assertFalse(latch.await(50, TimeUnit.MILLISECONDS));
}
Aggregations