com.spotify.helios.ZooKeeperTestingClusterManager Maven / Gradle / Ivy
/*
* Copyright (c) 2014 Spotify AB.
*
* 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.spotify.helios;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.InstanceSpec;
import org.apache.curator.test.TestingCluster;
import org.apache.curator.test.TestingZooKeeperServer;
import org.apache.zookeeper.KeeperException;
import org.junit.Rule;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;
import static org.apache.commons.io.FileUtils.deleteDirectory;
/**
* A ZooKeeperTestManager that uses the {@link org.apache.curator.test.TestingServer}
* to run an in-process ZooKeeper cluster.
*/
public class ZooKeeperTestingClusterManager implements ZooKeeperTestManager {
@Rule public final TemporaryPorts temporaryPorts = TemporaryPorts.create();
private final Path tempDir;
private List zkPeers;
private List zkAddresses;
private List zkServers;
private List peerCurators;
private CuratorFramework curator;
private TestingCluster cluster;
public ZooKeeperTestingClusterManager() {
try {
tempDir = Files.createTempDirectory("helios-zk");
} catch (IOException e) {
throw Throwables.propagate(e);
}
start();
}
@Override
public void ensure(String path) throws Exception {
curator.newNamespaceAwareEnsurePath(path).ensure(curator.getZookeeperClient());
}
@Override
public void close() {
try {
for (CuratorFramework curator : peerCurators) {
curator.close();
}
curator.close();
stop();
deleteDirectory(tempDir.toFile());
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
@Override
public String connectString() {
return connectString(zkAddresses);
}
@Override
public CuratorFramework curator() {
return curator;
}
@Override
public void awaitUp(long timeout, TimeUnit timeunit) throws TimeoutException {
Polling.awaitUnchecked(timeout, timeunit, new Callable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy