Home | About | Sematext search-lucene.com search-hadoop.com
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB
 Search Hadoop and all its subprojects:

Switch to Plain View
Zookeeper >> mail # user >> Embedded ZK instance for unit testing


+
Joshua Blatt 2012-12-03, 19:39
+
Jordan Zimmerman 2012-12-03, 19:44
Copy link to this message
-
RE: Embedded ZK instance for unit testing
We do it in our tests.

Project page: https://github.com/Dempsy/Dempsy
Embedded server for testing: https://github.com/Dempsy/Dempsy/blob/master/lib-dempsyimpl/src/test/java/com/nokia/dempsy/cluster/zookeeper/ZookeeperTestServer.java

Also, it creates the connection string dynamically and therefore starts the server on an ephemeral port.

-----Original Message-----
From: Jordan Zimmerman [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 03, 2012 2:45 PM
To: [EMAIL PROTECTED]
Subject: Re: Embedded ZK instance for unit testing

Curator has a test ZK server and cluster: curator-test

===================Jordan Zimmerman

On Dec 3, 2012, at 11:39 AM, Joshua Blatt <[EMAIL PROTECTED]> wrote:

> Hi guys,
>
> I've seen a few threads on why it's a bad idea to try to embed ZK in your app.   I'm not trying to do that for my production app.   I am trying to do that for unit tests.   I could try to mock out ZK but I'm concerned that'd be both a lot of typing and wouldn't, well, test against the real thing.
>
> Has anyone done this before?
>
> When I tried doing this I saw the ZK session request initiated by the unit test go through (after first trying IPv6, then falling back to IPv4), but then the first operation attempted timed out after 40 seconds.   The same test case passes against a 3 node external ZK cluster.
>
> Note that I'm spinning up a new ZK instance for every test run - I've tried inserting a several minute sleep after startup to give it time to initialize, but that didn't make a difference.
>
>
> import java.io.File;
> import java.util.UUID;
> import java.util.concurrent.Semaphore; import
> org.apache.commons.io.FileUtils; import
> org.apache.zookeeper.server.ZooKeeperServerMain;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> /**
> * A single-node embedded zookeeper server. <p>
> * WARNING: This class is only intended for unit testing. It's not
> production-
> * grade.
> *
> * @author jblatt
> */
> public class EmbeddedZooKeeper extends ZooKeeperServerMain {
>
>    private static final Logger LOGGER >            LoggerFactory.getLogger(EmbeddedZooKeeper.class);
>    private final File dataDir = new File(
>            FileUtils.getTempDirectoryPath() + "/"
>            + UUID.randomUUID().toString());
>    private final int clientPort;
>    private final Semaphore isRunning = new Semaphore(0);
>    private Thread thread;
>
>    public EmbeddedZooKeeper(int clientPort) throws ZooKeeperException {
>        this.clientPort = clientPort;
>    }
>
>    public void start() throws ZooKeeperException {
>        try {
>            FileUtils.forceMkdir(dataDir);
>
>            final EmbeddedZooKeeper self = this;
>
>            thread = new Thread() {
>
>                @Override
>                public void run() {
>                    try {
>                        LOGGER.info("Starting ZK server");
>
>                        self.initializeAndRun(new String[]{
>                                    Integer.toString(clientPort),
>                                    dataDir.getAbsolutePath()});
>
>                        LOGGER.info("Started ZK server");
>
>                        isRunning.acquire();
>
>                        LOGGER.info("Stopped ZK server");
>
>                    } catch (Throwable t) {
>                        LOGGER.error("Failure in embedded ZooKeeper", t);
>                    }
>                }
>            };
>
>            thread.start();
>        } catch (Throwable t) {
>            throw new ZooKeeperException("Cannot start embedded zookeeper", t);
>        }
>    }
>
>    public void stop() throws ZooKeeperException {
>        try {
>            LOGGER.info("Stopping ZK server");
>
>            super.shutdown();
>
>            isRunning.release();
>
>            thread.join();
>        } catch (Throwable t) {
>            throw new ZooKeeperException("Cannot stop embedded zookeeper", t);
>        } finally {
>            // Unconditionally try to cleanup tmp dirs even if this is done

The information contained in this communication may be CONFIDENTIAL and is intended only for the use of the recipient(s) named above.  If you are not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication, or any of its contents, is strictly prohibited.  If you have received this communication in error, please notify the sender and delete/destroy the original message and any copy of it from your computer or paper files.
+
Joshua Blatt 2012-12-03, 20:56
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB