com.dasasian.chok.testutil.TestUtil Maven / Gradle / Ivy
/**
* Copyright (C) 2014 Dasasian ([email protected])
*
* 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.dasasian.chok.testutil;
import com.dasasian.chok.client.Client;
import com.dasasian.chok.master.Master;
import com.dasasian.chok.node.Node;
import com.dasasian.chok.protocol.InteractionProtocol;
import com.dasasian.chok.protocol.metadata.IndexMetaData;
import com.dasasian.chok.protocol.metadata.IndexMetaData.Shard;
import com.dasasian.chok.util.ZkConfiguration;
import com.dasasian.chok.util.ZkConfiguration.PathDef;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.apache.log4j.Logger;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubber;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class TestUtil {
private static final Logger LOG = Logger.getLogger(TestUtil.class);
/**
* This waits until the provided {@link Callable} returns an object that is
* equals to the given expected value or the timeout has been reached. In both
* cases this method will return the return value of the latest
* {@link Callable} execution.
*
* @param expectedValue The expected value of the callable.
* @param callable The callable.
* @param The return type of the callable.
* @param timeUnit The timeout timeunit.
* @param timeout The timeout.
* @return the return value of the latest {@link Callable} execution.
* @throws Exception
* @throws InterruptedException when interrupted
*/
public static T waitUntil(T expectedValue, Callable callable, TimeUnit timeUnit, long timeout) throws Exception {
long startTime = System.currentTimeMillis();
do {
T actual = callable.call();
if (expectedValue.equals(actual)) {
return actual;
}
if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
return actual;
}
Thread.sleep(50);
} while (true);
}
/**
* This waits until a mockito verification passed (which is provided in the
* runnable). This waits until the virification passed or the timeout has been
* reached. If the timeout has been reached this method will rethrow the
* {@link MockitoAssertionError} that comes from the mockito verification
* code.
*
* @param runnable The runnable containing the mockito verification.
* @param timeUnit The timeout timeunit.
* @param timeout The timeout.
* @throws InterruptedException when interrupted
*/
public static void waitUntilVerified(Runnable runnable, TimeUnit timeUnit, int timeout) throws InterruptedException {
LOG.debug("Waiting for " + timeout + " " + timeUnit + " until verified.");
long startTime = System.currentTimeMillis();
do {
MockitoAssertionError exception = null;
try {
runnable.run();
} catch (MockitoAssertionError e) {
exception = e;
}
if (exception == null) {
return;
}
if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
LOG.debug("Timeout reached without satifying expectations.");
throw exception;
}
Thread.sleep(50);
} while (true);
}
public static void waitUntilNoExceptionThrown(Runnable runnable, TimeUnit timeUnit, int timeout) throws InterruptedException {
long startTime = System.currentTimeMillis();
do {
RuntimeException exception = null;
try {
runnable.run();
} catch (RuntimeException e) {
exception = e;
}
if (exception == null) {
return;
}
if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
throw exception;
}
Thread.sleep(50);
} while (true);
}
/**
* Creates a Mockito answer object that can be used for asynchronously
* stubbing. For example:
*
*
* final CountDownLatch countDownLatch = new CountDownLatch(1);
* Mockito.doAnswer(TestUtil.createCountDownAnswer(countDownLatch)).when(listener).announceNode(nodeName);
* mock.someMethod(someValue);
* countDownLatch.await(10, TimeUnit.SECONDS);
* Assert.assertEquals("expecting invocation within 10 seconds", 0, countDownLatch.getCount());
*
* @param countDownLatch the countdown latch
* @return the stubber
*/
public static Stubber createCountDownAnswer(final CountDownLatch countDownLatch) {
return Mockito.doAnswer(new Answer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy