当前位置: 首页>>代码示例>>Java>>正文


Java ClientBase.setupTestEnv方法代码示例

本文整理汇总了Java中org.apache.zookeeper.test.ClientBase.setupTestEnv方法的典型用法代码示例。如果您正苦于以下问题:Java ClientBase.setupTestEnv方法的具体用法?Java ClientBase.setupTestEnv怎么用?Java ClientBase.setupTestEnv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.zookeeper.test.ClientBase的用法示例。


在下文中一共展示了ClientBase.setupTestEnv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testAutoCreateDataLogDir

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Test verifies the auto creation of data dir and data log dir.
 */
@Test(timeout = 30000)
public void testAutoCreateDataLogDir() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, false);
    String args[] = new String[1];
    args[0] = main.confFile.toString();
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));

    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo", null, null)),
            "foobar");
    zk.close();

    main.shutdown();
    main.join();
    main.deleteDirs();

    Assert.assertTrue("waiting for server down", ClientBase
            .waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:35,代码来源:ZooKeeperServerMainTest.java

示例2: testJMXRegistrationWithNIO

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
@Test
public void testJMXRegistrationWithNIO() throws Exception {
    ClientBase.setupTestEnv();
    File tmpDir_1 = ClientBase.createTmpDir();
    ServerCnxnFactory server_1 = startServer(tmpDir_1);
    File tmpDir_2 = ClientBase.createTmpDir();
    ServerCnxnFactory server_2 = startServer(tmpDir_2);

    server_1.shutdown();
    server_2.shutdown();

    deleteFile(tmpDir_1);
    deleteFile(tmpDir_2);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:15,代码来源:ZooKeeperServerMainTest.java

示例3: testJMXRegistrationWithNetty

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
@Test
public void testJMXRegistrationWithNetty() throws Exception {
    String originalServerCnxnFactory = System
            .getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
    System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY,
            NettyServerCnxnFactory.class.getName());
    try {
        ClientBase.setupTestEnv();
        File tmpDir_1 = ClientBase.createTmpDir();
        ServerCnxnFactory server_1 = startServer(tmpDir_1);
        File tmpDir_2 = ClientBase.createTmpDir();
        ServerCnxnFactory server_2 = startServer(tmpDir_2);

        server_1.shutdown();
        server_2.shutdown();

        deleteFile(tmpDir_1);
        deleteFile(tmpDir_2);
    } finally {
        // setting back
        if (originalServerCnxnFactory == null
                || originalServerCnxnFactory.isEmpty()) {
            System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
        } else {
            System.setProperty(
                    ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY,
                    originalServerCnxnFactory);
        }
    }
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:31,代码来源:ZooKeeperServerMainTest.java

示例4: testMinMaxSessionTimeOut

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Test verifies that the server is able to redefine the min/max session
 * timeouts
 */
@Test
public void testMinMaxSessionTimeOut() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();
    final int tickTime = 2000;
    final int minSessionTimeout = tickTime * 2 - 100;
    final int maxSessionTimeout = 20 * tickTime + 1000;
    final String configs = "maxSessionTimeout=" + maxSessionTimeout + "\n"
            + "minSessionTimeout=" + minSessionTimeout + "\n";
    MainThread main = new MainThread(CLIENT_PORT, true, configs);
    main.start();

    String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    // create session with min value
    verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT);
    verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout,
            HOSTPORT);
    // create session with max value
    verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT);
    verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout,
            HOSTPORT);
    main.shutdown();

    Assert.assertTrue("waiting for server down", ClientBase
            .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:34,代码来源:ZooKeeperServerMainTest.java

示例5: testReadOnlySnapshotDir

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Tests that the ZooKeeper server will fail to start if the
 * snapshot directory is read only.
 *
 * This test will fail if it is executed as root user.
 */
@Test(timeout = 30000)
public void testReadOnlySnapshotDir() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();

    // Start up the ZK server to automatically create the necessary directories
    // and capture the directory where data is stored
    MainThread main = new MainThread(CLIENT_PORT, true);
    File tmpDir = main.tmpDir;
    main.start();
    Assert.assertTrue("waiting for server being up", ClientBase
            .waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT / 2));
    main.shutdown();

    // Make the snapshot directory read only
    File snapDir = new File(main.dataDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
    snapDir.setWritable(false);

    // Restart ZK and observe a failure
    main = new MainThread(CLIENT_PORT, false, tmpDir);
    main.start();

    Assert.assertFalse("waiting for server being up", ClientBase
            .waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT / 2));

    main.shutdown();

    snapDir.setWritable(true);

    main.deleteDirs();
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:40,代码来源:ZooKeeperServerMainTest.java

示例6: testWithOnlyMinSessionTimeout

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Test verifies that the server is able to redefine if user configured only
 * minSessionTimeout limit
 */
@Test
public void testWithOnlyMinSessionTimeout() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();
    final int tickTime = 2000;
    final int minSessionTimeout = tickTime * 2 - 100;
    int maxSessionTimeout = 20 * tickTime;
    final String configs = "minSessionTimeout=" + minSessionTimeout + "\n";
    MainThread main = new MainThread(CLIENT_PORT, true, configs);
    main.start();

    String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    // create session with min value
    verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT);
    verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout,
            HOSTPORT);
    // create session with max value
    verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT);
    verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout,
            HOSTPORT);
    main.shutdown();
    Assert.assertTrue("waiting for server down", ClientBase
            .waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:32,代码来源:ZooKeeperServerMainTest.java

示例7: testStandalone

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Verify the ability to start a standalone server instance.
 */
@Test
public void testStandalone() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, true);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));


    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar");
    zk.close();

    main.shutdown();
    main.join();
    main.deleteDirs();

    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:34,代码来源:ZooKeeperServerMainTest.java

示例8: setup

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
@Before
public void setup() {
    ClientBase.setupTestEnv();
    System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest",
            "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/);
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:7,代码来源:ReconfigBackupTest.java

示例9: testValidIpv6AddressInQuorum

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
@Test
public void testValidIpv6AddressInQuorum() throws Exception {
    assumeIPv6Available();

    ClientBase.setupTestEnv();

    // setup the logger to capture all logs
    Layout layout =
            Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setImmediateFlush(true);
    appender.setThreshold(Level.INFO);
    Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
    qlogger.addAppender(appender);

    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();

        String quorumCfgSection =
                "server.1=127.0.0.1:" + PortAssignment.unique()
                + ":" + PortAssignment.unique()
                + "\nserver.2=[0:0:0:0:0:0:0:1]:" + PortAssignment.unique()
                + ":" + PortAssignment.unique();

        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);

        q1.start();
        q2.start();

        Assert.assertTrue("waiting for server 1 being up",
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));

        Assert.assertTrue("waiting for server 2 being up",
                ClientBase.waitForServerUp("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));

        q1.shutdown();
        q2.shutdown();

        Assert.assertTrue("waiting for server 1 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));

        Assert.assertTrue("waiting for server 2 down",
                ClientBase.waitForServerDown("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));

    } finally {
        qlogger.removeAppender(appender);
    }

    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p =
            Pattern.compile(".*Resolved hostname: 0:0:0:0:0:0:0:1.*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("IPv6 address resolved", found);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:70,代码来源:QuorumPeerMainTest.java

示例10: testInvalidIpv6AddressInQuorum

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
@Test
public void testInvalidIpv6AddressInQuorum() throws Exception {
    assumeIPv6Available();

    ClientBase.setupTestEnv();

    // setup the logger to capture all logs
    Layout layout =
            Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setImmediateFlush(true);
    appender.setThreshold(Level.INFO);
    Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
    qlogger.addAppender(appender);

    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();

        String quorumCfgSection =
                "server.1=127.0.0.1:" + PortAssignment.unique()
                + ":" + PortAssignment.unique()
                + "\nserver.2=[0:0:0:0:0:0:0:1:" + PortAssignment.unique()
                + ":" + PortAssignment.unique();

        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();

        boolean isup =
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
                        30000);

        Assert.assertFalse("Server never came up", isup);

        q1.shutdown();

        Assert.assertTrue("waiting for server 1 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));

    } finally {
        qlogger.removeAppender(appender);
    }

    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p =
            Pattern.compile(".*QuorumPeerConfig\\$ConfigException.*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("complains about configuration", found);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:59,代码来源:QuorumPeerMainTest.java

示例11: testQuorum

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Starts a quorum of two servers and tests that we can query both AdminServers.
 */
@Test
public void testQuorum() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT_QP1 = PortAssignment.unique();
    final int CLIENT_PORT_QP2 = PortAssignment.unique();

    final int ADMIN_SERVER_PORT1 = PortAssignment.unique();
    final int ADMIN_SERVER_PORT2 = PortAssignment.unique();

    String quorumCfgSection = String.format
        ("server.1=127.0.0.1:%d:%d;%d\nserver.2=127.0.0.1:%d:%d;%d",
         PortAssignment.unique(), PortAssignment.unique(), CLIENT_PORT_QP1,
         PortAssignment.unique(), PortAssignment.unique(), CLIENT_PORT_QP2
        );
    QuorumPeerTestBase.MainThread q1 = new QuorumPeerTestBase.MainThread(
            1, CLIENT_PORT_QP1, ADMIN_SERVER_PORT1, quorumCfgSection, null);
    q1.start();

    // Since JettyAdminServer reads a system property to determine its port,
    // make sure it initializes itself before setting the system property
    // again with the second port number
    Thread.sleep(500);

    QuorumPeerTestBase.MainThread q2 = new QuorumPeerTestBase.MainThread(
            2, CLIENT_PORT_QP2, ADMIN_SERVER_PORT2, quorumCfgSection, null);
    q2.start();

    Thread.sleep(500);

    Assert.assertTrue("waiting for server 1 being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
            ClientBase.CONNECTION_TIMEOUT));
    Assert.assertTrue("waiting for server 2 being up",
                    ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2,
                    ClientBase.CONNECTION_TIMEOUT));

    queryAdminServer(ADMIN_SERVER_PORT1);
    queryAdminServer(ADMIN_SERVER_PORT2);

    q1.shutdown();
    q2.shutdown();

    Assert.assertTrue("waiting for server 1 down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
                    ClientBase.CONNECTION_TIMEOUT));
    Assert.assertTrue("waiting for server 2 down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2,
                    ClientBase.CONNECTION_TIMEOUT));
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:54,代码来源:JettyAdminServerTest.java

示例12: testQuorumDefaults

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Verify handling of quorum defaults
 * * default electionAlg is fast leader election
 */
@Test
public void testQuorumDefaults() throws Exception {
    ClientBase.setupTestEnv();

    // setup the logger to capture all logs
    Layout layout =
        Logger.getRootLogger().getAppender("CONSOLE").getLayout();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    WriterAppender appender = new WriterAppender(layout, os);
    appender.setImmediateFlush(true);
    appender.setThreshold(Level.INFO);
    Logger zlogger = Logger.getLogger("org.apache.zookeeper");
    zlogger.addAppender(appender);

    try {
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();

        String quorumCfgSection =
            "server.1=127.0.0.1:" + PortAssignment.unique()
            + ":" + PortAssignment.unique()
            + "\nserver.2=127.0.0.1:" + PortAssignment.unique()
            + ":" + PortAssignment.unique();

        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
        q1.start();
        q2.start();

        Assert.assertTrue("waiting for server 1 being up",
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
                        CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 being up",
                ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2,
                        CONNECTION_TIMEOUT));

        q1.shutdown();
        q2.shutdown();

        Assert.assertTrue("waiting for server 1 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
                        ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("waiting for server 2 down",
                ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2,
                        ClientBase.CONNECTION_TIMEOUT));

    } finally {
        zlogger.removeAppender(appender);
    }
    os.close();
    LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
    String line;
    boolean found = false;
    Pattern p =
        Pattern.compile(".*FastLeaderElection.*");
    while ((line = r.readLine()) != null) {
        found = p.matcher(line).matches();
        if (found) {
            break;
        }
    }
    Assert.assertTrue("fastleaderelection used", found);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:68,代码来源:QuorumPeerMainTest.java

示例13: testNonRecoverableError

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
 * Test to verify that even after non recoverable error (error while
 * writing transaction log) on ZooKeeper service will be available
 */
@Test(timeout = 30000)
public void testNonRecoverableError() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, true);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));


    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");

    // inject problem in server
    ZooKeeperServer zooKeeperServer = main.getCnxnFactory()
            .getZooKeeperServer();
    FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(
            snapLog.getDataDir(), snapLog.getSnapDir()) {
        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    zooKeeperServer.setZKDatabase(newDB);

    try {
        // do create operation, so that injected IOException is thrown
        zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        fail("IOException is expected as error is injected in transaction log commit funtionality");
    } catch (Exception e) {
        // do nothing
    }
    zk.close();
    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
    fileTxnSnapLogWithError.close();
    main.shutdown();
    main.deleteDirs();
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:57,代码来源:ZooKeeperServerMainTest.java

示例14: testNonRecoverableError

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
 * Test to verify that even after non recoverable error (error while
 * writing transaction log), ZooKeeper is still available.
 */
@Test(timeout = 30000)
public void testNonRecoverableError() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, true, null);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));


    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");

    // inject problem in server
    ZooKeeperServer zooKeeperServer = main.getCnxnFactory()
            .getZooKeeperServer();
    FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(
            snapLog.getDataDir(), snapLog.getSnapDir()) {
        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    zooKeeperServer.setZKDatabase(newDB);

    try {
        // do create operation, so that injected IOException is thrown
        zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        fail("IOException is expected as error is injected in transaction log commit funtionality");
    } catch (Exception e) {
        // do nothing
    }
    zk.close();
    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
    fileTxnSnapLogWithError.close();
    main.shutdown();
    main.deleteDirs();
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:57,代码来源:ZooKeeperServerMainTest.java

示例15: testObserverConvertedToParticipantDuringFLE

import org.apache.zookeeper.test.ClientBase; //导入方法依赖的package包/类
/**
 * Tests conversion of observer to participant AFTER new config was already
 * committed. Old config: servers 0 (participant), 1 (participant), 2
 * (observer) New config: servers 2 (participant), 3 (participant) We start
 * server 2 with old config and start server 3 with new config. All other
 * servers are down. In order to terminate FLE, server 3 must 'convince'
 * server 2 to adopt the new config and turn into a participant.
 */
@Test
public void testObserverConvertedToParticipantDuringFLE() throws Exception {
    ClientBase.setupTestEnv();

    final int SERVER_COUNT = 4;
    int[][] ports = generatePorts(SERVER_COUNT);
    String currentQuorumCfgSection, nextQuorumCfgSection;

    // generate old config string
    Set<Integer> observers = new HashSet<Integer>();
    observers.add(2);
    StringBuilder sb = generateConfig(3, ports, observers);
    currentQuorumCfgSection = sb.toString();

    // generate new config string
    ArrayList<String> allServersNext = new ArrayList<String>();
    sb = new StringBuilder();
    for (int i = 2; i < SERVER_COUNT; i++) {
        String server = "server." + i + "=localhost:" + ports[i][0] + ":"
                + ports[i][1] + ":participant;localhost:" + ports[i][2];
        allServersNext.add(server);
        sb.append(server + "\n");
    }
    nextQuorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];

    // start server 2 with old config, where it is an observer
    mt[2] = new MainThread(2, ports[2][2], currentQuorumCfgSection,
            true, "100000000");
    mt[2].start();
    zk[2] = new ZooKeeper("127.0.0.1:" + ports[2][2],
            ClientBase.CONNECTION_TIMEOUT, this);

    // start server 3 with new config
    mt[3] = new MainThread(3, ports[3][2], nextQuorumCfgSection,
            true, "200000000");
    mt[3].start();
    zk[3] = new ZooKeeper("127.0.0.1:" + ports[3][2],
            ClientBase.CONNECTION_TIMEOUT, this);

    for (int i = 2; i < SERVER_COUNT; i++) {
        Assert.assertTrue("waiting for server " + i + " being up",
                ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2],
                        CONNECTION_TIMEOUT * 2));
        ReconfigTest.testServerHasConfig(zk[i], allServersNext, null);
    }

    Assert.assertEquals(nextQuorumCfgSection + "version=200000000",
            ReconfigTest.testServerHasConfig(zk[2], null, null));
    Assert.assertEquals(nextQuorumCfgSection + "version=200000000",
            ReconfigTest.testServerHasConfig(zk[3], null, null));
    ReconfigTest.testNormalOperation(zk[2], zk[2]);
    ReconfigTest.testNormalOperation(zk[3], zk[2]);

    for (int i = 2; i < SERVER_COUNT; i++) {
        zk[i].close();
        mt[i].shutdown();
    }
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:70,代码来源:ReconfigRecoveryTest.java


注:本文中的org.apache.zookeeper.test.ClientBase.setupTestEnv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。