final Map<String, Object> attributes = Maps.newHashMap();
attributes.put(AttributeNames.QUERY, getSmallQueryText());
attributes.put(AttributeNames.RESULTS, getSmallQuerySize());
// Cache results from all DataSources
final Controller controller =
getCachingController(initAttributes, IDocumentSource.class);
int count = 3;
final ExecutorService executorService = Executors.newFixedThreadPool(count);
try {
List<Callable<ProcessingResult>> callables = Lists.newArrayList();
for (int i = 0; i < count; i++)
{
callables.add(new Callable<ProcessingResult>()
{
public ProcessingResult call() throws Exception
{
Map<String, Object> localAttributes = Maps.newHashMap(attributes);
return controller.process(localAttributes, getComponentClass());
}
});
}
final List<Future<ProcessingResult>> results = executorService.invokeAll(callables);
List<Document> documents = null;
int index = 0;
for (Future<ProcessingResult> future : results)
{
ProcessingResult processingResult = future.get();
final List<Document> documentsLocal = (List<Document>) processingResult
.getAttributes().get(AttributeNames.DOCUMENTS);
assertThat(documentsLocal).as("documents at " + index).isNotNull();
if (!canReturnMoreResultsThanRequested())
{
assertThat(documentsLocal.size()).as("documents.size() at " + index)
.isLessThanOrEqualTo(getSmallQuerySize());
}
assertThat(documentsLocal.size()).as("documents.size() at " + index)
.isGreaterThanOrEqualTo(getSmallQuerySize() / 2);
// Should have same documents (from the cache)
if (documents != null)
{
for (int i = 0; i < documents.size(); i++)
{
assertSame(documents.get(i), documentsLocal.get(i));
}
}
documents = documentsLocal;
index++;
}
} finally {
controller.dispose();
executorService.shutdown();
}
}