@Test public void testManagedLedgerAutoCreate() throws Exception { ManagedLedgerConfig config = new ManagedLedgerConfig().setCreateIfMissing(true); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("test", config); assertNotNull(ledger); }
@Test public void testManagedLedgerWithoutAutoCreate() throws Exception { ManagedLedgerConfig config = new ManagedLedgerConfig().setCreateIfMissing(false); try { factory.open("testManagedLedgerWithoutAutoCreate", config); fail("should have thrown ManagedLedgerNotFoundException"); } catch (ManagedLedgerNotFoundException e) { // Expected } assertFalse(factory.getManagedLedgers().containsKey("testManagedLedgerWithoutAutoCreate")); }
@Test(timeOut = 20000) public void terminateSimple() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); Position p0 = ledger.addEntry("entry-0".getBytes()); Position lastPosition = ledger.terminate(); assertEquals(lastPosition, p0); try { ledger.addEntry("entry-1".getBytes()); } catch (ManagedLedgerTerminatedException e) { // Expected } }
@Test(timeOut = 20000) void testFindNewestMatchingEdgeCase1() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void invalidReadEntriesArg2() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("entry".getBytes()); cursor.readEntries(0); fail("Should have thrown an exception in the above line"); }
@Test(timeOut = 20000) public void testEmptyManagedLedgerContent() throws Exception { ZooKeeper zk = bkc.getZkHandle(); zk.create("/managed-ledger", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/managed-ledger/my_test_ledger", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); ManagedLedger ledger = factory.open("my_test_ledger"); ledger.openCursor("test"); ledger.addEntry("entry-1".getBytes(Encoding)); assertEquals(ledger.getNumberOfEntries(), 1); }
@Test(timeOut = 20000) void testFindNewestMatchingEdgeCase2() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); Position p1 = ledger.addEntry("expired".getBytes(Encoding)); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p1); }
@Test(timeOut = 20000) void errorCreatingCursor() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); bkc.failAfter(1, BKException.Code.NotEnoughBookiesException); zkc.failNow(Code.SESSIONEXPIRED); try { ledger.openCursor("c1"); fail("should have failed"); } catch (ManagedLedgerException e) { // ok } }
@Test public void testCleanup() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ledger.openCursor("c1"); ledger.addEntry("data".getBytes(Encoding)); assertEquals(bkc.getLedgers().size(), 2); ledger.delete(); assertEquals(bkc.getLedgers().size(), 0); }
@Test(timeOut = 20000) void testFindNewestMatchingEdgeCase4() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); Position p1 = ledger.addEntry("expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p1); }
@Test(timeOut = 20000) void testFindNewestMatchingEven2() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); ledger.addEntry("expired".getBytes(Encoding)); Position p2 = ledger.addEntry("expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p2); }
@Test(timeOut = 20000) void testFindNewestMatchingEven1() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); Position p1 = ledger.addEntry("expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p1); }
@Test(timeOut = 20000) void testFindNewestMatchingEven4() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); ledger.addEntry("expired".getBytes(Encoding)); ledger.addEntry("expired".getBytes(Encoding)); ledger.addEntry("expired".getBytes(Encoding)); Position p4 = ledger.addEntry("expired".getBytes(Encoding)); assertEquals( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))), p4); }
@Test(timeOut = 20000) void testFindNewestMatching() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1"); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); ledger.addEntry("not-expired".getBytes(Encoding)); assertNull( c1.findNewestMatching(entry -> Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding)))); }
@Test(timeOut = 20000) void markDeleteWithErrors() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); List<Entry> entries = cursor.readEntries(100); stopBookKeeper(); assertEquals(entries.size(), 1); // Mark-delete should succeed if BK is down cursor.markDelete(entries.get(0).getPosition()); entries.forEach(e -> e.release()); }
@Test(timeOut = 20000) void markDeleteWithErrors() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); List<Entry> entries = cursor.readEntries(100); stopBookKeeper(); assertEquals(entries.size(), 1); // Mark-delete should succeed if BK is down cursor.markDelete(entries.get(0).getPosition()); entries.forEach(e -> e.release()); }
@Test(timeOut = 20000) public void deleteWithErrors1() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger"); PositionImpl position = (PositionImpl) ledger.addEntry("dummy-entry-1".getBytes(Encoding)); assertEquals(ledger.getNumberOfEntries(), 1); // Force delete a ledger and test that deleting the ML still happens // without errors bkc.deleteLedger(position.getLedgerId()); ledger.delete(); }
@Test(timeOut = 20000) void testMarkDeleteTwice() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2)); ManagedCursor cursor = ledger.openCursor("c1"); Position p1 = ledger.addEntry("entry1".getBytes()); cursor.markDelete(p1); cursor.markDelete(p1); assertEquals(cursor.getMarkDeletedPosition(), p1); }
@Test(timeOut = 20000) void seekPosition2() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2)); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); ledger.addEntry("dummy-entry-2".getBytes(Encoding)); PositionImpl seekPosition = (PositionImpl) ledger.addEntry("dummy-entry-3".getBytes(Encoding)); ledger.addEntry("dummy-entry-4".getBytes(Encoding)); ledger.addEntry("dummy-entry-5".getBytes(Encoding)); ledger.addEntry("dummy-entry-6".getBytes(Encoding)); cursor.seek(new PositionImpl(seekPosition.getLedgerId(), seekPosition.getEntryId())); }