I have the following Java code used to retry a certain action
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);
}
}
I am am attempting to test the functionality of the synchronous code via
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));
}
}
But calculator
is null in Retry.execute(calculator, Duration.ofSeconds(1), 3);
, why and what am I doing wrong here?
I am using Junit 4.12.
I have also tried the full syntax
@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;
}
};
}
still null.
Well I tested using JUnit 3
and @Before
is never called. Then I tested with JUnit 4 and @Before
is being called , try to see your JUnit Versions.
Can you check with this test code ?
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];
};
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.