final List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
final List<Thread> threads = new ArrayList<Thread>();
final AtomicLong finishedThreads = new AtomicLong();
final ServiceRegistration eventHandler = this.registerEventHandler("org/apache/sling/event/notification/job/*",
new EventHandler() {
@Override
public void handleEvent(final Event event) {
final String topic = (String) event.getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
if ( NotificationConstants.TOPIC_JOB_FINISHED.equals(event.getTopic())) {
finished.get(topic).incrementAndGet();
} else if ( NotificationConstants.TOPIC_JOB_ADDED.equals(event.getTopic())) {
added.get(topic).incrementAndGet();
}
}
});
try {
// setup job consumers
this.setupJobConsumers(registrations);
// setup job creation tests
this.setupJobCreationThreads(threads, jobManager, created, finishedThreads);
this.setupChaosThreads(threads, finishedThreads);
System.out.println("Starting threads...");
// start threads
for(final Thread t : threads) {
t.start();
}
System.out.println("Sleeping for " + DURATION + " seconds to wait for threads to finish...");
// for sure we can sleep for the duration
this.sleep(DURATION * 1000);
System.out.println("Polling for threads to finish...");
// wait until threads are finished
while ( finishedThreads.get() < threads.size() ) {
this.sleep(100);
}
System.out.println("Waiting for job handling to finish...");
final Set<String> allTopics = new HashSet<String>(topics);
while ( !allTopics.isEmpty() ) {
final Iterator<String> iter = allTopics.iterator();
while ( iter.hasNext() ) {
final String topic = iter.next();
if ( finished.get(topic).get() == created.get(topic).get() ) {
iter.remove();
}
}
this.sleep(100);
}
/* We could try to enable this with Oak again - but right now JR observation handler is too
* slow.
System.out.println("Checking notifications...");
for(final String topic : topics) {
assertEquals("Checking topic " + topic, created.get(topic).get(), added.get(topic).get());
}
*/
} finally {
eventHandler.unregister();
for(final ServiceRegistration reg : registrations) {
reg.unregister();
}
}