*/
private void setupChaosThreads(final List<Thread> threads,
final AtomicLong finishedThreads) {
final List<TopologyView> views = new ArrayList<TopologyView>();
// register topology listener
final ServiceRegistration reg = this.bc.registerService(TopologyEventListener.class.getName(), new TopologyEventListener() {
@Override
public void handleTopologyEvent(final TopologyEvent event) {
if ( event.getType() == Type.TOPOLOGY_INIT ) {
views.add(event.getNewView());
}
}
}, null);
while ( views.isEmpty() ) {
this.sleep(10);
}
reg.unregister();
final TopologyView view = views.get(0);
try {
final ServiceReference[] refs = this.bc.getServiceReferences(TopologyEventListener.class.getName(),
"(objectClass=org.apache.sling.event.impl.jobs.config.JobManagerConfiguration)");
assertNotNull(refs);
assertEquals(1, refs.length);
final TopologyEventListener tel = (TopologyEventListener)bc.getService(refs[0]);
threads.add(new Thread() {
private final Random random = new Random();
@Override
public void run() {
final long startTime = System.currentTimeMillis();
// this thread runs 30 seconds longer than the job creation thread
final long endTime = startTime + (DURATION +30) * 1000;
while ( System.currentTimeMillis() < endTime ) {
final int sleepTime = random.nextInt(25) + 15;
try {
Thread.sleep(sleepTime * 1000);
} catch ( final InterruptedException ie) {
Thread.currentThread().interrupt();
}
tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGING, view, null));
final int changingTime = random.nextInt(20) + 3;
try {
Thread.sleep(changingTime * 1000);
} catch ( final InterruptedException ie) {
Thread.currentThread().interrupt();
}
tel.handleTopologyEvent(new TopologyEvent(Type.TOPOLOGY_CHANGED, view, view));
}
tel.getClass().getName();
finishedThreads.incrementAndGet();
}
});
} catch (InvalidSyntaxException e) {
e.printStackTrace();