use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testDependentTaskExecution.
@Test
@SuppressWarnings("unchecked")
public void testDependentTaskExecution() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
String resultValue = "dependent-task-initiator";
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.CREATED);
filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
filter.setFrom(new DateTime());
List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(0L, createdLrts.size());
//
Task task = createTask("dependent-task-initiator");
DependentTaskTrigger trigger = new DependentTaskTrigger();
trigger.setInitiatorTaskId(task.getId());
//
// initiator = dependent task => circular execution
manager.createTrigger(task.getId(), trigger);
manager.runTask(task.getId());
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, createdLrts.size());
// execute first task
LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
//
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
//
longRunningTaskManager.cancel(createdLrts.get(0).getId());
//
longRunningTaskService.find(filter, null).getContent();
// cancel - clean up
Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testDependentTaskInDryModeExecution.
@Test
@SuppressWarnings("unchecked")
public void testDependentTaskInDryModeExecution() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
String resultValue = "dependent-task-initiator-dry-run";
//
IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
filter.setOperationState(OperationState.CREATED);
filter.setTaskType(TestSchedulableDryRunTask.class.getCanonicalName());
filter.setFrom(new DateTime());
List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(0L, createdLrts.size());
//
Task task = createDryRunTask(resultValue);
DependentTaskTrigger trigger = new DependentTaskTrigger();
trigger.setInitiatorTaskId(task.getId());
//
// initiator = dependent task => circular execution
manager.createTrigger(task.getId(), trigger);
manager.runTask(task.getId(), true);
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, createdLrts.size());
Assert.assertTrue(createdLrts.get(0).isDryRun());
UUID firstTaskId = createdLrts.get(0).getId();
// execute first task
LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
//
helper.waitForResult(getContinueFunction());
createdLrts = longRunningTaskService.find(filter, null).getContent();
Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
Assert.assertTrue(createdLrts.get(0).isDryRun());
Assert.assertNotEquals(firstTaskId, createdLrts.get(0).getId());
//
longRunningTaskManager.cancel(createdLrts.get(0).getId());
//
longRunningTaskService.find(filter, null).getContent();
// cancel - clean up
Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
use of java.util.concurrent.FutureTask in project CzechIdMng by bcvsolutions.
the class DefaultSchedulerManagerIntegrationTest method testCreateAndRunRoleExpirationTask.
@Test
public void testCreateAndRunRoleExpirationTask() throws Exception {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
try {
Task task = createRoleExpirationTask();
//
manager.createTrigger(task.getId(), getSimpleTrigger(task));
//
helper.waitForResult(getContinueFunction());
//
List<FutureTask<?>> taskList = getFutureTaskList(IdentityRoleExpirationTaskExecutor.class);
assertEquals(Boolean.TRUE, taskList.get(0).get());
//
checkScheduledTask(task);
} finally {
helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
}
}
use of java.util.concurrent.FutureTask in project cxf by apache.
the class JAXRSOverlappingDestinationsTest method testAbsolutePathOneAndTwo.
@Test
public void testAbsolutePathOneAndTwo() throws Exception {
final String requestURI = "http://localhost:" + PORT + "/one/bookstore/request?delay";
Callable<String> callable = new Callable<String>() {
public String call() {
WebClient wc = WebClient.create(requestURI);
return wc.accept("text/plain").get(String.class);
}
};
FutureTask<String> task = new FutureTask<String>(callable);
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(task);
Thread.sleep(1000);
Runnable runnable = new Runnable() {
public void run() {
try {
testAbsolutePathTwo();
} catch (Exception ex) {
throw new RuntimeException("Concurrent testAbsolutePathTwo failed");
}
}
};
new Thread(runnable).start();
Thread.sleep(2000);
String path = task.get();
assertEquals("Absolute RequestURI is wrong", requestURI, path);
}
use of java.util.concurrent.FutureTask in project hive by apache.
the class TestLowLevelCacheImpl method testMTTWithCleanup.
@Test
public void testMTTWithCleanup() {
final LowLevelCacheImpl cache = new LowLevelCacheImpl(LlapDaemonCacheMetrics.create("test", "1"), new DummyCachePolicy(), new DummyAllocator(), true, 1);
final long fn1 = 1, fn2 = 2;
final int offsetsToUse = 8;
final CountDownLatch cdlIn = new CountDownLatch(4), cdlOut = new CountDownLatch(1);
final AtomicInteger rdmsDone = new AtomicInteger(0);
Callable<Long> rdmCall = new Callable<Long>() {
public Long call() {
int gets = 0, puts = 0;
try {
Random rdm = new Random(1234 + Thread.currentThread().getId());
syncThreadStart(cdlIn, cdlOut);
for (int i = 0; i < 20000; ++i) {
boolean isGet = rdm.nextBoolean(), isFn1 = rdm.nextBoolean();
long fileName = isFn1 ? fn1 : fn2;
int fileIndex = isFn1 ? 1 : 2;
int count = rdm.nextInt(offsetsToUse);
if (isGet) {
int[] offsets = new int[count];
count = generateOffsets(offsetsToUse, rdm, offsets);
CreateHelper list = new CreateHelper();
for (int j = 0; i < count; ++i) {
list.addOrMerge(offsets[j], offsets[j] + 1, true, false);
}
DiskRangeList iter = cache.getFileData(fileName, list.get(), 0, testFactory, null, null);
int j = -1;
while (iter != null) {
++j;
if (!(iter instanceof CacheChunk)) {
iter = iter.next;
continue;
}
++gets;
LlapAllocatorBuffer result = (LlapAllocatorBuffer) ((CacheChunk) iter).getBuffer();
assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), result.getArenaIndex());
cache.decRefBuffer(result);
iter = iter.next;
}
} else {
DiskRange[] ranges = new DiskRange[count];
int[] offsets = new int[count];
for (int j = 0; j < count; ++j) {
int next = rdm.nextInt(offsetsToUse);
ranges[j] = dr(next, next + 1);
offsets[j] = next;
}
MemoryBuffer[] buffers = new MemoryBuffer[count];
for (int j = 0; j < offsets.length; ++j) {
LlapDataBuffer buf = LowLevelCacheImpl.allocateFake();
buf.setNewAllocLocation(makeFakeArenaIndex(fileIndex, offsets[j]), 0);
buffers[j] = buf;
}
long[] mask = cache.putFileData(fileName, ranges, buffers, 0, Priority.NORMAL, null, null);
puts += buffers.length;
long maskVal = 0;
if (mask != null) {
assertEquals(1, mask.length);
maskVal = mask[0];
}
for (int j = 0; j < offsets.length; ++j) {
LlapDataBuffer buf = (LlapDataBuffer) (buffers[j]);
if ((maskVal & 1) == 1) {
assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), buf.getArenaIndex());
}
maskVal >>= 1;
cache.decRefBuffer(buf);
}
}
}
} finally {
rdmsDone.incrementAndGet();
}
return (((long) gets) << 32) | puts;
}
private int makeFakeArenaIndex(int fileIndex, long offset) {
return (int) ((fileIndex << 12) + offset);
}
};
FutureTask<Integer> evictionTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() {
boolean isFirstFile = false;
Random rdm = new Random(1234 + Thread.currentThread().getId());
int evictions = 0;
syncThreadStart(cdlIn, cdlOut);
while (rdmsDone.get() < 3) {
DiskRangeList head = new DiskRangeList(0, offsetsToUse + 1);
isFirstFile = !isFirstFile;
long fileId = isFirstFile ? fn1 : fn2;
head = cache.getFileData(fileId, head, 0, testFactory, null, null);
DiskRange[] results = head.listToArray();
int startIndex = rdm.nextInt(results.length), index = startIndex;
LlapDataBuffer victim = null;
do {
DiskRange r = results[index];
if (r instanceof CacheChunk) {
LlapDataBuffer result = (LlapDataBuffer) ((CacheChunk) r).getBuffer();
cache.decRefBuffer(result);
if (victim == null && result.invalidate() == LlapCacheableBuffer.INVALIDATE_OK) {
++evictions;
victim = result;
}
}
++index;
if (index == results.length)
index = 0;
} while (index != startIndex);
if (victim == null)
continue;
cache.notifyEvicted(victim);
}
return evictions;
}
});
FutureTask<Long> rdmTask1 = new FutureTask<Long>(rdmCall), rdmTask2 = new FutureTask<Long>(rdmCall), rdmTask3 = new FutureTask<Long>(rdmCall);
Executor threadPool = Executors.newFixedThreadPool(4);
threadPool.execute(rdmTask1);
threadPool.execute(rdmTask2);
threadPool.execute(rdmTask3);
threadPool.execute(evictionTask);
try {
cdlIn.await();
cdlOut.countDown();
long result1 = rdmTask1.get(), result2 = rdmTask2.get(), result3 = rdmTask3.get();
int evictions = evictionTask.get();
LOG.info("MTT test: task 1: " + descRdmTask(result1) + ", task 2: " + descRdmTask(result2) + ", task 3: " + descRdmTask(result3) + "; " + evictions + " evictions");
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
Aggregations