Skip to content

Commit

Permalink
RATIS-2081. Enable checkstyle for test source directory
Browse files Browse the repository at this point in the history
  • Loading branch information
symious committed May 8, 2024
1 parent ac05d64 commit 55f51cb
Show file tree
Hide file tree
Showing 88 changed files with 535 additions and 464 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
<version>${maven-checkstyle-plugin.version}</version>
<configuration>
<configLocation>dev-support/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<failOnViolation>true</failOnViolation>
<linkXRef>false</linkXRef>
</configuration>
Expand Down
5 changes: 3 additions & 2 deletions ratis-common/src/test/java/org/apache/ratis/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

@Timeout(value = 100)
public abstract class BaseTest {
@SuppressWarnings({"VisibilityModifier", "MemberName"})
public final Logger LOG = LoggerFactory.getLogger(getClass());

public static final TimeDuration HUNDRED_MILLIS = TimeDuration.valueOf(100, TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -124,7 +125,7 @@ public int getGlobalTimeoutSeconds() {
return 100;
}

private static final Supplier<File> rootTestDir = JavaUtils.memoize(
private static final Supplier<File> ROOT_TEST_DIR = JavaUtils.memoize(
() -> JavaUtils.callAsUnchecked(() -> {
final File dir = new File(System.getProperty("test.build.data", "target/test/data"),
Integer.toHexString(ThreadLocalRandom.current().nextInt()));
Expand All @@ -138,7 +139,7 @@ public int getGlobalTimeoutSeconds() {


public static File getRootTestDir() {
return rootTestDir.get();
return ROOT_TEST_DIR.get();
}

public File getClassTestDir() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Set;

import org.apache.ratis.thirdparty.com.google.common.collect.Lists;
import org.apache.ratis.util.RefCountingMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.apache.ratis.util.JavaUtils;
import org.apache.ratis.util.TimeDuration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,12 +45,14 @@ public static Collection<Object[]> data() {
return Collections.emptyList();
}

/** For {@link ParameterizedTest} so that a cluster can be shared by multiple {@link Test} */
private static final AtomicReference<MiniRaftCluster> currentCluster = new AtomicReference<>();
/** For {@link org.junit.jupiter.params.ParameterizedTest} so that a cluster can be shared
* by multiple {@link org.junit.jupiter.api.Test}
*/
private static final AtomicReference<MiniRaftCluster> CURRENT_CLUSTER = new AtomicReference<>();

/** Set {@link #currentCluster} to the given cluster and start it if {@link #currentCluster} is changed. */
/** Set {@link #CURRENT_CLUSTER} to the given cluster and start it if {@link #CURRENT_CLUSTER} is changed. */
public static void setAndStart(MiniRaftCluster cluster) throws InterruptedException, IOException {
final MiniRaftCluster previous = currentCluster.getAndSet(cluster);
final MiniRaftCluster previous = CURRENT_CLUSTER.getAndSet(cluster);
if (previous != cluster) {
if (previous != null) {
previous.shutdown();
Expand All @@ -65,7 +65,7 @@ public static void setAndStart(MiniRaftCluster cluster) throws InterruptedExcept

@AfterAll
public static void shutdownCurrentCluster() {
final MiniRaftCluster cluster = currentCluster.getAndSet(null);
final MiniRaftCluster cluster = CURRENT_CLUSTER.getAndSet(null);
if (cluster != null) {
cluster.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQRT;
import static org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQUARE;

@SuppressWarnings({"LocalFinalVariableName", "LocalVariableName"})
public class TestArithmetic extends ParameterizedBaseTest {
{
Slf4jUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.DEBUG);
Expand Down Expand Up @@ -107,7 +108,8 @@ void runGaussLegendre(RaftClient client) throws IOException {
final Variable p0 = new Variable("p" + i_1);
final Variable a1 = defineVariable(client, "a"+i, DIV.apply(ADD.apply(a0, b0), 2));
final Variable b1 = defineVariable(client, "b"+i, SQRT.apply(MULT.apply(a0, b0)));
final Variable t1 = defineVariable(client, "t"+i, SUBTRACT.apply(t0, MULT.apply(p0, SQUARE.apply(SUBTRACT.apply(a0, a1)))));
final Variable t1 = defineVariable(client, "t"+i,
SUBTRACT.apply(t0, MULT.apply(p0, SQUARE.apply(SUBTRACT.apply(a0, a1)))));
final Variable p1 = defineVariable(client, "p"+i, MULT.apply(2, p0));

final Variable pi_i = new Variable("pi_"+i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ public class TestArithmeticLogDump extends BaseTest {

public static final int NUM_SERVERS = 1;

protected static final RaftProperties properties = new RaftProperties();
protected static final RaftProperties PROPERTIES = new RaftProperties();

private final MiniRaftClusterWithSimulatedRpc cluster = MiniRaftClusterWithSimulatedRpc
.FACTORY.newCluster(NUM_SERVERS, getProperties());

public RaftProperties getProperties() {
RaftServerConfigKeys.Rpc
.setSlownessTimeout(properties, TimeDuration.valueOf(1, TimeUnit.SECONDS));
properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
.setSlownessTimeout(PROPERTIES, TimeDuration.valueOf(1, TimeUnit.SECONDS));
PROPERTIES.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
SimpleStateMachine4Testing.class, StateMachine.class);
return properties;
return PROPERTIES;
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@SuppressWarnings("AvoidNestedBlocks")
public class TestExpression extends BaseTest {
@Override
public int getGlobalTimeoutSeconds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@

import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

public class TestSubCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

class FileStoreWriter implements Closeable {
final class FileStoreWriter implements Closeable {
public static final Logger LOG = LoggerFactory.getLogger(FileStoreWriter.class);

final long seed = ThreadLocalRandom.current().nextLong();
final byte[] buffer = new byte[4 << 10];
private final long seed = ThreadLocalRandom.current().nextLong();
private final byte[] buffer = new byte[4 << 10];

final String fileName;
final SizeInBytes fileSize;
final FileStoreClient client;
final Executor asyncExecutor;
final int bufferSize;
private final String fileName;
private final SizeInBytes fileSize;
private final FileStoreClient client;
private final Executor asyncExecutor;
private final int bufferSize;

static Builder newBuilder() {
return new Builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import org.apache.ratis.datastream.MiniRaftClusterWithRpcTypeGrpcAndDataStreamTypeNetty;

public class TestFileStoreStreamingWithGrpcCluster extends FileStoreStreamingBaseTest<MiniRaftClusterWithRpcTypeGrpcAndDataStreamTypeNetty>
public class TestFileStoreStreamingWithGrpcCluster extends
FileStoreStreamingBaseTest<MiniRaftClusterWithRpcTypeGrpcAndDataStreamTypeNetty>
implements MiniRaftClusterWithRpcTypeGrpcAndDataStreamTypeNetty.FactoryGet {

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import static org.apache.ratis.examples.arithmetic.expression.BinaryExpression.Op.ADD;

@SuppressWarnings("LocalFinalVariableName")
public class TestReadAfterWrite
extends BaseTest
implements MiniRaftClusterWithGrpc.FactoryGet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ default Factory<MiniRaftClusterWithGrpc> getFactory() {
}
}

public static final DelayLocalExecutionInjection sendServerRequestInjection =
public static final DelayLocalExecutionInjection SEND_SERVER_REQUEST_INJECTION =
new DelayLocalExecutionInjection(GrpcService.GRPC_SEND_SERVER_REQUEST);

protected MiniRaftClusterWithGrpc(String[] ids, String[] listenerIds, RaftProperties properties,
Expand All @@ -82,7 +82,7 @@ protected Parameters setPropertiesAndInitParameters(RaftPeerId id, RaftGroup gro
@Override
protected void blockQueueAndSetDelay(String leaderId, int delayMs)
throws InterruptedException {
RaftTestUtil.blockQueueAndSetDelay(getServers(), sendServerRequestInjection,
RaftTestUtil.blockQueueAndSetDelay(getServers(), SEND_SERVER_REQUEST_INJECTION,
leaderId, delayMs, getTimeoutMax());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testAddRemoveReporter() {
Consumer<RatisMetricRegistry> reporter = v-> cntr.incrementAndGet();
Consumer<RatisMetricRegistry> stopReporter = v-> cntr.incrementAndGet();
r.addReporterRegistration(reporter, stopReporter);

// check if add and remove of metric do reporting counter increase
MetricRegistryInfo info = new MetricRegistryInfo("t1", "t1", "t1", "t1");
r.create(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ default Factory<MiniRaftClusterWithNetty> getFactory() {
}
}

public static final DelayLocalExecutionInjection sendServerRequest
public static final DelayLocalExecutionInjection SEND_SERVER_REQUEST
= new DelayLocalExecutionInjection(NettyRpcService.SEND_SERVER_REQUEST);

protected MiniRaftClusterWithNetty(String[] ids, String[] listenerIds, RaftProperties properties) {
Expand All @@ -64,7 +64,7 @@ protected Parameters setPropertiesAndInitParameters(RaftPeerId id, RaftGroup gro
@Override
protected void blockQueueAndSetDelay(String leaderId, int delayMs)
throws InterruptedException {
RaftTestUtil.blockQueueAndSetDelay(getServers(), sendServerRequest,
RaftTestUtil.blockQueueAndSetDelay(getServers(), SEND_SERVER_REQUEST,
leaderId, delayMs, getTimeoutMax());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ private void testMultiFileInstallSnapshot(CLUSTER cluster) throws Exception {

private static class StateMachineWithMultiNestedSnapshotFile extends SimpleStateMachine4Testing {

File snapshotRoot;
File file1;
File file2;
private File snapshotRoot;
private File file1;
private File file2;

@Override
public synchronized void initialize(RaftServer server, RaftGroupId groupId, RaftStorage raftStorage) throws IOException {
public synchronized void initialize(RaftServer server, RaftGroupId groupId, RaftStorage raftStorage)
throws IOException {
super.initialize(server, groupId, raftStorage);

// contains two snapshot files
Expand Down Expand Up @@ -215,7 +216,8 @@ private static class StateMachineWithSeparatedSnapshotPath extends SimpleStateMa
private File tmpDir;

@Override
public synchronized void initialize(RaftServer server, RaftGroupId groupId, RaftStorage raftStorage) throws IOException {
public synchronized void initialize(RaftServer server, RaftGroupId groupId, RaftStorage raftStorage)
throws IOException {
super.initialize(server, groupId, raftStorage);
this.root = new File("/tmp/ratis-tests/statemachine/" + getId().toString());
this.snapshotDir = new File(root, "snapshot");
Expand All @@ -238,7 +240,8 @@ public synchronized void pause() {
public long takeSnapshot() {
final TermIndex lastApplied = getLastAppliedTermIndex();
final File snapshotTmpDir = new File(tmpDir, UUID.randomUUID().toString());
final File snapshotRealDir = new File(snapshotDir, String.format("%d_%d", lastApplied.getTerm(), lastApplied.getIndex()));
final File snapshotRealDir = new File(snapshotDir,
String.format("%d_%d", lastApplied.getTerm(), lastApplied.getIndex()));

try {
FileUtils.deleteFully(snapshotRealDir);
Expand Down
Loading

0 comments on commit 55f51cb

Please sign in to comment.