blob: 8d6e30e2bb608810723d1154ecf5e69693607a21 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jwplayer.sqe.util;
import kafka.utils.ZkUtils;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import java.io.IOException;
public class ZookeeperTestServer {
private CuratorFramework zookeeperTestServer;
private TestingServer testingServer;
public ZookeeperTestServer() {
try {
testingServer = new TestingServer();
ExponentialBackoffRetry backoffRetry = new ExponentialBackoffRetry(1000, 4);
zookeeperTestServer = CuratorFrameworkFactory.newClient(getConnectionString(), backoffRetry);
} catch (Exception ex) {
throw new RuntimeException("Couldn't start test ZK server", ex);
}
}
public String getConnectionString() {
return testingServer.getConnectString();
}
public ZkUtils getZkUtils() {
ZkClient zkClient = new ZkClient(getConnectionString());
return new ZkUtils(zkClient, new ZkConnection(getConnectionString()), false);
}
public void shutdown() {
try {
if (zookeeperTestServer.getState().equals(CuratorFrameworkState.STARTED)) {
zookeeperTestServer.close();
}
testingServer.close();
} catch (IOException ex) {
throw new RuntimeException("Couldn't shutdown test ZK server", ex);
}
}
}