public void testInvalidate() throws Exception {
final int MAX_ACTIVE = 3;
final int MAX_WAIT_MILLISECONDS = 100;
cleanUpOldFiles(PREFIX);
WriterPool pool = new ARCWriterPool(getSettings(true),
MAX_ACTIVE, MAX_WAIT_MILLISECONDS);
WriterPoolMember [] writers = new WriterPoolMember[MAX_ACTIVE];
final String CONTENT = "Any old content";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(CONTENT.getBytes());
for (int i = 0; i < MAX_ACTIVE; i++) {
writers[i] = pool.borrowFile();
assertEquals("Number active", i + 1, pool.getNumActive());
((ARCWriter)writers[i]).write("http://one.two.three", "no-type",
"0.0.0.0", 1234567890, CONTENT.length(), baos);
}
WriterPoolMember writer2Invalidate = writers[pool.getNumActive() - 1];
writers[pool.getNumActive() - 1] = null;
pool.invalidateFile(writer2Invalidate);
for (int i = 0; i < (MAX_ACTIVE - 1); i++) {
if (writers[i] == null) {
continue;
}
pool.returnFile(writers[i]);
}
for (int i = 0; i < MAX_ACTIVE; i++) {
writers[i] = pool.borrowFile();
assertEquals("Number active", i + 1, pool.getNumActive());
((ARCWriter)writers[i]).write("http://one.two.three", "no-type",
"0.0.0.0", 1234567890, CONTENT.length(), baos);
}
for (int i = (MAX_ACTIVE - 1); i >= 0; i--) {
pool.returnFile(writers[i]);
assertEquals("Number active", i, pool.getNumActive());
assertEquals("Number idle", MAX_ACTIVE - pool.getNumActive(),
pool.getNumIdle());
}
pool.close();
}