net.openhft.chronicle.testframework.internal.VanillaFlakyTestRunnerBuilder Maven / Gradle / Ivy
package net.openhft.chronicle.testframework.internal;
import net.openhft.chronicle.testframework.FlakyTestRunner;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
import static java.util.Objects.requireNonNull;
public final class VanillaFlakyTestRunnerBuilder implements FlakyTestRunner.Builder {
final FlakyTestRunner.RunnableThrows action;
boolean flakyOnThisArchitecture = true;
int maxIterations = 1;
long delayMs = 500;
boolean interIterationGc = true;
Consumer super String> infoLogger = System.out::println;
Consumer super String> errorLogger = System.err::println;
private boolean built = false;
public VanillaFlakyTestRunnerBuilder(@NotNull final FlakyTestRunner.RunnableThrows action) {
this.action = requireNonNull(action);
}
@Override
public FlakyTestRunner.Builder withFlakyOnThisArchitecture(boolean flakyOnThisArchitecture) {
this.flakyOnThisArchitecture = flakyOnThisArchitecture;
return this;
}
@Override
public FlakyTestRunner.Builder withMaxIterations(int maxIterations) {
if (maxIterations < 0)
throw new IllegalArgumentException("maxIterations is negative: " + maxIterations);
this.maxIterations = maxIterations;
return this;
}
@Override
public FlakyTestRunner.Builder withIterationDelay(long delayMs) {
if (delayMs < 0)
throw new IllegalArgumentException("delayMs is negative: " + delayMs);
this.delayMs = delayMs;
return this;
}
@Override
public FlakyTestRunner.Builder withInterIterationGc(boolean interIterationGc) {
this.interIterationGc = interIterationGc;
return this;
}
@Override
public FlakyTestRunner.Builder withInfoLogger(Consumer super String> infoLogger) {
this.infoLogger = requireNonNull(infoLogger);
return this;
}
@Override
public FlakyTestRunner.Builder withErrorLogger(Consumer super String> errorLogger) {
this.errorLogger = requireNonNull(errorLogger);
return this;
}
@Override
public FlakyTestRunner.RunnableThrows build() {
if (built)
throw new IllegalStateException("Builder already used");
built = true;
return new VanillaFlakyTestRunner<>(this);
}
}