use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class ManagedCursorTest method cancelReadOperation.
@Test(timeOut = 20000)
void cancelReadOperation() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
ManagedCursor c1 = ledger.openCursor("c1");
// No read request so far
assertEquals(c1.cancelPendingReadRequest(), false);
CountDownLatch counter = new CountDownLatch(1);
c1.asyncReadEntriesOrWait(1, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
counter.countDown();
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter.countDown();
}
}, null);
assertEquals(c1.cancelPendingReadRequest(), true);
CountDownLatch counter2 = new CountDownLatch(1);
c1.asyncReadEntriesOrWait(1, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
counter2.countDown();
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter2.countDown();
}
}, null);
ledger.addEntry("entry-1".getBytes(Encoding));
Thread.sleep(100);
// Read operation should have already been completed
assertEquals(c1.cancelPendingReadRequest(), false);
counter2.await();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class ManagedLedgerTest method asyncAPI.
@Test(timeOut = 20000)
public void asyncAPI() throws Throwable {
final CountDownLatch counter = new CountDownLatch(1);
factory.asyncOpen("my_test_ledger", new ManagedLedgerConfig(), new OpenLedgerCallback() {
@Override
public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
ledger.asyncOpenCursor("test-cursor", new OpenCursorCallback() {
@Override
public void openCursorComplete(ManagedCursor cursor, Object ctx) {
ManagedLedger ledger = (ManagedLedger) ctx;
ledger.asyncAddEntry("test".getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
@SuppressWarnings("unchecked") Pair<ManagedLedger, ManagedCursor> pair = (Pair<ManagedLedger, ManagedCursor>) ctx;
ManagedLedger ledger = pair.first;
ManagedCursor cursor = pair.second;
assertEquals(ledger.getNumberOfEntries(), 1);
assertEquals(ledger.getTotalSize(), "test".getBytes(Encoding).length);
cursor.asyncReadEntries(2, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(entries.size(), 1);
Entry entry = entries.get(0);
assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
log.debug("Mark-Deleting to position {}", entry.getPosition());
cursor.asyncMarkDelete(entry.getPosition(), new MarkDeleteCallback() {
@Override
public void markDeleteComplete(Object ctx) {
log.debug("Mark delete complete");
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(cursor.hasMoreEntries(), false);
counter.countDown();
}
@Override
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, new Pair<ManagedLedger, ManagedCursor>(ledger, cursor));
}
@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, ledger);
}
@Override
public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, null);
counter.await();
log.info("Test completed");
}
Aggregations