@Test
public void testLedgerRecovery(){
/*
* Instantiate BookKeeper object.
*/
BookKeeper bk = null;
try{
bk = new BookKeeper(HOSTPORT);
} catch (KeeperException ke){
LOG.error("Error instantiating BookKeeper", ke);
fail("ZooKeeper error");
} catch (IOException ioe){
LOG.error(ioe);
fail("Failure due to IOException");
}
/*
* Create ledger.
*/
LedgerHandle beforelh = null;
try{
beforelh = bk.createLedger("".getBytes());
} catch (KeeperException ke){
LOG.error("Error creating a ledger", ke);
fail("ZooKeeper error");
} catch (BKException bke){
LOG.error("BookKeeper error");
fail("BookKeeper error");
} catch (InterruptedException ie) {
LOG.error(ie);
fail("Failure due to interrupted exception");
} catch (IOException ioe) {
LOG.error(ioe);
fail("Failure due to IO exception");
}
/*
* Write a 1000 entries.
*/
try{
String tmp = "BookKeeper is cool!";
for(int i = 0; i < 1000; i++){
beforelh.addEntry(tmp.getBytes());
}
//bk.resetLedger(beforelh);
} catch(InterruptedException e){
LOG.error("Interrupted when adding entry", e);
fail("Couldn't finish adding entries");
} catch(BKException e){
LOG.error("BookKeeper exception", e);
fail("BookKeeper exception while adding entries");
}
/*
* Try to open ledger.
*/
try{
LedgerHandle afterlh = bk.openLedger(beforelh.getId(), "".getBytes());
/*
* Check if has recovered properly.
*/
assertTrue("Has not recovered correctly: " + afterlh.getLast(), afterlh.getLast() == 999);