com.twitter.common.zookeeper.testing
Class ZooKeeperTestServer

java.lang.Object
  extended by com.twitter.common.zookeeper.testing.ZooKeeperTestServer
Direct Known Subclasses:
AngryBirdZooKeeperServer

public class ZooKeeperTestServer
extends Object

A helper class for starting in-process ZooKeeper server and clients.

This is ONLY meant to be used for testing.


Field Summary
static Amount<Integer,Time> DEFAULT_SESSION_TIMEOUT
          The default session timeout for clients created by servers constructed with ZooKeeperTestServer(int, ShutdownRegistry).
protected  org.apache.zookeeper.server.ZooKeeperServer zooKeeperServer
           
 
Constructor Summary
ZooKeeperTestServer(int port, ShutdownRegistry shutdownRegistry)
           
ZooKeeperTestServer(int port, ShutdownRegistry shutdownRegistry, Amount<Integer,Time> defaultSessionTimeout)
           
 
Method Summary
 ZooKeeperClient createClient()
          Returns a new unauthenticated zookeeper client connected to the in-process zookeeper server with the default session timeout.
 ZooKeeperClient createClient(Amount<Integer,Time> sessionTimeout)
          Returns a new unauthenticated zookeeper client connected to the in-process zookeeper server with a custom sessionTimeout.
 ZooKeeperClient createClient(Amount<Integer,Time> sessionTimeout, ZooKeeperClient.Credentials credentials)
          Returns a new authenticated zookeeper client connected to the in-process zookeeper server with a custom sessionTimeout.
 ZooKeeperClient createClient(ZooKeeperClient.Credentials credentials)
          Returns a new authenticated zookeeper client connected to the in-process zookeeper server with the default session timeout.
 void expireClientSession(ZooKeeperClient zkClient)
          Expires the active session for the given client.
 int getPort()
          Returns the current port to connect to the in-process zookeeper instance.
 void restartNetwork()
          Starts zookeeper back up on the last used port.
 void shutdownNetwork()
          Shuts down the in-process zookeeper network server.
 int startNetwork()
          Starts zookeeper up on the configured port.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SESSION_TIMEOUT

public static final Amount<Integer,Time> DEFAULT_SESSION_TIMEOUT
The default session timeout for clients created by servers constructed with ZooKeeperTestServer(int, ShutdownRegistry).


zooKeeperServer

protected final org.apache.zookeeper.server.ZooKeeperServer zooKeeperServer
Constructor Detail

ZooKeeperTestServer

public ZooKeeperTestServer(int port,
                           ShutdownRegistry shutdownRegistry)
                    throws IOException
Parameters:
port - the port to start the zoo keeper server on - 0 picks an ephemeral port
shutdownRegistry - a registry that will be used to register client and server shutdown commands. It is up to the caller to execute the registered actions at an appropriate time.
Throws:
IOException - if there was aproblem creating the server's database

ZooKeeperTestServer

public ZooKeeperTestServer(int port,
                           ShutdownRegistry shutdownRegistry,
                           Amount<Integer,Time> defaultSessionTimeout)
                    throws IOException
Parameters:
port - the port to start the zoo keeper server on - 0 picks an ephemeral port
shutdownRegistry - a registry that will be used to register client and server shutdown commands. It is up to the caller to execute the registered actions at an appropriate time.
defaultSessionTimeout - the default session timeout for clients created with createClient().
Throws:
IOException - if there was aproblem creating the server's database
Method Detail

startNetwork

public final int startNetwork()
                       throws IOException,
                              InterruptedException
Starts zookeeper up on the configured port. If the configured port is the ephemeral port (@{code 0}), then the actual chosen port is returned.

Throws:
IOException
InterruptedException

restartNetwork

public final void restartNetwork()
                          throws IOException,
                                 InterruptedException
Starts zookeeper back up on the last used port.

Throws:
IOException
InterruptedException

shutdownNetwork

public final void shutdownNetwork()
Shuts down the in-process zookeeper network server.


expireClientSession

public final void expireClientSession(ZooKeeperClient zkClient)
                               throws ZooKeeperClient.ZooKeeperConnectionException,
                                      InterruptedException
Expires the active session for the given client. The client should be one returned from createClient().

Parameters:
zkClient - the client to expire
Throws:
ZooKeeperClient.ZooKeeperConnectionException - if a problem is encountered connecting to the local zk server while trying to expire the session
InterruptedException - if interrupted while requesting expiration

getPort

public final int getPort()
Returns the current port to connect to the in-process zookeeper instance.


createClient

public final ZooKeeperClient createClient()
Returns a new unauthenticated zookeeper client connected to the in-process zookeeper server with the default session timeout.


createClient

public final ZooKeeperClient createClient(ZooKeeperClient.Credentials credentials)
Returns a new authenticated zookeeper client connected to the in-process zookeeper server with the default session timeout.


createClient

public final ZooKeeperClient createClient(Amount<Integer,Time> sessionTimeout)
Returns a new unauthenticated zookeeper client connected to the in-process zookeeper server with a custom sessionTimeout.


createClient

public final ZooKeeperClient createClient(Amount<Integer,Time> sessionTimeout,
                                          ZooKeeperClient.Credentials credentials)
Returns a new authenticated zookeeper client connected to the in-process zookeeper server with a custom sessionTimeout.