use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class JobHandlingDistributionQueueTest method testPackageAddition.
@SuppressWarnings("unchecked")
@Test
public void testPackageAddition() throws Exception {
JobManager jobManager = mock(JobManager.class);
JobBuilder builder = mock(JobBuilder.class);
when(builder.properties(any(Map.class))).thenReturn(builder);
Job job = mock(Job.class);
when(job.getId()).thenReturn("id-123");
when(builder.add()).thenReturn(job);
String topic = JobHandlingDistributionQueue.DISTRIBUTION_QUEUE_TOPIC + "/aname";
when(jobManager.createJob(topic)).thenReturn(builder);
when(jobManager.findJobs(JobManager.QueryType.ALL, topic, -1)).thenReturn(Collections.<Job>emptySet());
when(builder.properties(any(Map.class))).thenReturn(builder);
DistributionQueue queue = new JobHandlingDistributionQueue("aname", topic, jobManager, true, DistributionQueueType.ORDERED);
DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
DistributionQueueItem distributionQueueItem = new DistributionQueueItem("an-id", packageInfo);
assertNotNull(queue.add(distributionQueueItem));
}
use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class JobHandlingDistributionQueueProviderTest method testEnableQueueProcessing.
@Test
public void testEnableQueueProcessing() throws Exception {
JobManager jobManager = mock(JobManager.class);
ConfigurationAdmin configAdmin = mock(ConfigurationAdmin.class);
Configuration config = mock(Configuration.class);
when(configAdmin.createFactoryConfiguration(QueueConfiguration.class.getName(), null)).thenReturn(config);
BundleContext context = mock(BundleContext.class);
JobHandlingDistributionQueueProvider jobHandlingdistributionQueueProvider = new JobHandlingDistributionQueueProvider("dummy-agent", jobManager, context);
DistributionQueueProcessor queueProcessor = mock(DistributionQueueProcessor.class);
jobHandlingdistributionQueueProvider.enableQueueProcessing(queueProcessor);
}
use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class JobHandlingTest method testCancelJob.
/**
* Test canceling a job
* The job execution always fails
*/
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testCancelJob() throws Exception {
final Barrier cb = new Barrier(2);
final Barrier cb2 = new Barrier(2);
this.registerJobConsumer(TOPIC, new JobConsumer() {
@Override
public JobResult process(Job job) {
cb.block();
cb2.block();
return JobResult.FAILED;
}
});
final Map<String, Object> jobProperties = Collections.singletonMap("id", (Object) "cancelJobId");
@SuppressWarnings("unchecked") final Map<String, Object>[] jobPropertiesAsArray = new Map[1];
jobPropertiesAsArray[0] = jobProperties;
// create job
final JobManager jobManager = this.getJobManager();
jobManager.addJob(TOPIC, jobProperties);
cb.block();
assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
// job is currently waiting, therefore cancel fails
final Job e1 = jobManager.getJob(TOPIC, jobProperties);
assertNotNull(e1);
// and continue job
cb2.block();
sleep(200);
// the job is now in the queue again
final Job e2 = jobManager.getJob(TOPIC, jobProperties);
assertNotNull(e2);
assertTrue(jobManager.removeJobById(e2.getId()));
assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, jobPropertiesAsArray).size());
final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, jobPropertiesAsArray);
try {
assertEquals(1, col.size());
} finally {
for (final Job j : col) {
jobManager.removeJobById(j.getId());
}
}
}
use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class RoundRobinQueueTest method testRoundRobinQueue.
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testRoundRobinQueue() throws Exception {
final JobManager jobManager = this.getJobManager();
final Barrier cb = new Barrier(2);
this.registerJobConsumer(TOPIC + "/start", new JobConsumer() {
@Override
public JobResult process(final Job job) {
cb.block();
return JobResult.OK;
}
});
// register new consumer and event handle
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger parallelCount = new AtomicInteger(0);
final Set<Integer> maxParticipants = new HashSet<Integer>();
this.registerJobConsumer(TOPIC + "/*", new JobConsumer() {
@Override
public JobResult process(final Job job) {
final int max = parallelCount.incrementAndGet();
if (max > MAX_PAR) {
parallelCount.decrementAndGet();
return JobResult.FAILED;
}
synchronized (maxParticipants) {
maxParticipants.add(max);
}
sleep(job.getProperty("sleep", 30));
parallelCount.decrementAndGet();
return JobResult.OK;
}
});
this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {
@Override
public void handleEvent(final Event event) {
count.incrementAndGet();
}
});
// we first sent one event to get the queue started
jobManager.addJob(TOPIC + "/start", null);
assertTrue("No event received in the given time.", cb.block(5));
cb.reset();
// get the queue
final Queue q = jobManager.getQueue(QUEUE_NAME);
assertNotNull("Queue '" + QUEUE_NAME + "' should exist!", q);
// suspend it
q.suspend();
// we start "some" jobs:
for (int i = 0; i < NUM_JOBS; i++) {
final String subTopic = TOPIC + "/sub" + (i % 10);
final Map<String, Object> props = new HashMap<String, Object>();
if (i < 10) {
props.put("sleep", 300);
} else {
props.put("sleep", 30);
}
jobManager.addJob(subTopic, props);
}
// start the queue
q.resume();
while (count.get() < NUM_JOBS + 1) {
assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
sleep(300);
}
// we started one event before the test, so add one
assertEquals("Finished count", NUM_JOBS + 1, count.get());
assertEquals("Finished count", NUM_JOBS + 1, jobManager.getStatistics().getNumberOfFinishedJobs());
assertEquals("Finished count", NUM_JOBS + 1, q.getStatistics().getNumberOfFinishedJobs());
assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
for (int i = 1; i <= MAX_PAR; i++) {
assertTrue("# Participants " + String.valueOf(i) + " not in " + maxParticipants, maxParticipants.contains(i));
}
}
use of org.apache.sling.event.jobs.JobManager in project sling by apache.
the class ClassloadingTest method testSimpleClassloading.
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleClassloading() throws Exception {
final AtomicInteger processedJobsCount = new AtomicInteger(0);
final List<Event> finishedEvents = Collections.synchronizedList(new ArrayList<Event>());
final CountDownLatch latch = new CountDownLatch(1);
this.registerJobConsumer(TOPIC, new JobConsumer() {
@Override
public JobResult process(Job job) {
processedJobsCount.incrementAndGet();
return JobResult.OK;
}
});
this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {
@Override
public void handleEvent(Event event) {
finishedEvents.add(event);
latch.countDown();
}
});
final JobManager jobManager = this.getJobManager();
final List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
final Map<String, String> map = new HashMap<String, String>();
map.put("a", "a1");
map.put("b", "b2");
// we start a single job
final Map<String, Object> props = new HashMap<String, Object>();
props.put("string", "Hello");
props.put("int", new Integer(5));
props.put("long", new Long(7));
props.put("list", list);
props.put("map", map);
final String jobId = jobManager.addJob(TOPIC, props).getId();
try {
latch.await(5, TimeUnit.SECONDS);
assertFalse("At least one finished job", finishedEvents.isEmpty());
assertEquals(1, processedJobsCount.get());
final String jobTopic = (String) finishedEvents.get(0).getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
assertNotNull(jobTopic);
assertEquals("Hello", finishedEvents.get(0).getProperty("string"));
assertEquals(new Integer(5), Integer.valueOf(finishedEvents.get(0).getProperty("int").toString()));
assertEquals(new Long(7), Long.valueOf(finishedEvents.get(0).getProperty("long").toString()));
assertEquals(list, finishedEvents.get(0).getProperty("list"));
assertEquals(map, finishedEvents.get(0).getProperty("map"));
} finally {
jobManager.removeJobById(jobId);
}
}
Aggregations