use of org.apache.bookkeeper.meta.AbstractZkLedgerManager.ZK_CONNECT_BACKOFF_MS in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testLedgerMetadataListenerOnRetries.
@Test
public void testLedgerMetadataListenerOnRetries() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
LinkedBlockingQueue<LedgerMetadata> changes = new LinkedBlockingQueue<>();
LedgerMetadataListener listener = (ledgerId1, metadata) -> changes.add(metadata);
metadata.setVersion(new LongVersion(1234L));
Stat stat = mock(Stat.class);
when(stat.getVersion()).thenReturn(1234);
when(stat.getCtime()).thenReturn(metadata.getCtime());
// fail the first get, so the ledger manager will retry get data again.
mockZkGetData(ledgerStr, true, KeeperException.Code.SESSIONEXPIRED.intValue(), null, null);
ledgerManager.registerLedgerMetadataListener(ledgerId, listener);
assertTrue(ledgerManager.listeners.containsKey(ledgerId));
// the listener will not be notified with any updates
assertNull(changes.poll());
// an retry task is scheduled
verify(scheduler, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
// zookeeper is called once
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// watcher is not registered since getData call is failed
assertFalse(watchers.containsKey(ledgerStr));
// mock get data to return a valid response
mockZkGetData(ledgerStr, true, KeeperException.Code.OK.intValue(), metadata.serialize(), stat);
schedulerController.advance(Duration.ofMillis(ZK_CONNECT_BACKOFF_MS));
// the listener will be notified with first get
LedgerMetadata change = changes.take();
assertEquals(metadata, change);
verify(mockZk, times(2)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// watcher is registered after successfully `getData`
assertTrue(watchers.containsKey(ledgerStr));
}
Aggregations