use of org.apache.helix.zookeeper.datamodel.SessionAwareZNRecord in project helix by apache.
the class TestRawZkClient method testAsyncWriteByExpectedSession.
@Test
public void testAsyncWriteByExpectedSession() throws Exception {
ZkClient zkClient = new ZkClient(ZkTestBase.ZK_ADDR);
zkClient.setZkSerializer(new ZNRecordSerializer());
String sessionId = Long.toHexString(zkClient.getSessionId());
String path = "/" + TestHelper.getTestClassName() + "_" + TestHelper.getTestMethodName();
SessionAwareZNRecord record = new SessionAwareZNRecord("test");
// Set a dummy session id string to be mismatched with the real session id in ZkClient.
record.setExpectedSessionId("ExpectedSession");
ZkAsyncCallbacks.CreateCallbackHandler createCallback = new ZkAsyncCallbacks.CreateCallbackHandler();
try {
zkClient.asyncCreate(path, record, CreateMode.PERSISTENT, createCallback);
Assert.fail("Invalid session should not create znode");
} catch (ZkSessionMismatchedException expected) {
Assert.assertEquals(expected.getMessage(), "Failed to get expected zookeeper instance! There is a session id mismatch. Expected: " + "ExpectedSession. Actual: " + sessionId);
// Ensure the async callback is cancelled because of the exception
Assert.assertTrue(createCallback.waitForSuccess(), "Callback operation should be done");
Assert.assertEquals(createCallback.getRc(), KeeperException.Code.APIERROR.intValue());
}
Assert.assertFalse(zkClient.exists(path));
// A valid session should be able to create the znode.
record.setExpectedSessionId(sessionId);
createCallback = new ZkAsyncCallbacks.CreateCallbackHandler();
zkClient.asyncCreate(path, record, CreateMode.PERSISTENT, createCallback);
Assert.assertTrue(createCallback.waitForSuccess(), "Callback operation should be done");
Assert.assertEquals(createCallback.getRc(), 0);
Assert.assertTrue(zkClient.exists(path));
// Test asyncSetData() failed by mismatched session
record.setExpectedSessionId("ExpectedSession");
ZkAsyncCallbacks.SetDataCallbackHandler setDataCallback = new ZkAsyncCallbacks.SetDataCallbackHandler();
try {
zkClient.asyncSetData(path, record, 0, setDataCallback);
Assert.fail("Invalid session should not change znode data");
} catch (ZkSessionMismatchedException expected) {
Assert.assertEquals(expected.getMessage(), "Failed to get expected zookeeper instance! There is a session id mismatch. Expected: " + "ExpectedSession. Actual: " + sessionId);
// Ensure the async callback is cancelled because of the exception
Assert.assertTrue(setDataCallback.waitForSuccess(), "Callback operation should be done");
Assert.assertEquals(setDataCallback.getRc(), KeeperException.Code.APIERROR.intValue());
}
TestHelper.verify(() -> zkClient.delete(path), TestHelper.WAIT_DURATION);
zkClient.close();
}
Aggregations