/** * Test write operations using multi request. */ @Test(timeout = 90000) public void testMultiTransaction() throws Exception { CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected final String data = "Data to be read in RO mode"; final String node1 = "/tnode1"; final String node2 = "/tnode2"; zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.reset(); qu.shutdown(2); watcher.waitForConnected(CONNECTION_TIMEOUT); Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY, zk.getState()); // read operation during r/o mode String remoteData = new String(zk.getData(node1, false, null)); Assert.assertEquals("Failed to read data in r-o mode", data, remoteData); try { Transaction transaction = zk.transaction(); transaction.setData(node1, "no way".getBytes(), -1); transaction.create(node2, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); transaction.commit(); Assert.fail("Write operation using multi-transaction" + " api has succeeded during RO mode"); } catch (NotReadOnlyException e) { // ok } Assert.assertNull("Should have created the znode:" + node2, zk.exists(node2, false)); }
/** * Basic test of read-only client functionality. Tries to read and write * during read-only mode, then regains a quorum and tries to write again. */ @Test(timeout = 90000) public void testReadOnlyClient() throws Exception { CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected final String data = "Data to be read in RO mode"; final String node = "/tnode"; zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.reset(); qu.shutdown(2); watcher.waitForConnected(CONNECTION_TIMEOUT); // read operation during r/o mode String remoteData = new String(zk.getData(node, false, null)); Assert.assertEquals(data, remoteData); try { zk.setData(node, "no way".getBytes(), -1); Assert.fail("Write operation has succeeded during RO mode"); } catch (NotReadOnlyException e) { // ok } watcher.reset(); qu.start(2); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); watcher.waitForConnected(CONNECTION_TIMEOUT); zk.setData(node, "We're in the quorum now".getBytes(), -1); zk.close(); }
/** * Basic test of read-only client functionality. Tries to read and write * during read-only mode, then regains a quorum and tries to write again. */ @Test public void testReadOnlyClient() throws Exception { CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected final String data = "Data to be read in RO mode"; final String node = "/tnode"; zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.reset(); qu.shutdown(2); watcher.waitForConnected(CONNECTION_TIMEOUT); // read operation during r/o mode String remoteData = new String(zk.getData(node, false, null)); Assert.assertEquals(data, remoteData); try { zk.setData(node, "no way".getBytes(), -1); Assert.fail("Write operation has succeeded during RO mode"); } catch (NotReadOnlyException e) { // ok } watcher.reset(); qu.start(2); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); watcher.waitForConnected(CONNECTION_TIMEOUT); zk.setData(node, "We're in the quorum now".getBytes(), -1); zk.close(); }
/** * Basic test of read-only client functionality. Tries to read and write * during read-only mode, then regains a quorum and tries to write again. */ @Test(timeout = 90000) public void testReadOnlyClient() throws Exception { CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected final String data = "Data to be read in RO mode"; final String node = "/tnode"; zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.reset(); qu.shutdown(2); zk.close(); // Re-connect the client (in case we were connected to the shut down // server and the local session was not persisted). zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); // read operation during r/o mode String remoteData = new String(zk.getData(node, false, null)); Assert.assertEquals(data, remoteData); try { zk.setData(node, "no way".getBytes(), -1); Assert.fail("Write operation has succeeded during RO mode"); } catch (NotReadOnlyException e) { // ok } watcher.reset(); qu.start(2); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); zk.close(); watcher.reset(); // Re-connect the client (in case we were connected to the shut down // server and the local session was not persisted). zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); zk.setData(node, "We're in the quorum now".getBytes(), -1); zk.close(); }