Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

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


Copy link to this message
-
Re: Embedded ZK instance for unit testing
Thanks for the references guys.

Turns out... my embedded ZK does actually work... on linux, not mac.  :(

On Dec 3, 2012, at 11:48 AM, "Carroll James (Nokia-LC/Malvern)" <[EMAIL PROTECTED]> wrote:

> 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");