GithubHelp home page GithubHelp logo

isabella232 / curator-test-rule Goto Github PK

View Code? Open in Web Editor NEW

This project forked from palantir/curator-test-rule

0.0 0.0 0.0 171 KB

JUnit TestRule for running tests with ZooKeeperServer and CuratorFrameworks

License: Apache License 2.0

Java 98.60% Groovy 1.40%

curator-test-rule's Introduction

curator-test-rule Build Status

Summary

This project provides a JUnit TestRule that manages the startup and shutdown of the underlying ZooKeeperServer. The TestRule also includes a way of getting CuratorFramework instances that automatically connect and disconnect from the underlying ZooKeeperServer based on the scope of rule.

Why didn't we use curator-test

  • curator-test provides a TestingServer base class, which presents a problem when a testing class needs to extend two different base classes. Using a JUnit TestRule gives us multiple advantages, including the option to start and stop a test server once per test class (@ClassRule) or test method (@Rule).
  • There was a race condition in curator-test where if we didn't specify a port, it would bind 0 to get a free port, then unbind from that port, then try to bind to that port later in the code.
  • The underlying ZooKeeperServer timeout cannot be configured since we are locked into the configurations in InstanceSpec
  • Because of a blocking cnxnFactory.join() call in ZooKeeperServerMain#runFromConfig, curator-test had to do several hacks like spawning a new thread and synchronizing on the status of the ZooKeeperServer

Installation

Include curator-test-rule as a dependency in your build system

###In Gradle

...
repositories {
    mavenCentral()
}

dependencies {
    compile "com.palantir:curator-test-rule:CURRENT_VERSION"
}
...

###In Maven

  ...
  <dependencies>
    <dependency>
      <groupId>com.palantir</groupId>
      <artifactId>curator-test-rule</artifactId>
      <version>CURRENT_VERSION</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  ...

How to use it

public final class LocalZooKeeperRuleExample {

    @Rule                                                  // or @ClassRule
    public ZooKeeperRule rule1 = new LocalZooKeeperRule(); // or SharedZooKeeperRule()

    @Test
    public void testCase() {
        CuratorFramework client = rule1.getClient();
        client.getState();

        // do something
    }
}

There are two subclasses of ZooKeeperRule as of now: LocalZooKeeperRule and SharedZooKeeperRule. LocalZooKeeperRule starts and closes the underlying server based on the scope of the TestRule. SharedZooKeeperRule also does this for serial execution. For concurrent execution (such as by using a ParallelSuite), all SharedZooKeeperRules using the same port will share the same underlying server.

Please read the Javadocs for LocalZooKeeperRule and SharedZooKeeperRule for some caveats regarding these classes.

Javadoc

The javadoc is at http://palantir.github.io/curator-test-rule/javadoc/. It is highly recommended to read the javadoc for some caveats when using curator-test-rule.

License

See LICENSE.txt

curator-test-rule's People

Contributors

justinuang avatar derekcicerone avatar jacob-meacham avatar punya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.