use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testChangePermits2.
/**
* Tests no permit available upon schedule
*/
@Test
public void testChangePermits2() throws InterruptedException {
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(10);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
// job-1
IFuture<Void> future1 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
latch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-1").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
assertTrue(latch.await());
assertEquals(CollectionUtility.hashSet("job-1-running"), protocol);
protocol.clear();
// Change permits to zero.
semaphore.withPermits(0);
latch.unblock();
future1.awaitDone();
// job-2
IFuture<Void> future2 = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-2-running");
}
}, Jobs.newInput().withName("job-2").withExecutionSemaphore(semaphore));
SleepUtil.sleepSafe(1, TimeUnit.SECONDS);
assertTrue(protocol.isEmpty());
assertFalse(future2.isDone());
// Change permits to 1 --> job should run
semaphore.withPermits(1);
future2.awaitDone(10, TimeUnit.SECONDS);
assertEquals(CollectionUtility.hashSet("job-2-running"), protocol);
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testZeroPermits.
@Test
public void testZeroPermits() {
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(0);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-running");
}
}, Jobs.newInput().withName("job").withExecutionSemaphore(semaphore));
SleepUtil.sleepSafe(1, TimeUnit.SECONDS);
assertTrue(protocol.isEmpty());
assertFalse(future.isDone());
// Change permits to 1 --> job should start running
semaphore.withPermits(1);
future.awaitDone(10, TimeUnit.SECONDS);
assertEquals(CollectionUtility.hashSet("job-running"), protocol);
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testSealSemaphore.
@Test(expected = AssertionException.class)
public void testSealSemaphore() {
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1).seal();
semaphore.withPermits(2);
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testChangePermits4.
/**
* Test with change of permits to serial execution.
*/
@Test
// regression
@Times(500)
public void testChangePermits4() 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 setupLatch = new BlockingCountDownLatch(3);
// job-1
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
setupLatch.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");
setupLatch.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();
}
}, Jobs.newInput().withName("job-3").withExecutionSemaphore(semaphore));
// job-4
final BlockingCountDownLatch latchJob4 = new BlockingCountDownLatch(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-4-running");
latchJob4.countDownAndBlock();
}
}, Jobs.newInput().withName("job-4").withExecutionSemaphore(semaphore));
// job-5
final BlockingCountDownLatch latchJob5 = new BlockingCountDownLatch(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-5-running");
latchJob5.countDownAndBlock();
}
}, Jobs.newInput().withName("job-5").withExecutionSemaphore(semaphore));
// job-6
final BlockingCountDownLatch latchJob6 = new BlockingCountDownLatch(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-6-running");
latchJob6.countDownAndBlock();
}
}, Jobs.newInput().withName("job-6").withExecutionSemaphore(semaphore));
assertTrue(setupLatch.await());
assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running", "job-3-running"), protocol);
protocol.clear();
// serial execution
semaphore.withPermits(1);
setupLatch.unblock();
latchJob4.await();
assertEquals(CollectionUtility.hashSet("job-4-running"), protocol);
protocol.clear();
latchJob4.unblock();
latchJob5.await();
assertEquals(CollectionUtility.hashSet("job-5-running"), protocol);
protocol.clear();
latchJob5.unblock();
latchJob6.await();
assertEquals(CollectionUtility.hashSet("job-6-running"), protocol);
protocol.clear();
latchJob6.unblock();
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ExecutionSemaphoreTest method testChangePermits3.
/**
* Tests multiple permits available after permit change.
*/
@Test
public void testChangePermits3() throws InterruptedException {
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(0);
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(3);
// job-1
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-1-running");
finishLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-1").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
// job-2
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-2-running");
finishLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-2").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
// job-3
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("job-3-running");
finishLatch.countDownAndBlock();
}
}, Jobs.newInput().withName("job-3").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
// job-1, job-2, job-3
JobTestUtil.waitForPermitCompetitors(semaphore, 3);
semaphore.withPermits(3);
assertTrue(finishLatch.await());
assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running", "job-3-running"), protocol);
finishLatch.unblock();
}
Aggregations