Search in sources :

Example 1 with BKLedgerClosedException

use of org.apache.bookkeeper.client.BKException.BKLedgerClosedException in project bookkeeper by apache.

the class BookieWriteLedgerTest method testLedgerHandleAdvFunctionality.

/*
     * Verify the functionality of Advanced Ledger which accepts ledgerId as
     * input and returns LedgerHandleAdv. LedgerHandleAdv takes entryId for
     * addEntry, and let user manage entryId allocation.
     * This testcase is mainly added for covering missing code coverage branches
     * in LedgerHandleAdv
     *
     * @throws Exception
     */
@Test
public void testLedgerHandleAdvFunctionality() throws Exception {
    // Create a ledger
    long ledgerId = 0xABCDEF;
    lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);
    numEntriesToWrite = 3;
    ByteBuffer entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    lh.addEntry(0, entry.array());
    // here asyncAddEntry(final long entryId, final byte[] data, final
    // AddCallback cb, final Object ctx) method is
    // called which is not covered in any other testcase
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    CountDownLatch latch = new CountDownLatch(1);
    final int[] returnedRC = new int[1];
    lh.asyncAddEntry(1, entry.array(), new AddCallback() {

        @Override
        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            returnedRC[0] = rc;
            latch.countDown();
        }
    }, latch);
    latch.await();
    assertTrue("Returned code is expected to be OK", returnedRC[0] == BKException.Code.OK);
    // here addEntry is called with incorrect offset and length
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    try {
        lh.addEntry(2, entry.array(), -3, 9);
        fail("AddEntry is called with negative offset and incorrect length," + "so it is expected to throw RuntimeException/IndexOutOfBoundsException");
    } catch (RuntimeException exception) {
    // expected RuntimeException/IndexOutOfBoundsException
    }
    // here addEntry is called with corrected offset and length and it is
    // supposed to succeed
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    lh.addEntry(2, entry.array());
    // LedgerHandle is closed for write
    lh.close();
    // here addEntry is called even after the close of the LedgerHandle, so
    // it is expected to throw exception
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    try {
        lh.addEntry(3, entry.array());
        fail("AddEntry is called after the close of LedgerHandle," + "so it is expected to throw BKLedgerClosedException");
    } catch (BKLedgerClosedException exception) {
    }
    readEntries(lh, entries1);
    bkc.deleteLedger(ledgerId);
}
Also used : AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) BKLedgerClosedException(org.apache.bookkeeper.client.BKException.BKLedgerClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AddCallback (org.apache.bookkeeper.client.AsyncCallback.AddCallback)1 BKLedgerClosedException (org.apache.bookkeeper.client.BKException.BKLedgerClosedException)1 Test (org.junit.Test)1