Search in sources :

Example 11 with GenericCallbackFuture

use of org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testReadLedgerMetadataDataCorrupted.

@Test
public void testReadLedgerMetadataDataCorrupted() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    Stat stat = mock(Stat.class);
    when(stat.getVersion()).thenReturn(1234);
    when(stat.getCtime()).thenReturn(metadata.getCtime());
    mockZkGetData(ledgerStr, false, KeeperException.Code.OK.intValue(), new byte[0], stat);
    GenericCallbackFuture<LedgerMetadata> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.readLedgerMetadata(ledgerId, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail on reading ledger metadata if a ledger doesn't exist");
    } catch (BKException bke) {
        assertEquals(Code.ZKException, bke.getCode());
    }
    verify(mockZk, times(1)).getData(eq(ledgerStr), eq(null), any(DataCallback.class), any());
}
Also used : Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with GenericCallbackFuture

use of org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testWriteLedgerMetadataSuccess.

@Test
public void testWriteLedgerMetadataSuccess() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    Stat stat = mock(Stat.class);
    when(stat.getVersion()).thenReturn(1235);
    when(stat.getCtime()).thenReturn(metadata.getCtime());
    mockZkSetData(ledgerStr, metadata.serialize(), 1234, KeeperException.Code.OK.intValue(), stat);
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.writeLedgerMetadata(ledgerId, metadata, callbackFuture);
    result(callbackFuture);
    assertEquals(new LongVersion(1235L), metadata.getVersion());
    verify(mockZk, times(1)).setData(eq(ledgerStr), any(byte[].class), eq(1234), any(StatCallback.class), any());
}
Also used : Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with GenericCallbackFuture

use of org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testWriteLedgerMetadataException.

@Test
public void testWriteLedgerMetadataException() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    mockZkSetData(ledgerStr, metadata.serialize(), 1234, KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.writeLedgerMetadata(ledgerId, metadata, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail on writing ledger metadata if encountering zookeeper exceptions");
    } catch (BKException bke) {
        assertEquals(Code.ZKException, bke.getCode());
    }
    // version remain unchanged
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    verify(mockZk, times(1)).setData(eq(ledgerStr), any(byte[].class), eq(1234), any(StatCallback.class), any());
}
Also used : LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with GenericCallbackFuture

use of org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testRemoveLedgerMetadataNoNode.

@Test
public void testRemoveLedgerMetadataNoNode() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    LongVersion version = new LongVersion(1234L);
    mockZkDelete(ledgerStr, (int) version.getLongVersion(), KeeperException.Code.NONODE.intValue());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.removeLedgerMetadata(ledgerId, version, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail to remove metadata if no such ledger exists");
    } catch (BKException bke) {
        assertEquals(Code.NoSuchLedgerExistsException, bke.getCode());
    }
    verify(mockZk, times(1)).delete(eq(ledgerStr), eq(1234), any(VoidCallback.class), eq(null));
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with GenericCallbackFuture

use of org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testRemoveLedgerMetadataHierarchicalLedgerManager.

private void testRemoveLedgerMetadataHierarchicalLedgerManager(AbstractZkLedgerManager lm) throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = lm.getLedgerPath(ledgerId);
    LongVersion version = new LongVersion(1234L);
    mockZkUtilsAsyncDeleteFullPathOptimistic(ledgerStr, (int) version.getLongVersion(), KeeperException.Code.OK.intValue());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    lm.removeLedgerMetadata(ledgerId, version, callbackFuture);
    result(callbackFuture);
    PowerMockito.verifyStatic(ZkUtils.class, times(1));
    ZkUtils.asyncDeleteFullPathOptimistic(eq(mockZk), eq(ledgerStr), eq(1234), any(VoidCallback.class), eq(ledgerStr));
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)

Aggregations

GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)16 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 BKException (org.apache.bookkeeper.client.BKException)10 LongVersion (org.apache.bookkeeper.versioning.LongVersion)10 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)5 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)5 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)5 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)3 Stat (org.apache.zookeeper.data.Stat)3 KeeperException (org.apache.zookeeper.KeeperException)2