@Test public void testTruncateAll() throws Exception { Log log = getLog(); // Appends 100 transactions. appendTxns(log, new Zxid(0, 0), 100); log.truncate(new Zxid(0, -1)); appendTxns(log, new Zxid(0, 0), 100); }

@Test public void testTruncateFile() throws Exception { Log log = getLog(); // Appends 100 transactions. appendTxns(log, new Zxid(0, 0), 100); // Make sure latest zxid is <0, 99>. Assert.assertEquals(new Zxid(0, 99), log.getLatestZxid()); int truncIdx = new Random().nextInt(100); log.truncate(new Zxid(0, truncIdx)); // Make sure latest zxid is <0, truncIdx>. Assert.assertEquals(new Zxid(0, truncIdx), log.getLatestZxid()); }

@Test public void testTruncateAndAppend() throws Exception { Log log = getLog(); // Appends 100 transactions. appendTxns(log, new Zxid(0, 0), 100); // Make sure latest zxid is <0, 99>. Assert.assertEquals(new Zxid(0, 99), log.getLatestZxid()); // Truncates file after <0, 77> log.truncate(new Zxid(0, 77)); // Make sure latest zxid is <0, 77>. Assert.assertEquals(new Zxid(0, 77), log.getLatestZxid()); // Appends 22 more transactions. appendTxns(log, new Zxid(0, 78), 22); List<Zxid> zxids = getZxids(log, Zxid.ZXID_NOT_EXIST); // Make sure latest zxid is <0, 99> Assert.assertEquals(new Zxid(0, 99), log.getLatestZxid()); // Check all the transactions are in log. Assert.assertEquals(100, zxids.size()); }

@Test public void testDivergingPoint() throws Exception { /* * case 1: * the log : <0, 0>, <0, 1>, <1, 1> * zxid : <0, 2> * returns (<0, 1>, iter points to <1, 1>) */ Log log = getLog(); appendTxns(log, new Zxid(0, 0), 2); appendTxns(log, new Zxid(1, 1), 1); Log.DivergingTuple dp = log.firstDivergingPoint(new Zxid(0, 2)); Assert.assertEquals(new Zxid(0, 1), dp.getDivergingZxid()); Assert.assertEquals(new Zxid(1, 1), dp.getIterator().next().getZxid()); /* * case 2: * the log : <0, 0>, <0, 1>, <1, 1> * zxid : <0, 1> * returns (<0, 1>, iter points to <1, 1>) */ dp = log.firstDivergingPoint(new Zxid(0, 1)); Assert.assertEquals(new Zxid(0, 1), dp.getDivergingZxid()); Assert.assertEquals(new Zxid(1, 1), dp.getIterator().next().getZxid()); /* * case 3: * the log : <0, 0>, <0, 1>, <1, 1> * zxid : <1, 2> * returns (<1, 1>, iter points to end of the log) */ dp = log.firstDivergingPoint(new Zxid(1, 2)); Assert.assertEquals(new Zxid(1, 1), dp.getDivergingZxid()); Assert.assertFalse(dp.getIterator().hasNext()); /* * case 4: * the log : <0, 2> * zxid : <0, 1> * returns (<0, -1>, iter points <0, 2>) */ log.truncate(Zxid.ZXID_NOT_EXIST); appendTxns(log, new Zxid(0, 2), 1); dp = log.firstDivergingPoint(new Zxid(0, 1)); Assert.assertEquals(Zxid.ZXID_NOT_EXIST, dp.getDivergingZxid()); Assert.assertEquals(new Zxid(0, 2), dp.getIterator().next().getZxid()); /* * case 5: * the log : <0, 2> * zxid : <0, -1> * returns (<0, -1>, iter points <0, 2>) */ dp = log.firstDivergingPoint(Zxid.ZXID_NOT_EXIST); Assert.assertEquals(Zxid.ZXID_NOT_EXIST, dp.getDivergingZxid()); Assert.assertEquals(new Zxid(0, 2), dp.getIterator().next().getZxid()); /* * case 6: * the log : empty * zxid : <0, 1> * returns (<0, -1>, iter points to end of the log) */ log.truncate(Zxid.ZXID_NOT_EXIST); dp = log.firstDivergingPoint(new Zxid(0, 1)); Assert.assertEquals(Zxid.ZXID_NOT_EXIST, dp.getDivergingZxid()); Assert.assertFalse(dp.getIterator().hasNext()); }