}
@Test
public void testCleanupFrequency() {
SchedulerService scheduler = new SchedulerService(1, time);
try {
Date now = new Date();
// clean up will purge everything older than last 2 seconds
Runnable cleanupJob = new DataCleanupJob<ByteArray, byte[], byte[]>(engine,
new ScanPermitWrapper(1),
2 * Time.MS_PER_SECOND,
SystemTime.INSTANCE,
new EventThrottler(1),
null);
// and will run every 5 seconds starting now
scheduler.schedule("cleanup-freq-test", cleanupJob, now, 5 * Time.MS_PER_SECOND);
// load some data
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
engine.put(b, new Versioned<byte[]>(b.get()), null);
}
// sleep for 2 seconds
Thread.sleep(2 * Time.MS_PER_SECOND);
// None of the keys should have been deleted, i.e data cleanup
// should n't have run.
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Did not find key '" + i + "' in store!", found.size() > 0);
}
// wait till 4 seconds from start
Thread.sleep(System.currentTimeMillis() - (now.getTime() + 4 * Time.MS_PER_SECOND));
// load some more data
for(int i = 10; i < 20; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
engine.put(b, new Versioned<byte[]>(b.get()), null);
}
// give time for data cleanup to finally run
Thread.sleep(System.currentTimeMillis() - (now.getTime() + 6 * Time.MS_PER_SECOND));
// first batch of writes should have been deleted
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Expected key '" + i + "' to be deleted!", found.size() == 0);
}
// and later ones retained.
for(int i = 10; i < 20; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Expected key '" + i + "' to be retained!", found.size() > 0);
}
} catch(Exception e) {
} finally {
scheduler.stop();
}
}