Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved code coverage

git-svn-id: svn+ssh://svn.corp.yahoo.com/yahoo/platform/yxms/trunk/managed-ledger@333 b48032ab-c6b0-43d1-8dc6-875f9f7cdcf0
  • Loading branch information...
commit 71b9c185ce5df9dd28260ea4e05ddfc22419ab4c 1 parent 60cafa4
mmerli authored
4 src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
@@ -138,7 +138,7 @@ public PositionImpl getSlowestReaderPosition() {
138 138 * the node to push
139 139 */
140 140 private void pushTowardHead(Node node) {
141   - while (node != null && node.previous != null) {
  141 + while (node.previous != null) {
142 142 // While this node is "bigger" than its previous, swap the two.
143 143 long currentId = ((PositionImpl) node.data.getMarkDeletedPosition()).getLedgerId();
144 144 long previousId = ((PositionImpl) node.previous.data.getMarkDeletedPosition()).getLedgerId();
@@ -165,7 +165,7 @@ private void pushTowardHead(Node node) {
165 165 * the node to push
166 166 */
167 167 private void pushTowardTail(Node node) {
168   - while (node != null && node.next != null) {
  168 + while (node.next != null) {
169 169 // While this node is "bigger" than its previous, swap the two.
170 170 long current = ((PositionImpl) node.data.getMarkDeletedPosition()).getLedgerId();
171 171 long next = ((PositionImpl) node.next.data.getMarkDeletedPosition()).getLedgerId();
7 src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
@@ -48,8 +48,7 @@
48 48 private final AtomicReference<PositionImpl> acknowledgedPosition = new AtomicReference<PositionImpl>();
49 49 private final AtomicReference<PositionImpl> readPosition = new AtomicReference<PositionImpl>();
50 50
51   - ManagedCursorImpl(ManagedLedgerImpl ledger, String name, PositionImpl position) throws InterruptedException,
52   - ManagedLedgerException {
  51 + ManagedCursorImpl(ManagedLedgerImpl ledger, String name, PositionImpl position) {
53 52 this.ledger = ledger;
54 53 this.name = name;
55 54 this.acknowledgedPosition.set(position);
@@ -157,8 +156,8 @@ public void run() {
157 156
158 157 @Override
159 158 public synchronized String toString() {
160   - return Objects.toStringHelper(this).add("name", name).add("ackPos", acknowledgedPosition)
161   - .add("readPos", readPosition).toString();
  159 + return Objects.toStringHelper(this).add("ledger", ledger.getName()).add("name", name)
  160 + .add("ackPos", acknowledgedPosition).add("readPos", readPosition).toString();
162 161 }
163 162
164 163 @Override
59 src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
@@ -90,6 +90,7 @@
90 90 ClosedLedger, // Current ledger has been closed and there's no pending
91 91 // operation
92 92 CreatingLedger, // Creating a new ledger
  93 + Closed, // ManagedLedger has been closed
93 94 Fenced, // A managed ledger is fenced when there is some concurrent
94 95 // access from a different session/machine. In this state the
95 96 // managed ledger will throw exception for all operations, since
@@ -221,19 +222,9 @@ private void initializeCursors(final ManagedLedgerInitializeLedgerCallback callb
221 222 store.getConsumers(name, new MetaStoreCallback<List<Pair<String, Position>>>() {
222 223 public void operationComplete(List<Pair<String, Position>> result, Version v) {
223 224 // Load existing cursors
224   - try {
225   - for (Pair<String, Position> pair : result) {
226   - log.debug("[{}] Loading cursor {}", name, pair);
227   - cursors.add(new ManagedCursorImpl(ManagedLedgerImpl.this, pair.first,
228   - (PositionImpl) pair.second));
229   - }
230   - } catch (InterruptedException e) {
231   - Thread.currentThread().interrupt();
232   - callback.initializeFailed(new ManagedLedgerException(e));
233   - return;
234   - } catch (ManagedLedgerException e) {
235   - callback.initializeFailed(e);
236   - return;
  225 + for (Pair<String, Position> pair : result) {
  226 + log.debug("[{}] Loading cursor {}", name, pair);
  227 + cursors.add(new ManagedCursorImpl(ManagedLedgerImpl.this, pair.first, (PositionImpl) pair.second));
237 228 }
238 229
239 230 // Calculate total entries and size
@@ -288,11 +279,13 @@ public void addFailed(ManagedLedgerException exception, Object ctx) {
288 279
289 280 @Override
290 281 public synchronized void asyncAddEntry(final byte[] data, final AddEntryCallback callback, final Object ctx) {
291   - checkArgument(state != State.None);
292 282 log.debug("[{}] asyncAddEntry size={} state={}", va(name, data.length, state));
293 283 if (state == State.Fenced) {
294 284 callback.addFailed(new ManagedLedgerFencedException(), ctx);
295 285 return;
  286 + } else if (state == State.Closed) {
  287 + callback.addFailed(new ManagedLedgerException("Managed ledger was already closed"), ctx);
  288 + return;
296 289 }
297 290
298 291 OpAddEntry addOperation = new OpAddEntry(this, data, callback, ctx);
@@ -331,6 +324,7 @@ public synchronized void asyncAddEntry(final byte[] data, final AddEntryCallback
331 324
332 325 @Override
333 326 public synchronized ManagedCursor openCursor(String cursorName) throws InterruptedException, ManagedLedgerException {
  327 + checkManagedLedgerIsOpen();
334 328 checkFenced();
335 329
336 330 ManagedCursor cursor = cursors.get(cursorName);
@@ -392,6 +386,7 @@ public synchronized void close() throws InterruptedException, ManagedLedgerExcep
392 386 ledgerCache.invalidateAll();
393 387 log.info("Invalidated {} ledgers in cache", ledgerCache.size());
394 388 factory.close(this);
  389 + state = State.Closed;
395 390 }
396 391
397 392 @Override
@@ -502,18 +497,6 @@ synchronized void asyncReadEntries(OpReadEntry opReadEntry) {
502 497
503 498 LedgerHandle ledger = null;
504 499
505   - if (opReadEntry.readPosition.getLedgerId() == -1) {
506   - if (ledgers.isEmpty()) {
507   - // The ManagedLedger is completely empty
508   - opReadEntry.emptyResponse();
509   - return;
510   - }
511   -
512   - // Initialize the position on the first entry for the first ledger
513   - // in the set
514   - opReadEntry.readPosition = new PositionImpl(ledgers.firstKey(), 0);
515   - }
516   -
517 500 long id = opReadEntry.readPosition.getLedgerId();
518 501
519 502 if (id == currentLedger.getId()) {
@@ -610,6 +593,7 @@ public synchronized void openComplete(int rc, LedgerHandle ledger, Object ctx) {
610 593 }
611 594
612 595 log.debug("[{}] Successfully opened ledger {} for reading", name, ledger.getId());
  596 + ledgerCache.put(ledger.getId(), ledger);
613 597 internalReadFromLedger(ledger, opReadEntry);
614 598 }
615 599
@@ -662,6 +646,7 @@ synchronized void updateCursor(ManagedCursorImpl cursor, PositionImpl newPositio
662 646 void trimConsumedLedgersInBackground() {
663 647 executor.execute(new Runnable() {
664 648 public void run() {
  649 + // Ensure only one trimming operation is active
665 650 synchronized (trimmerMutex) {
666 651 internalTrimConsumedLedgers();
667 652 }
@@ -676,7 +661,7 @@ public void run() {
676 661 * @throws Exception
677 662 */
678 663 void internalTrimConsumedLedgers() {
679   - // Ensure only one trimming operation is active
  664 +
680 665 List<LedgerStat> ledgersToDelete = Lists.newArrayList();
681 666
682 667 synchronized (this) {
@@ -777,8 +762,7 @@ synchronized long getNumberOfEntries(PositionImpl position) {
777 762 long count = 0;
778 763 // First count the number of unread entries in the ledger pointed by
779 764 // position
780   - if (position.getLedgerId() >= 0)
781   - count += ledgers.get(position.getLedgerId()).getEntriesCount() - position.getEntryId();
  765 + count += ledgers.get(position.getLedgerId()).getEntriesCount() - position.getEntryId();
782 766
783 767 // Then, recur all the next ledgers and sum all the entries they contain
784 768 for (LedgerStat ls : ledgers.tailMap(position.getLedgerId(), false).values()) {
@@ -808,16 +792,11 @@ synchronized PositionImpl skipEntries(PositionImpl startPosition, int entriesToS
808 792 entriesToSkip += startPosition.getEntryId();
809 793
810 794 while (entriesToSkip > 0) {
811   - if (currentLedger != null && ledgerId == currentLedger.getId()) {
  795 + if (ledgerId == currentLedger.getId()) {
812 796 checkArgument(entriesToSkip <= (currentLedger.getLastAddConfirmed() + 1));
813 797 return new PositionImpl(ledgerId, entriesToSkip);
814 798 } else {
815 799 LedgerStat ledger = ledgers.get(ledgerId);
816   - if (ledger == null) {
817   - checkArgument(!ledgers.isEmpty());
818   - ledgerId = ledgers.ceilingKey(ledgerId);
819   - continue;
820   - }
821 800
822 801 if (entriesToSkip < ledger.getEntriesCount()) {
823 802 return new PositionImpl(ledgerId, entriesToSkip);
@@ -842,14 +821,14 @@ synchronized PositionImpl skipEntries(PositionImpl startPosition, int entriesToS
842 821 */
843 822 synchronized boolean isValidPosition(PositionImpl position) {
844 823 if (position.getLedgerId() == currentLedger.getId()) {
845   - return position.getEntryId() <= currentLedger.getLastAddConfirmed();
  824 + return position.getEntryId() <= (currentLedger.getLastAddConfirmed() + 1);
846 825 } else {
847 826 // Look in the ledgers map
848 827 LedgerStat ls = ledgers.get(position.getLedgerId());
849 828 if (ls == null)
850 829 return false;
851 830
852   - return position.getEntryId() < ls.getEntriesCount();
  831 + return position.getEntryId() <= ls.getEntriesCount();
853 832 }
854 833 }
855 834
@@ -874,6 +853,12 @@ private void checkFenced() throws ManagedLedgerException {
874 853 }
875 854 }
876 855
  856 + private void checkManagedLedgerIsOpen() throws ManagedLedgerException {
  857 + if (state == State.Closed) {
  858 + throw new ManagedLedgerException("ManagedLedger " + name + " has already been closed");
  859 + }
  860 + }
  861 +
877 862 synchronized void setFenced() {
878 863 state = State.Fenced;
879 864 }
110 src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
@@ -56,6 +56,11 @@ void readFromEmptyLedger() throws Exception {
56 56
57 57 entries = c1.readEntries(10);
58 58 assertEquals(entries.size(), 0);
  59 +
  60 + // Test string representation
  61 + assertEquals(c1.toString(), "ManagedCursorImpl{ledger=my_test_ledger, name=c1, ackPos=3:-1, readPos=3:1}");
  62 +
  63 + factory.shutdown();
59 64 }
60 65
61 66 @Test(timeOut = 3000)
@@ -134,6 +139,32 @@ public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
134 139 counter.await();
135 140 }
136 141
  142 + @Test(timeOut = 3000, expectedExceptions = IllegalArgumentException.class)
  143 + void asyncReadWithInvalidParameter() throws Exception {
  144 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  145 + ManagedLedger ledger = factory.open("my_test_ledger");
  146 + ManagedCursor cursor = ledger.openCursor("c1");
  147 +
  148 + ledger.addEntry("dummy-entry-1".getBytes(Encoding));
  149 +
  150 + final CountDownLatch counter = new CountDownLatch(1);
  151 +
  152 + stopBKCluster();
  153 +
  154 + cursor.asyncReadEntries(0, new ReadEntriesCallback() {
  155 + public void readEntriesComplete(List<Entry> entries, Object ctx) {
  156 + fail("async-call should have failed");
  157 + }
  158 +
  159 + public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
  160 + counter.countDown();
  161 + }
  162 +
  163 + }, null);
  164 +
  165 + counter.await();
  166 + }
  167 +
137 168 @Test(timeOut = 3000)
138 169 void asyncMarkDeleteWithErrors() throws Exception {
139 170 ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
@@ -207,6 +238,22 @@ void skipEntries2() throws Exception {
207 238 cursor.readEntries(2);
208 239 cursor.skip(1);
209 240 assertEquals(cursor.getNumberOfEntries(), 3);
  241 +
  242 + try {
  243 + cursor.skip(4);
  244 + fail("should have failed");
  245 + } catch (IllegalArgumentException e) {
  246 + // ok
  247 + }
  248 +
  249 + cursor.skip(3);
  250 +
  251 + try {
  252 + cursor.skip(1);
  253 + fail("should have failed");
  254 + } catch (IllegalArgumentException e) {
  255 + // ok
  256 + }
210 257 }
211 258
212 259 @Test(expectedExceptions = IllegalArgumentException.class)
@@ -263,54 +310,37 @@ void seekPosition3() throws Exception {
263 310 cursor.seek(new PositionImpl(seekPosition.getLedgerId(), seekPosition.getEntryId()));
264 311 }
265 312
266   - @Test(expectedExceptions = IllegalArgumentException.class)
267   - void seekPositionEmpty() throws Exception {
268   - ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
269   - ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
270   - ManagedCursor cursor = ledger.openCursor("c1");
271   -
272   - Position currentPosition = cursor.getReadPosition();
273   - cursor.seek(currentPosition);
274   - }
275   -
276   - @Test(expectedExceptions = IllegalArgumentException.class)
277   - void seekPositionWithError() throws Exception {
278   - ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
279   - ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
280   - ManagedCursor cursor = ledger.openCursor("c1");
281   - ledger.addEntry("dummy-entry-1".getBytes(Encoding));
282   - ledger.addEntry("dummy-entry-2".getBytes(Encoding));
283   -
284   - Position p = cursor.getReadPosition();
285   - List<Entry> entries = cursor.readEntries(1);
286   - assertEquals(entries.size(), 1);
287   - cursor.markDelete(entries.get(0).getPosition());
288   - cursor.seek(p);
289   - }
290   -
291   - @Test(expectedExceptions = IllegalArgumentException.class)
292   - void seekPositionWithError2() throws Exception {
293   - ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
294   - ManagedLedger ledger = factory.open("my_test_ledger");
295   - ManagedCursor cursor = ledger.openCursor("c1");
296   - ledger.addEntry("dummy-entry-1".getBytes(Encoding));
297   - ledger.addEntry("dummy-entry-2".getBytes(Encoding));
298   -
299   - PositionImpl p = (PositionImpl) cursor.getReadPosition();
300   - cursor.seek(new PositionImpl(p.getLedgerId(), 2));
301   - }
302   -
303   - @Test(expectedExceptions = IllegalArgumentException.class)
  313 + @Test
304 314 void seekPositionWithError3() throws Exception {
305 315 ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
306   - ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10));
  316 + ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
307 317 ManagedCursor cursor = ledger.openCursor("c1");
308   - ledger.addEntry("dummy-entry-1".getBytes(Encoding));
  318 + PositionImpl firstPosition = (PositionImpl) ledger.addEntry("dummy-entry-1".getBytes(Encoding));
309 319 ledger.addEntry("dummy-entry-2".getBytes(Encoding));
310 320 ledger.addEntry("dummy-entry-3".getBytes(Encoding));
311 321 PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding));
312 322
313 323 cursor.seek(new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() + 1));
  324 + try {
  325 + cursor.seek(new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() + 2));
  326 + fail("Should have failed");
  327 + } catch (IllegalArgumentException e) {
  328 + // Ok
  329 + }
  330 +
  331 + try {
  332 + cursor.seek(new PositionImpl(firstPosition.getLedgerId(), 1000));
  333 + fail("Should have failed");
  334 + } catch (IllegalArgumentException e) {
  335 + // Ok
  336 + }
  337 +
  338 + try {
  339 + cursor.seek(new PositionImpl(firstPosition.getLedgerId() + 1000, 0));
  340 + fail("Should have failed");
  341 + } catch (IllegalArgumentException e) {
  342 + // Ok
  343 + }
314 344 }
315 345
316 346 @Test(timeOut = 3000)
183 src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java
@@ -125,6 +125,7 @@ public void simple() throws Exception {
125 125 assertEquals(entries.size(), 0);
126 126
127 127 ledger.close();
  128 + factory.shutdown();
128 129 }
129 130
130 131 @Test(timeOut = 3000)
@@ -704,7 +705,7 @@ public void testEmptyManagedLedgerContent() throws Exception {
704 705 assertEquals(ledger.getNumberOfEntries(), 1);
705 706 }
706 707
707   - @Test(timeOut = 5000, enabled = false)
  708 + @Test(timeOut = 5000)
708 709 public void testProducerAndNoConsumer() throws Exception {
709 710 ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
710 711 ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
@@ -726,6 +727,34 @@ public void testProducerAndNoConsumer() throws Exception {
726 727 Thread.sleep(10);
727 728 }
728 729
  730 + @Test(timeOut = 5000)
  731 + public void testTrimmer() throws Exception {
  732 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  733 + ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
  734 + ManagedLedger ledger = factory.open("my_test_ledger", config);
  735 + ManagedCursor cursor = ledger.openCursor("c1");
  736 +
  737 + assertEquals(ledger.getNumberOfEntries(), 0);
  738 +
  739 + ledger.addEntry("entry-1".getBytes(Encoding));
  740 + ledger.addEntry("entry-2".getBytes(Encoding));
  741 + ledger.addEntry("entry-3".getBytes(Encoding));
  742 + ledger.addEntry("entry-4".getBytes(Encoding));
  743 + assertEquals(ledger.getNumberOfEntries(), 4);
  744 +
  745 + cursor.readEntries(1);
  746 + cursor.readEntries(1);
  747 + Position lastPosition = cursor.readEntries(1).get(0).getPosition();
  748 +
  749 + assertEquals(ledger.getNumberOfEntries(), 4);
  750 +
  751 + cursor.markDelete(lastPosition);
  752 +
  753 + while (ledger.getNumberOfEntries() != 2) {
  754 + Thread.sleep(10);
  755 + }
  756 + }
  757 +
729 758 @Test(timeOut = 3000)
730 759 public void testAsyncAddEntryAndSyncClose() throws Exception {
731 760 ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
@@ -855,6 +884,12 @@ public void fenceManagedLedger() throws Exception {
855 884 }
856 885
857 886 try {
  887 + ledger1.addEntry("entry-2".getBytes(Encoding));
  888 + fail("Expecting exception");
  889 + } catch (ManagedLedgerFencedException e) {
  890 + }
  891 +
  892 + try {
858 893 cursor1.readEntries(10);
859 894 fail("Expecting exception");
860 895 } catch (ManagedLedgerFencedException e) {
@@ -871,4 +906,150 @@ public void fenceManagedLedger() throws Exception {
871 906 assertEquals(cursor2.getNumberOfEntries(), 2);
872 907 }
873 908
  909 + @Test
  910 + public void forceCloseLedgers() throws Exception {
  911 + ManagedLedgerFactory factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  912 + ManagedLedger ledger1 = factory1.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
  913 + ledger1.openCursor("c1");
  914 + ManagedCursor c2 = ledger1.openCursor("c2");
  915 + ledger1.addEntry("entry-1".getBytes(Encoding));
  916 + ledger1.addEntry("entry-2".getBytes(Encoding));
  917 + ledger1.addEntry("entry-3".getBytes(Encoding));
  918 +
  919 + c2.readEntries(1);
  920 + c2.readEntries(1);
  921 + c2.readEntries(1);
  922 +
  923 + ledger1.close();
  924 +
  925 + try {
  926 + ledger1.addEntry("entry-3".getBytes(Encoding));
  927 + fail("should not have reached this point");
  928 + } catch (ManagedLedgerException e) {
  929 + // ok
  930 + }
  931 +
  932 + try {
  933 + ledger1.openCursor("new-cursor");
  934 + fail("should not have reached this point");
  935 + } catch (ManagedLedgerException e) {
  936 + // ok
  937 + }
  938 + }
  939 +
  940 + @Test
  941 + public void closeLedgerWithError() throws Exception {
  942 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  943 + ManagedLedger ledger = factory.open("my_test_ledger");
  944 + ledger.addEntry("entry-1".getBytes(Encoding));
  945 +
  946 + stopZKCluster();
  947 + stopBKCluster();
  948 +
  949 + try {
  950 + ledger.close();
  951 + // fail("should have thrown exception");
  952 + } catch (ManagedLedgerException e) {
  953 + // Ok
  954 + }
  955 + }
  956 +
  957 + @Test(timeOut = 3000)
  958 + public void deleteWithErrors1() throws Exception {
  959 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  960 +
  961 + ManagedLedger ledger = factory.open("my_test_ledger");
  962 +
  963 + PositionImpl position = (PositionImpl) ledger.addEntry("dummy-entry-1".getBytes(Encoding));
  964 + assertEquals(ledger.getNumberOfEntries(), 1);
  965 + ledger.close();
  966 +
  967 + // Force delete a ledger and test that deleting the ML still happens
  968 + // without errors
  969 + bkc.deleteLedger(position.getLedgerId());
  970 + factory.delete("my_test_ledger");
  971 + }
  972 +
  973 + @Test(timeOut = 3000)
  974 + public void deleteWithErrors2() throws Exception {
  975 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  976 +
  977 + ManagedLedger ledger = factory.open("my_test_ledger");
  978 + ledger.addEntry("dummy-entry-1".getBytes(Encoding));
  979 + ledger.close();
  980 +
  981 + stopZKCluster();
  982 +
  983 + try {
  984 + factory.delete("my_test_ledger");
  985 + fail("should have failed");
  986 + } catch (ManagedLedgerException e) {
  987 + // ok
  988 + }
  989 + }
  990 +
  991 + @Test(timeOut = 3000)
  992 + public void readWithErrors1() throws Exception {
  993 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  994 +
  995 + ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
  996 + ManagedCursor cursor = ledger.openCursor("c1");
  997 + ledger.addEntry("dummy-entry-1".getBytes(Encoding));
  998 + ledger.addEntry("dummy-entry-2".getBytes(Encoding));
  999 +
  1000 + stopZKCluster();
  1001 +
  1002 + try {
  1003 + cursor.readEntries(10);
  1004 + fail("should have failed");
  1005 + } catch (ManagedLedgerException e) {
  1006 + // ok
  1007 + }
  1008 +
  1009 + try {
  1010 + ledger.addEntry("dummy-entry-3".getBytes(Encoding));
  1011 + fail("should have failed");
  1012 + } catch (ManagedLedgerException e) {
  1013 + // ok
  1014 + }
  1015 + }
  1016 +
  1017 + @Test(timeOut = 3000, enabled = false)
  1018 + void concurrentAsyncOpen() throws Exception {
  1019 + ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
  1020 +
  1021 + final CountDownLatch counter = new CountDownLatch(2);
  1022 +
  1023 + class Result {
  1024 + ManagedLedger instance1 = null;
  1025 + ManagedLedger instance2 = null;
  1026 + }
  1027 +
  1028 + final Result result = new Result();
  1029 + factory.asyncOpen("my-test-ledger", new OpenLedgerCallback() {
  1030 +
  1031 + public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
  1032 + result.instance1 = ledger;
  1033 + counter.countDown();
  1034 + }
  1035 +
  1036 + public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
  1037 + }
  1038 + }, null);
  1039 +
  1040 + factory.asyncOpen("my-test-ledger", new OpenLedgerCallback() {
  1041 +
  1042 + public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
  1043 + result.instance2 = ledger;
  1044 + counter.countDown();
  1045 + }
  1046 +
  1047 + public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
  1048 + }
  1049 + }, null);
  1050 +
  1051 + counter.await();
  1052 + assertEquals(result.instance1, result.instance2);
  1053 + assertNotNull(result.instance1);
  1054 + }
874 1055 }

0 comments on commit 71b9c18

Please sign in to comment.
Something went wrong with that request. Please try again.