Search in sources :

Example 1 with BKBookieHandleNotAvailableException

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

the class BookKeeperTest method testReadFailureCallback.

@Test
public void testReadFailureCallback() throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper bkc = new BookKeeper(conf);
    LedgerHandle lh = bkc.createLedger(digestType, "testPasswd".getBytes());
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        lh.addEntry(("entry-" + i).getBytes());
    }
    stopBKCluster();
    try {
        lh.readEntries(0, numEntries - 1);
        fail("Read operation should have failed");
    } catch (BKBookieHandleNotAvailableException e) {
    // expected
    }
    final CountDownLatch counter = new CountDownLatch(1);
    final AtomicInteger receivedResponses = new AtomicInteger(0);
    final AtomicInteger returnCode = new AtomicInteger();
    lh.asyncReadEntries(0, numEntries - 1, new ReadCallback() {

        @Override
        public void readComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> seq, Object ctx) {
            returnCode.set(rc);
            receivedResponses.incrementAndGet();
            counter.countDown();
        }
    }, null);
    counter.await();
    // Wait extra time to ensure no extra responses received
    Thread.sleep(1000);
    assertEquals(1, receivedResponses.get());
    assertEquals(BKException.Code.BookieHandleNotAvailableException, returnCode.get());
    bkc.close();
    startBKCluster();
}
Also used : BKBookieHandleNotAvailableException(org.apache.bookkeeper.client.BKException.BKBookieHandleNotAvailableException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReadCallback (org.apache.bookkeeper.client.AsyncCallback.ReadCallback)1 BKBookieHandleNotAvailableException (org.apache.bookkeeper.client.BKException.BKBookieHandleNotAvailableException)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 Test (org.junit.Test)1