com.api6.zkclient.util.TestUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.api6.zkclient.util.TestUtil.java

Source

/**
 *Copyright 2016 zhaojie
 *
 *Licensed 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.api6.zkclient.util;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.junit.rules.TemporaryFolder;

import com.api6.zkclient.exception.ZKException;

/**
 * 
 * @author: zhaojie/zh_jie@163.com.com 
 * @version: 2016527 ?10:13:46
 */
public class TestUtil {

    /**
     * callableexpectedValue
     * @param expectedValue
     * @param callable
     * @param timeUnit
     * @param timeout
     * @throws Exception 
     * @return T callable
     */
    public static <T> T waitUntil(T expectedValue, Callable<T> callable, TimeUnit timeUnit, long timeout)
            throws Exception {
        long startTime = System.currentTimeMillis();
        do {
            T actual = callable.call();
            if (expectedValue.equals(actual)) {
                System.out.println("TestUtil.waitUntil expected");
                return actual;
            }
            if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
                System.out.println("TestUtil.waitUntil timeout!");
                return actual;
            }
            Thread.sleep(300);
        } while (true);
    }

    /**
     * ??ZooKeeper server
     * @param serverName
     * @param port
     * @return 
     * @return ZKServer
     * @author: zhaojie/zh_jie@163.com 
     */
    public static ZKServer startServer(String serverName, int port) {
        String dataPath = "./target/test-classes/" + serverName + "/data";
        String logPath = "./target/test-classes/" + serverName + "/log";
        try {
            FileUtils.deleteDirectory(new File(dataPath));
            FileUtils.deleteDirectory(new File(logPath));
        } catch (IOException e) {
            throw new ZKException("start server error!", e);
        }
        return startServer(dataPath, logPath, port);
    }

    public static ZKServer startZkServer(TemporaryFolder temporaryFolder, int port) throws IOException {
        File dataFolder = temporaryFolder.newFolder("data");
        File logFolder = temporaryFolder.newFolder("log");
        return startServer(dataFolder.getAbsolutePath(), logFolder.getAbsolutePath(), port);
    }

    private static ZKServer startServer(String dataPath, String logPath, int port) {
        ZKServer zkServer = new ZKServer(dataPath, logPath, port, ZKServer.DEFAULT_TICK_TIME, 100);
        zkServer.start();
        return zkServer;
    }
}