Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void testReconnect() throws Exception
server.stop();
Thread.sleep(1000);

server = new TestingServer(serverPort, tempDirectory);
server.restart();
Assert.assertTrue(client.blockUntilConnectedOrTimedOut());
byte[] readData = client.getZooKeeper().getData("/test", false, null);
Assert.assertEquals(readData, writtenData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testRetryLoopWithFailure() throws Exception

case 2:
{
server = new TestingServer(serverPort, tempDirectory);
server.restart();
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ else if ( newState == ConnectionState.RECONNECTED )

timing.sleepABit();

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));

timing.sleepABit();
Expand Down Expand Up @@ -145,7 +145,7 @@ else if ( newState == ConnectionState.RECONNECTED )

timing.sleepABit();

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));

timing.sleepABit();
Expand Down Expand Up @@ -208,7 +208,7 @@ else if ( newState == ConnectionState.RECONNECTED )

timing.sleepABit();

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));

timing.sleepABit();
Expand Down Expand Up @@ -240,7 +240,6 @@ public void testBasic() throws Exception
int serverPort = server.getPort();

server.stop(); // cause the next delete to fail
server = null;
try
{
client.delete().forPath(PATH);
Expand All @@ -251,11 +250,10 @@ public void testBasic() throws Exception
// expected
}

server = new TestingServer(serverPort, serverDir);
server.restart();
Assert.assertNotNull(client.checkExists().forPath(PATH));

server.stop(); // cause the next delete to fail
server = null;
try
{
client.delete().guaranteed().forPath(PATH);
Expand All @@ -266,7 +264,7 @@ public void testBasic() throws Exception
// expected
}

server = new TestingServer(serverPort, serverDir);
server.restart();

final int TRIES = 5;
for ( int i = 0; i < TRIES; ++i )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void stateChanged(CuratorFramework client, ConnectionState newState)
// expected
}

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
try
{
client.setData().forPath("/test", "test".getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleep
{
try
{
server = new TestingServer(serverPort, tempDirectory);
server.restart();
}
catch ( Exception e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void stateChanged(CuratorFramework client, ConnectionState newState)

Assert.assertEquals(getLeaders(latches).size(), 0);

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
Assert.assertEquals(waitForALeader(latches, timing).size(), 1); // should reconnect
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ else if ( newState == ConnectionState.LOST )
timing.sleepABit();
debugLeadershipWaitLatch.countDown();

server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));

Assert.assertFalse(takeLeadershipLatch.await(3, TimeUnit.SECONDS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Object call() throws Exception

server.stop();
Assert.assertTrue(timing.awaitLatch(latch));
server = new TestingServer(server.getPort(), server.getTempDirectory());
server.restart();
}
}
finally
Expand Down
116 changes: 98 additions & 18 deletions curator-test/src/main/java/org/apache/curator/test/TestingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,104 @@ public class TestingServer implements Closeable

/**
* Create the server using a random port
*
* @throws Exception errors
*
* @throws Exception
* errors
*/
public TestingServer() throws Exception
{
this(-1, null);
this(true);
}

/**
* Create the server using a random port
*
* @param start
* True if the server should be started, false otherwise
* @throws Exception
* errors
*/
public TestingServer(boolean start) throws Exception
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start argument is not actually used. Is it meant to be?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, yes it should be passed into the other constructor, will fix this up now.

The start flag is not currently used in any of the unit tests, but I think that it's useful to be able to create a server without starting it. There are cases where you want to start with a server that is not started.

{
this(-1, null, start);
}

/**
* Create the server using the given port
*
* @param port the port
* @throws Exception errors
*
* @param port
* the port
* @throws Exception
* errors
*/
public TestingServer(int port) throws Exception
{
this(port, null);
this(port, true);
}

/**
* Create the server using the given port
*
* @param port
* the port
* @param start
* True if the server should be started, false otherwise
* @throws Exception
* errors
*/
public TestingServer(int port, boolean start) throws Exception
{
this(port, null, start);
}

/**
* Create the server using the given port
*
* @param port the port
* @param tempDirectory directory to use
* @throws Exception errors
*
* @param port
* the port
* @param tempDirectory
* directory to use
* @throws Exception
* errors
*/
public TestingServer(int port, File tempDirectory) throws Exception
{
this(new InstanceSpec(tempDirectory, port, -1, -1, true, -1));
this(port, tempDirectory, true);
}

public TestingServer(InstanceSpec spec) throws Exception
/**
* Create the server using the given port
*
* @param port
* the port
* @param tempDirectory
* directory to use
* @param start
* True if the server should be started, false otherwise
* @throws Exception
* errors
*/
public TestingServer(int port, File tempDirectory, boolean start)
throws Exception
{
this(new InstanceSpec(tempDirectory, port, -1, -1, true, -1), start);
}

public TestingServer(InstanceSpec spec, boolean start) throws Exception
{
this.spec = spec;
testingZooKeeperServer = new TestingZooKeeperServer(new QuorumConfigBuilder(spec));
testingZooKeeperServer.start();
testingZooKeeperServer = new TestingZooKeeperServer(
new QuorumConfigBuilder(spec));

if (start)
{
testingZooKeeperServer.start();
}
}

/**
* Return the port being used
*
*
* @return port
*/
public int getPort()
Expand All @@ -87,14 +144,24 @@ public int getPort()

/**
* Returns the temp directory being used
*
*
* @return directory
*/
public File getTempDirectory()
{
return spec.getDataDirectory();
}

/**
* Start the server
*
* @throws Exception
*/
public void start() throws Exception
{
testingZooKeeperServer.start();
}

/**
* Stop the server without deleting the temp directory
*/
Expand All @@ -103,6 +170,19 @@ public void stop() throws IOException
testingZooKeeperServer.stop();
}

/**
* Restart the server. If the server is currently running it will be stopped
* and restarted. If it's not currently running then it will be started. If
* it has been closed (had close() called on it) then an exception will be
* thrown.
*
* @throws Exception
*/
public void restart() throws Exception
{
testingZooKeeperServer.restart();
}

/**
* Close the server and any open clients and delete the temp directory
*/
Expand All @@ -114,7 +194,7 @@ public void close() throws IOException

/**
* Returns the connection string to use
*
*
* @return connection string
*/
public String getConnectString()
Expand Down
Loading