public void testCursorTruncate() throws IOException, InterruptedException {
// normal implementation uses synchronous queue, but we use array blocking
// queue for single threaded testing
BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(10);
File f = createDataFile(5);
Cursor c = new Cursor(q, f);
assertTrue(c.tailBody()); // open file the file
assertEquals(0, c.lastChannelPos);
assertTrue(null != c.in);
assertTrue(c.tailBody()); // finish reading the file
assertTrue(0 != c.lastChannelSize);
assertTrue(null != c.in);
assertFalse(c.tailBody()); // attempt to open file again.
assertEquals(5, q.size()); // should be 5 in queue.
// truncate the file -- there will be 1 full event and one unproperly closed
// event.
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(10);
raf.close();
assertFalse(c.tailBody()); // detect file truncation, no data to read
assertEquals(5, q.size()); // should be 5 in queue.
assertEquals(10, c.lastChannelPos);
assertTrue(null != c.in);
assertFalse(c.tailBody()); // no data to read
assertEquals(5, q.size()); // should be 5 in queue.
appendData(f, 5, 5); // appending data after truncation
assertTrue(c.tailBody()); // reading appended data
assertEquals(10, q.size()); // should be 5 + 5 in queue.
assertFalse(c.tailBody()); // no data to read
assertEquals(10, q.size()); // should be 5 + 5 in queue.
}