我有以下用于重试某个操作的Java代码
package helpers;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public final class Retry {
public static <V> V execute(Callable<V> action, Duration retryInterval, int retryCount)
throws AggregateException {
List<Throwable> exceptions = new ArrayList<>();
for (int retry = 0; retry < retryCount; ++retry) {
try {
if (retry > 0)
Thread.sleep(retryInterval.toMillis());
return action.call();
} catch (Throwable t) {
exceptions.add(t);
}
}
throw new AggregateException(exceptions);
}
public static <V> Future executeAsync(Callable<V> action, Duration retryInterval, int retryCount)
throws AggregateException {
FutureTask<V> task = new FutureTask<>(action);
ExecutorService executor = Executors.newSingleThreadExecutor();
return executor.submit(task);
}
}
我正在尝试通过测试同步代码的功能
package helpers;
import org.junit.Before;
import org.junit.Test;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.concurrent.Callable;
import static org.junit.Assert.assertEquals;
public class RetryTest {
private Integer counter = 0;
private Callable<Integer> calculator;
@Before
public void init() {
calculator = () -> {
for (int i = 0; i < 3; ++i) {
counter++;
System.out.println(MessageFormat.format(
"Counter = {0}", Integer.toString(counter)));
}
if (counter < 6)
throw new Exception();
return counter;
};
}
@Test
public void execute() throws AggregateException {
Integer result = Retry.execute(calculator, Duration.ofSeconds(1), 3);
assertEquals(9, java.util.Optional.ofNullable(result));
}
}
但是calculator
在Retry.execute(calculator, Duration.ofSeconds(1), 3);
中是无效的,为什么我在这里做错了什么?
我正在使用Junit 4.12。
我也尝试了完整的语法
@Before
public void init() {
calculator = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
for (int i = 0; i < 3; ++i) {
counter++;
System.out.println(MessageFormat.format(
"Counter = {0}", Integer.toString(counter)));
}
if (counter < 6)
throw new Exception();
return counter;
}
};
}
仍然无效。
好吧,我测试使用JUnit 3
和@Before
永远不会被调用。然后我测试了JUnit 4并调用了@Before
,试着看看你的JUnit版本。
你能查看这个测试代码吗?
public class RetryTest {
@Test
public void execute() throws AggregateException {
Integer result = Retry.execute(getCallableCalculater(), Duration.ofSeconds(1), 3);
assertEquals(9, java.util.Optional.ofNullable(result));
}
private Callable<Integer> getCallableCalculater() {
final Integer[] counter = {0};
return () -> {
for (int i = 0; i < 3; ++i) {
counter[0]++;
System.out.println(MessageFormat.format(
"Counter = {0}", Integer.toString(counter[0])));
}
if (counter[0] < 6)
throw new Exception();
return counter[0];
};
}
}