use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class EntryCacheTest method testReadMissingMultiple.
@Test(timeOut = 5000)
void testReadMissingMultiple() throws Exception {
LedgerHandle lh = getLedgerHandle();
when(lh.getId()).thenReturn((long) 0);
EntryCacheManager cacheManager = factory.getEntryCacheManager();
EntryCache entryCache = cacheManager.getEntryCache(ml);
byte[] data = new byte[10];
entryCache.insert(new EntryImpl(0, 0, data));
entryCache.insert(new EntryImpl(0, 2, data));
entryCache.insert(new EntryImpl(0, 5, data));
entryCache.insert(new EntryImpl(0, 8, data));
final CountDownLatch counter = new CountDownLatch(1);
entryCache.asyncReadEntry(lh, 0, 9, false, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
assertEquals(entries.size(), 10);
counter.countDown();
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
Assert.fail("should not have failed");
}
}, null);
counter.await();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class EntryCacheTest method testReadWithError.
@Test(timeOut = 5000)
void testReadWithError() throws Exception {
final LedgerHandle lh = getLedgerHandle();
when(lh.getId()).thenReturn((long) 0);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
ReadCallback callback = (ReadCallback) args[2];
Object ctx = args[3];
callback.readComplete(BKException.Code.NoSuchLedgerExistsException, lh, null, ctx);
return null;
}
}).when(lh).asyncReadEntries(anyLong(), anyLong(), any(ReadCallback.class), any());
EntryCacheManager cacheManager = factory.getEntryCacheManager();
EntryCache entryCache = cacheManager.getEntryCache(ml);
byte[] data = new byte[10];
entryCache.insert(new EntryImpl(0, 2, data));
final CountDownLatch counter = new CountDownLatch(1);
entryCache.asyncReadEntry(lh, 0, 9, false, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
Assert.fail("should not complete");
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter.countDown();
}
}, null);
counter.await();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class EntryCacheTest method testRead.
@Test(timeOut = 5000)
void testRead() throws Exception {
LedgerHandle lh = getLedgerHandle();
when(lh.getId()).thenReturn((long) 0);
EntryCacheManager cacheManager = factory.getEntryCacheManager();
EntryCache entryCache = cacheManager.getEntryCache(ml);
byte[] data = new byte[10];
for (int i = 0; i < 10; i++) {
entryCache.insert(new EntryImpl(0, i, data));
}
final CountDownLatch counter = new CountDownLatch(1);
entryCache.asyncReadEntry(lh, 0, 9, false, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
assertEquals(entries.size(), 10);
entries.forEach(e -> e.release());
counter.countDown();
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
Assert.fail("should not have failed");
}
}, null);
counter.await();
// Verify no entries were read from bookkeeper
verify(lh, never()).asyncReadEntries(anyLong(), anyLong(), any(ReadCallback.class), any());
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class ReplicatorTest method testCloseReplicatorStartProducer.
/**
* It verifies that PersistentReplicator considers CursorAlreadyClosedException as non-retriable-read exception and
* it should closed the producer as cursor is already closed because replicator is already deleted.
*
* @throws Exception
*/
@Test(timeOut = 5000)
public void testCloseReplicatorStartProducer() throws Exception {
DestinationName dest = DestinationName.get("persistent://pulsar/global/ns1/closeCursor");
// Producer on r1
MessageProducer producer1 = new MessageProducer(url1, dest);
// Consumer on r1
MessageConsumer consumer1 = new MessageConsumer(url1, dest);
// Consumer on r2
MessageConsumer consumer2 = new MessageConsumer(url2, dest);
// Replicator for r1 -> r2
PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(dest.toString());
PersistentReplicator replicator = topic.getPersistentReplicator("r2");
// close the cursor
Field cursorField = PersistentReplicator.class.getDeclaredField("cursor");
cursorField.setAccessible(true);
ManagedCursor cursor = (ManagedCursor) cursorField.get(replicator);
cursor.close();
// try to read entries
CountDownLatch latch = new CountDownLatch(1);
producer1.produce(10);
cursor.asyncReadEntriesOrWait(10, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
latch.countDown();
fail("it should have been failed");
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
latch.countDown();
assertTrue(exception instanceof CursorAlreadyClosedException);
}
}, null);
// replicator-readException: cursorAlreadyClosed
replicator.readEntriesFailed(new CursorAlreadyClosedException("Cursor already closed exception"), null);
// wait replicator producer to be closed
Thread.sleep(1000);
// Replicator producer must be closed
Field producerField = PersistentReplicator.class.getDeclaredField("producer");
producerField.setAccessible(true);
ProducerImpl replicatorProducer = (ProducerImpl) producerField.get(replicator);
assertEquals(replicatorProducer, null);
producer1.close();
consumer1.close();
consumer2.close();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.
the class ManagedCursorTest method asyncReadWithoutErrors.
@Test(timeOut = 20000)
void asyncReadWithoutErrors() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ManagedCursor cursor = ledger.openCursor("c1");
ledger.addEntry("dummy-entry-1".getBytes(Encoding));
final CountDownLatch counter = new CountDownLatch(1);
cursor.asyncReadEntries(100, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
assertNull(ctx);
assertEquals(entries.size(), 1);
entries.forEach(e -> e.release());
counter.countDown();
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, null);
counter.await();
}
Aggregations