You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by si...@apache.org on 2012/03/13 07:31:37 UTC

svn commit: r1299984 [2/2] - in /zookeeper/bookkeeper/trunk: ./ bookkeeper-server/ bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/ bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/ bookkeeper-server/src/test/java/org/apache/bo...

Added: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java?rev=1299984&view=auto
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java (added)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java Tue Mar 13 06:31:36 2012
@@ -0,0 +1,133 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.bookkeeper.test;
+
+import java.io.File;
+import java.net.InetSocketAddress;
+
+import org.apache.commons.io.FileUtils;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.ZooDefs.Ids;
+
+import org.apache.zookeeper.server.NIOServerCnxnFactory;
+import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.test.ClientBase;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.Assert.*;
+
+public class ZooKeeperUtil {
+    static final Logger LOG = LoggerFactory.getLogger(ZooKeeperUtil.class);
+
+    // ZooKeeper related variables
+    protected static Integer ZooKeeperDefaultPort = 2181;
+    private final InetSocketAddress zkaddr;
+
+    protected ZooKeeperServer zks;
+    protected ZooKeeper zkc; // zookeeper client
+    protected NIOServerCnxnFactory serverFactory;
+    protected File ZkTmpDir;
+    private final String connectString;
+
+    public ZooKeeperUtil() {
+        zkaddr = new InetSocketAddress(ZooKeeperDefaultPort);
+        connectString= "localhost:" + ZooKeeperDefaultPort;
+    }
+
+    public ZooKeeper getZooKeeperClient() {
+        return zkc;
+    }
+
+    public String getZooKeeperConnectString() {
+        return connectString;
+    }
+
+    public void startServer() throws Exception {
+        // create a ZooKeeper server(dataDir, dataLogDir, port)
+        LOG.debug("Running ZK server");
+        // ServerStats.registerAsConcrete();
+        ClientBase.setupTestEnv();
+        ZkTmpDir = File.createTempFile("zookeeper", "test");
+        ZkTmpDir.delete();
+        ZkTmpDir.mkdir();
+
+        zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort);
+        serverFactory = new NIOServerCnxnFactory();
+        serverFactory.configure(zkaddr, 100);
+        serverFactory.startup(zks);
+
+        boolean b = ClientBase.waitForServerUp(getZooKeeperConnectString(),
+                                               ClientBase.CONNECTION_TIMEOUT);
+        LOG.debug("Server up: " + b);
+
+        // create a zookeeper client
+        LOG.debug("Instantiate ZK Client");
+        final CountDownLatch latch = new CountDownLatch(1);
+        zkc = new ZooKeeper(getZooKeeperConnectString(), 10000,
+                            new Watcher() {
+                                @Override
+                                public void process(WatchedEvent event) {
+                                    // handle session disconnects and expires
+                                    if (event.getState().equals(Watcher.Event.KeeperState.SyncConnected)) {
+                                        latch.countDown();
+                                    }
+                                }
+                            });
+        if (!latch.await(10000, TimeUnit.MILLISECONDS)) {
+            zkc.close();
+            fail("Could not connect to zookeeper server");
+        }
+
+        // initialize the zk client with values
+        zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+    }
+
+    public void killServer() throws Exception {
+        if (zkc != null) {
+            zkc.close();
+        }
+
+        // shutdown ZK server
+        if (serverFactory != null) {
+            serverFactory.shutdown();
+            assertTrue("waiting for server down",
+                       ClientBase.waitForServerDown(getZooKeeperConnectString(),
+                                                    ClientBase.CONNECTION_TIMEOUT));
+        }
+        // ServerStats.unregister();
+        FileUtils.deleteDirectory(ZkTmpDir);
+    }
+}
\ No newline at end of file

Propchange: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native