Search in sources :

Example 6 with BKClientClosedException

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

the class BookKeeperCloseTest method testCreateLedger.

/**
 * Test that createledger using bookkeeper client which is closed should
 * throw ClientClosedException.
 */
@Test
public void testCreateLedger() throws Exception {
    BookKeeper bk = new BookKeeper(baseClientConf, zkc);
    LOG.info("Closing bookkeeper client");
    bk.close();
    try {
        bk.createLedger(digestType, PASSWORD.getBytes());
        fail("should have failed, client is closed");
    } catch (BKClientClosedException e) {
    // correct
    }
    // using async, because this could trigger an assertion
    final AtomicInteger returnCode = new AtomicInteger(0);
    final CountDownLatch openLatch = new CountDownLatch(1);
    CreateCallback cb = new CreateCallback() {

        @Override
        public void createComplete(int rc, LedgerHandle lh, Object ctx) {
            returnCode.set(rc);
            openLatch.countDown();
        }
    };
    bk.asyncCreateLedger(3, 2, digestType, PASSWORD.getBytes(), cb, openLatch);
    LOG.info("Waiting to finish the ledger creation");
    // wait for creating the ledger
    assertTrue("create ledger call should have completed", openLatch.await(20, TimeUnit.SECONDS));
    assertEquals("Succesfully created ledger through closed bkclient!", BKException.Code.ClientClosedException, returnCode.get());
}
Also used : CreateCallback(org.apache.bookkeeper.client.AsyncCallback.CreateCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with BKClientClosedException

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

the class BookKeeperCloseTest method testFenceLedger.

/**
 * Test that opening a ledger using bookkeeper client which is closed should
 * throw ClientClosedException.
 */
@Test
public void testFenceLedger() throws Exception {
    BookKeeper bk = new BookKeeper(baseClientConf, zkc);
    LOG.info("Create ledger and add entries to it");
    LedgerHandle lh = createLedgerWithEntries(bk, 100);
    LOG.info("Closing bookkeeper client");
    restartBookieSlow();
    bk.close();
    try {
        bk.openLedger(lh.getId(), digestType, PASSWORD.getBytes());
        fail("should have failed, client is closed");
    } catch (BKClientClosedException e) {
    // correct
    }
    try {
        bk.openLedgerNoRecovery(lh.getId(), digestType, PASSWORD.getBytes());
        fail("should have failed, client is closed");
    } catch (BKClientClosedException e) {
    // correct
    }
    final AtomicInteger returnCode = new AtomicInteger(0);
    final CountDownLatch openLatch = new CountDownLatch(1);
    AsyncCallback.OpenCallback cb = new AsyncCallback.OpenCallback() {

        public void openComplete(int rc, LedgerHandle lh, Object ctx) {
            returnCode.set(rc);
            openLatch.countDown();
        }
    };
    bk.asyncOpenLedger(lh.getId(), digestType, PASSWORD.getBytes(), cb, openLatch);
    LOG.info("Waiting to open the ledger asynchronously");
    assertTrue("Open call should have completed", openLatch.await(20, TimeUnit.SECONDS));
    assertTrue("Open should not have succeeded through closed bkclient!", BKException.Code.ClientClosedException == returnCode.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BKClientClosedException (org.apache.bookkeeper.client.BKException.BKClientClosedException)7 Test (org.junit.Test)7 Enumeration (java.util.Enumeration)1 AddCallback (org.apache.bookkeeper.client.AsyncCallback.AddCallback)1 CloseCallback (org.apache.bookkeeper.client.AsyncCallback.CloseCallback)1 CreateCallback (org.apache.bookkeeper.client.AsyncCallback.CreateCallback)1 ReadCallback (org.apache.bookkeeper.client.AsyncCallback.ReadCallback)1