}
@Test
public void saturateInvokers() {
s_logger.info("saturateInvokers");
final JobDispatcher jobDispatcher = new JobDispatcher();
final JobInvoker[] jobInvokers = new JobInvoker[3];
for (int i = 0; i < jobInvokers.length; i++) {
jobDispatcher.registerJobInvoker(new AbstractJobInvoker("" + (i + 1)) {
private final Random rnd = new Random();
private boolean _busy;
private JobInvokerRegister _callback;
@Override
public boolean invoke(final CalculationJob job, final JobInvocationReceiver receiver) {
final JobInvoker instance = this;
synchronized (instance) {
if (_busy) {
return false;
}
_executorService.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(rnd.nextInt(50));
} catch (InterruptedException e) {
s_logger.warn("invoker {} interrupted", getInvokerId ());
}
s_logger.debug("invoker {} completed job {}", getInvokerId (), job.getSpecification());
receiver.jobCompleted(createTestJobResult(job.getSpecification(), 0L, instance.toString()));
synchronized (instance) {
_busy = false;
if (_callback != null) {
s_logger.debug("re-registering invoker {} with dispatcher", getInvokerId ());
final JobInvokerRegister callback = _callback;
_callback = null;
callback.registerJobInvoker(instance);
} else {
s_logger.debug("invoker {} completed job without notify", getInvokerId ());
}
}
}
});
_busy = true;
return true;
}
}
@Override
public boolean notifyWhenAvailable(final JobInvokerRegister callback) {
synchronized (this) {
if (_busy) {
assertNull(_callback);
s_logger.debug("invoker {} busy - storing callback", getInvokerId ());
_callback = callback;
return false;
} else {
s_logger.debug("invoker {} ready - immediate callback", getInvokerId ());
return true;
}
}
}
});
}
final CalculationJob[] jobs = new CalculationJob[100];
final TestJobResultReceiver[] resultReceivers = new TestJobResultReceiver[jobs.length];
s_logger.debug("Dispatching {} jobs to {} nodes", jobs.length, jobInvokers.length);
for (int i = 0; i < jobs.length; i++) {
jobDispatcher.dispatchJob(jobs[i] = createTestJob(), resultReceivers[i] = new TestJobResultReceiver());
}
s_logger.debug("Jobs dispatched");
for (int i = 0; i < jobs.length; i++) {
s_logger.debug("Waiting for result {}", i);
final CalculationJobResult result = resultReceivers[i].waitForResult(TIMEOUT * 2);