You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/01/24 18:42:09 UTC

[GitHub] sijie closed pull request #1045: More testcases for LedgerHandle

sijie closed pull request #1045: More testcases for LedgerHandle
URL: https://github.com/apache/bookkeeper/pull/1045
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
index 06327fc6d..312af3940 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
@@ -38,16 +38,20 @@
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
+import org.apache.bookkeeper.client.AsyncCallback.CloseCallback;
 import org.apache.bookkeeper.client.AsyncCallback.ReadCallback;
 import org.apache.bookkeeper.client.AsyncCallback.ReadLastConfirmedCallback;
 import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BKException.BKIllegalOpException;
 import org.apache.bookkeeper.client.BookKeeper.DigestType;
 import org.apache.bookkeeper.client.LedgerEntry;
 import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.streaming.LedgerInputStream;
 import org.apache.bookkeeper.streaming.LedgerOutputStream;
+import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -953,13 +957,139 @@ public void testReadFromOpenLedgerZeroAndOne() throws Exception {
         }
     }
 
+    @Test
+    public void testWriteUsingReadOnlyHandle() throws Exception {
+        // Create a ledger
+        lh = bkc.createLedger(digestType, ledgerPassword);
+        ledgerId = lh.getId();
+        LOG.info("Ledger ID: " + lh.getId());
+
+        long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
+        LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
+
+        // addEntry on ReadOnlyHandle should fail
+        CountDownLatch latch = new CountDownLatch(1);
+        final int[] rcArray = { 0 };
+        lhOpen.asyncAddEntry("".getBytes(), new AddCallback() {
+            @Override
+            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        if (rcArray[0] != BKException.Code.IllegalOpException) {
+            Assert.fail("Test1 - asyncAddOperation is supposed to be failed, but it got following rc - "
+                    + KeeperException.Code.get(rcArray[0]));
+        }
+
+        // addEntry on ReadOnlyHandle should fail
+        latch = new CountDownLatch(1);
+        rcArray[0] = 0;
+        lhOpen.asyncAddEntry("".getBytes(), 0, 0, new AddCallback() {
+            @Override
+            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        if (rcArray[0] != BKException.Code.IllegalOpException) {
+            Assert.fail(
+                    "Test2 - asyncAddOperation is supposed to fail with IllegalOpException, but it got following rc - "
+                            + KeeperException.Code.get(rcArray[0]));
+        }
+
+        // close readonlyhandle
+        latch = new CountDownLatch(1);
+        rcArray[0] = 0;
+        lhOpen.asyncClose(new CloseCallback() {
+            @Override
+            public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        if (rcArray[0] != KeeperException.Code.OK.intValue()) {
+            Assert.fail("Test3 - asyncClose failed because of exception - " + KeeperException.Code.get(rcArray[0]));
+        }
+
+        // close of readonlyhandle should not affect the writehandle
+        writeNEntriesLastWriteSync(lh, 5);
+        lh.close();
+    }
+
+    @Test
+    public void testLedgerHandle() throws Exception {
+        // Create a ledger
+        lh = bkc.createLedger(digestType, ledgerPassword);
+        ledgerId = lh.getId();
+        LOG.info("Ledger ID: " + lh.getId());
+
+        long lac = writeNEntriesLastWriteSync(lh, 5);
+
+        // doing addEntry with entryid using regular Ledgerhandle should fail
+        CountDownLatch latch = new CountDownLatch(1);
+        final int[] rcArray = { 0 };
+        lh.asyncAddEntry(lac + 1, "".getBytes(), new AddCallback() {
+            @Override
+            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        if (rcArray[0] != BKException.Code.IllegalOpException) {
+            Assert.fail(
+                    "Test1 - addEntry with EntryID is expected to fail with IllegalOpException, "
+                    + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
+        }
+
+        // doing addEntry with entryid using regular Ledgerhandle should fail
+        latch = new CountDownLatch(1);
+        rcArray[0] = 0;
+        lh.asyncAddEntry(lac + 1, "".getBytes(), 0, 0, new AddCallback() {
+            @Override
+            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        if (rcArray[0] != BKException.Code.IllegalOpException) {
+            Assert.fail(
+                    "Test2 - addEntry with EntryID is expected to fail with IllegalOpException,"
+                    + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
+        }
+
+        // doing addEntry with entryid using regular Ledgerhandle should fail
+        try {
+            lh.addEntry(lac + 1, "".getBytes());
+            Assert.fail("Test3 - addEntry with EntryID is expected to fail");
+        } catch (BKIllegalOpException E) {
+        }
+
+        // doing addEntry with entryid using regular Ledgerhandle should fail
+        try {
+            lh.addEntry(lac + 1, "".getBytes(), 0, 0);
+            Assert.fail("Test4 - addEntry with EntryID is expected to fail");
+        } catch (BKIllegalOpException E) {
+        }
+
+        lh.close();
+    }
 
     @Test
     public void testLastConfirmedAdd() throws Exception {
         try {
             // Create a ledger
             lh = bkc.createLedger(digestType, ledgerPassword);
-            // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
 
@@ -1005,6 +1135,66 @@ public void testLastConfirmedAdd() throws Exception {
         }
     }
 
+    @Test
+    public void testReadLastConfirmed() throws Exception {
+        // Create a ledger and add entries
+        lh = bkc.createLedger(digestType, ledgerPassword);
+        // bkc.initMessageDigest("SHA1");
+        ledgerId = lh.getId();
+        LOG.info("Ledger ID: " + lh.getId());
+        long previousLAC = writeNEntriesLastWriteSync(lh, 5);
+
+        // add more entries after opening ReadonlyLedgerHandle
+        LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
+        long currentLAC = writeNEntriesLastWriteSync(lh, 5);
+
+        // get LAC instance variable of ReadHandle and verify if it is equal to (previousLAC - 1)
+        long readLAC = lhOpen.getLastAddConfirmed();
+        Assert.assertEquals("Test1 - For ReadHandle LAC", (previousLAC - 1), readLAC);
+
+        // close the write LedgerHandle and sleep for 500 msec to make sure all close watchers are called
+        lh.close();
+        Thread.sleep(500);
+
+        // now call asyncReadLastConfirmed and verify if it is equal to currentLAC
+        CountDownLatch latch = new CountDownLatch(1);
+        final int[] rcArray = { 0 };
+        final long[] lastConfirmedArray = { 0 };
+        lhOpen.asyncReadLastConfirmed(new ReadLastConfirmedCallback() {
+            @Override
+            public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                lastConfirmedArray[0] = lastConfirmed;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        Assert.assertEquals("Test3 - asyncReadLastConfirmed response", KeeperException.Code.OK.intValue(), rcArray[0]);
+        Assert.assertEquals("Test3 - ReadLAC", currentLAC, lastConfirmedArray[0]);
+
+        // similarly try calling asyncTryReadLastConfirmed and verify if it is equal to currentLAC
+        latch = new CountDownLatch(1);
+        rcArray[0] = 0;
+        lastConfirmedArray[0] = 0;
+        lhOpen.asyncTryReadLastConfirmed(new ReadLastConfirmedCallback() {
+            @Override
+            public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
+                CountDownLatch latch = (CountDownLatch) ctx;
+                rcArray[0] = rc;
+                lastConfirmedArray[0] = lastConfirmed;
+                latch.countDown();
+            }
+        }, latch);
+        latch.await();
+        Assert.assertEquals("Test4 - asyncTryReadLastConfirmed response", KeeperException.Code.OK.intValue(),
+                rcArray[0]);
+        Assert.assertEquals("Test4 - ReadLAC", currentLAC, lastConfirmedArray[0]);
+
+        // similarly try calling tryReadLastConfirmed and verify if it is equal to currentLAC
+        long tryReadLAC = lhOpen.tryReadLastConfirmed();
+        Assert.assertEquals("Test5 - ReadLAC", currentLAC, tryReadLAC);
+    }
 
     @Override
     public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services