@Test public void testSyncCreate() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", testName, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); boolean success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertTrue(success); ZNRecord getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getId(), "msg_0"); record.setSimpleField("key0", "value0"); success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertFalse(success, "Should fail since node already exists"); getRecord = _gZkClient.readData(path); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getSimpleFields().size(), 0); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testSyncGetStat() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", testName, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); Stat stat = accessor.getStat(path, 0); Assert.assertNull(stat); boolean success = accessor.create(path, record, AccessOption.EPHEMERAL); Assert.assertTrue(success); stat = accessor.getStat(path, 0); Assert.assertNotNull(stat); Assert.assertEquals(stat.getVersion(), 0); Assert.assertNotSame(stat.getEphemeralOwner(), 0); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testSyncGet() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", testName, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); Stat stat = new Stat(); ZNRecord getRecord = accessor.get(path, stat, 0); Assert.assertNull(getRecord); try { accessor.get(path, stat, AccessOption.THROW_EXCEPTION_IFNOTEXIST); Assert.fail("Should throw exception if not exist"); } catch (Exception e) { // OK } boolean success = accessor.create(path, record, AccessOption.PERSISTENT); Assert.assertTrue(success); getRecord = accessor.get(path, stat, 0); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getId(), "msg_0"); Assert.assertEquals(stat.getVersion(), 0); record.setSimpleField("key0", "value0"); success = accessor.set(path, record, AccessOption.PERSISTENT); Assert.assertTrue(success); getRecord = accessor.get(path, stat, 0); Assert.assertNotNull(getRecord); Assert.assertEquals(record.getSimpleFields().size(), 1); Assert.assertNotNull(getRecord.getSimpleField("key0")); Assert.assertEquals(getRecord.getSimpleField("key0"), "value0"); Assert.assertEquals(stat.getVersion(), 1); ZNRecord newRecord = new ZNRecord("msg_0"); newRecord.setSimpleField("key1", "value1"); success = accessor.update(path, new ZNRecordUpdater(newRecord), AccessOption.PERSISTENT); Assert.assertTrue(success); getRecord = accessor.get(path, stat, 0); Assert.assertNotNull(getRecord); Assert.assertEquals(getRecord.getSimpleFields().size(), 2); Assert.assertNotNull(getRecord.getSimpleField("key0")); Assert.assertEquals(getRecord.getSimpleField("key0"), "value0"); Assert.assertNotNull(getRecord.getSimpleField("key1")); Assert.assertEquals(getRecord.getSimpleField("key1"), "value1"); Assert.assertEquals(stat.getVersion(), 2); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }
@Test public void testSyncExist() { String className = TestHelper.getTestClassName(); String methodName = TestHelper.getTestMethodName(); String testName = className + "_" + methodName; System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis())); String path = String.format("/%s/%s", testName, "msg_0"); ZNRecord record = new ZNRecord("msg_0"); ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient); boolean success = accessor.exists(path, 0); Assert.assertFalse(success); success = accessor.create(path, record, AccessOption.EPHEMERAL); Assert.assertTrue(success); success = accessor.exists(path, 0); Assert.assertTrue(success); System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis())); }