This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new a477d6d28 RATIS-2254. Replace junit 4 Timeout with junit 5. (#1230)
a477d6d28 is described below
commit a477d6d289982a20d479f58fdc6a9daf835b1162
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Wed Feb 26 12:39:03 2025 -0800
RATIS-2254. Replace junit 4 Timeout with junit 5. (#1230)
---
.../src/test/java/org/apache/ratis/BaseTest.java | 49 ++++++++--------------
.../arithmetic/expression/TestExpression.java | 9 ++--
.../test/java/org/apache/ratis/RaftTestUtil.java | 10 +++++
.../ratis/server/impl/GroupManagementBaseTest.java | 2 +
.../ratis/server/impl/LeaderElectionTests.java | 1 +
.../org/apache/ratis/RaftLogTruncateTests.java | 7 +---
.../datastream/DataStreamAsyncClusterTests.java | 7 +---
.../org/apache/ratis/grpc/TestRaftWithGrpc.java | 2 +
.../ratis/retry/TestMultipleLinearRandomRetry.java | 7 +---
.../org/apache/ratis/retry/TestRetryPolicy.java | 7 +---
10 files changed, 44 insertions(+), 57 deletions(-)
diff --git a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
index af54cafc7..bd6202661 100644
--- a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
+++ b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
@@ -18,7 +18,6 @@
package org.apache.ratis;
import org.apache.ratis.conf.ConfUtils;
-import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.util.ExitUtils;
import org.apache.ratis.util.FileUtils;
import org.apache.ratis.util.JavaUtils;
@@ -28,6 +27,7 @@ import org.apache.ratis.util.StringUtils;
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.function.CheckedRunnable;
import org.junit.After;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
@@ -42,8 +42,7 @@ import org.slf4j.event.Level;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
@@ -75,33 +74,30 @@ public abstract class BaseTest {
}
}
- public List<RaftPeer> getPeersWithPriority(List<RaftPeer> peers, RaftPeer
suggestedLeader) {
- List<RaftPeer> peersWithPriority = new ArrayList<>();
- for (int i = 0; i < peers.size(); i++) {
- RaftPeer peer = peers.get(i);
- final int priority = peer.equals(suggestedLeader)? 2: 1;
- peersWithPriority.add(
- RaftPeer.newBuilder(peer).setPriority(priority).build());
- }
- return peersWithPriority;
- }
-
-
- /*
- * Junit 4 reference will be removed and the code will be refactored once
- * all the unit tests are migrated to Junit 5.
- */
+ // TODO: Junit 4 reference should be removed once all the unit tests are
migrated to Junit 5.
private String testCaseName;
@BeforeEach
public void setup(TestInfo testInfo) {
- testCaseName = testInfo.getTestMethod()
- .orElseThrow(() -> new RuntimeException("Exception while getting test
name."))
- .getName();
+ checkAssumptions();
+ final Method method = testInfo.getTestMethod().orElse(null);
+ testCaseName = testInfo.getTestClass().orElse(getClass()).getSimpleName()
+ + "." + (method == null? null : method.getName());
+ }
+
+ // @Before annotation is retained to support junit 4 tests.
+ @Before
+ public void checkAssumptions() {
final int leaks =
ReferenceCountedLeakDetector.getLeakDetector().getLeakCount();
Assumptions.assumeFalse(0 < leaks, () -> "numLeaks " + leaks + " > 0");
+
+ final Throwable first = firstException.get();
+ Assumptions.assumeTrue(first == null, () -> "Already failed with " +
first);
+
+ final Throwable exited = ExitUtils.getFirstExitException();
+ Assumptions.assumeTrue(exited == null, () -> "Already exited with " +
exited);
}
// @After annotation is retained to support junit 4 tests.
@@ -116,19 +112,10 @@ public abstract class BaseTest {
ExitUtils.assertNotTerminated();
}
- // Retained to support junit 4 tests.
- @Rule
- public final org.junit.rules.Timeout globalTimeout = new
org.junit.rules.Timeout(
- getGlobalTimeoutSeconds(), TimeUnit.SECONDS );
-
// Retained to support junit 4 tests.
@Rule
public final TestName testName = new TestName();
- public int getGlobalTimeoutSeconds() {
- return 100;
- }
-
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"),
diff --git
a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/expression/TestExpression.java
b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/expression/TestExpression.java
index 4cc81c6ed..b512c617f 100644
---
a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/expression/TestExpression.java
+++
b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/expression/TestExpression.java
@@ -1,4 +1,4 @@
-/**
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -21,16 +21,13 @@ package org.apache.ratis.examples.arithmetic.expression;
import org.apache.ratis.BaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
+@Timeout(value = 1)
public class TestExpression extends BaseTest {
- @Override
- public int getGlobalTimeoutSeconds() {
- return 1;
- }
-
@Test
public void testArithmeticUtils() throws Exception {
final Random ran = ThreadLocalRandom.current();
diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java
b/ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java
index 733993701..a1c468ca5 100644
--- a/ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java
+++ b/ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java
@@ -25,6 +25,7 @@ import org.apache.ratis.protocol.ClientId;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftGroupId;
+import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.RaftServer;
import org.apache.ratis.server.RaftServerConfigKeys;
@@ -467,6 +468,15 @@ public interface RaftTestUtil {
}
}
+ static List<RaftPeer> getPeersWithPriority(List<RaftPeer> peers, RaftPeer
suggestedLeader) {
+ List<RaftPeer> peersWithPriority = new ArrayList<>();
+ for (RaftPeer peer : peers) {
+ final int priority = peer.equals(suggestedLeader) ? 2 : 1;
+
peersWithPriority.add(RaftPeer.newBuilder(peer).setPriority(priority).build());
+ }
+ return peersWithPriority;
+ }
+
static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader)
throws Exception {
return changeLeader(cluster, oldLeader, AssumptionViolatedException::new);
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
b/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
index 30f9f7579..a06336f91 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/impl/GroupManagementBaseTest.java
@@ -54,6 +54,8 @@ import java.util.concurrent.TimeUnit;
import java.util.Random;
import java.util.stream.Collectors;
+import static org.apache.ratis.RaftTestUtil.getPeersWithPriority;
+
public abstract class GroupManagementBaseTest extends BaseTest {
static final Logger LOG =
LoggerFactory.getLogger(GroupManagementBaseTest.class);
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java
b/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java
index c51ffda64..f35626894 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java
@@ -63,6 +63,7 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
+import static org.apache.ratis.RaftTestUtil.getPeersWithPriority;
import static org.apache.ratis.RaftTestUtil.waitForLeader;
import static
org.apache.ratis.server.metrics.LeaderElectionMetrics.LAST_LEADER_ELECTION_ELAPSED_TIME;
import static
org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_COUNT_METRIC;
diff --git
a/ratis-test/src/test/java/org/apache/ratis/RaftLogTruncateTests.java
b/ratis-test/src/test/java/org/apache/ratis/RaftLogTruncateTests.java
index 327163de0..9ea78e47c 100644
--- a/ratis-test/src/test/java/org/apache/ratis/RaftLogTruncateTests.java
+++ b/ratis-test/src/test/java/org/apache/ratis/RaftLogTruncateTests.java
@@ -42,6 +42,7 @@ import org.apache.ratis.util.Slf4jUtils;
import org.apache.ratis.util.TimeDuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
import org.slf4j.event.Level;
import java.util.ArrayList;
@@ -54,6 +55,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static org.apache.ratis.RaftTestUtil.waitForLeader;
+@Timeout(value = 200)
public abstract class RaftLogTruncateTests<CLUSTER extends MiniRaftCluster>
extends BaseTest
implements MiniRaftCluster.Factory.Get<CLUSTER> {
public static final int NUM_SERVERS = 5;
@@ -84,11 +86,6 @@ public abstract class RaftLogTruncateTests<CLUSTER extends
MiniRaftCluster> exte
RaftServerConfigKeys.Rpc.setFirstElectionTimeoutMax(p,
TimeDuration.ONE_SECOND.multiply(2));
}
- @Override
- public int getGlobalTimeoutSeconds() {
- return 200;
- }
-
@Test
public void testLogTruncate() throws Exception {
runWithNewCluster(NUM_SERVERS, this::runTestLogTruncate);
diff --git
a/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamAsyncClusterTests.java
b/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamAsyncClusterTests.java
index a34350206..eb25a369e 100644
---
a/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamAsyncClusterTests.java
+++
b/ratis-test/src/test/java/org/apache/ratis/datastream/DataStreamAsyncClusterTests.java
@@ -36,6 +36,7 @@ import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.function.CheckedBiFunction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
import org.slf4j.event.Level;
import java.io.IOException;
@@ -47,15 +48,11 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
+@Timeout(value = 300)
public abstract class DataStreamAsyncClusterTests<CLUSTER extends
MiniRaftCluster>
extends DataStreamClusterTests<CLUSTER> {
final Executor executor = Executors.newFixedThreadPool(16);
- @Override
- public int getGlobalTimeoutSeconds() {
- return 300;
- }
-
@Test
public void testSingleStreamsMultipleServers() throws Exception {
Slf4jUtils.setLogLevel(NettyClientStreamRpc.LOG, Level.TRACE);
diff --git
a/ratis-test/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java
b/ratis-test/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java
index c13544a78..42211cefc 100644
--- a/ratis-test/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java
+++ b/ratis-test/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java
@@ -33,6 +33,7 @@ import org.apache.ratis.util.JavaUtils;
import org.apache.ratis.util.TimeDuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -57,6 +58,7 @@ public class TestRaftWithGrpc
}
@Flaky("RATIS-2253")
+ @Timeout(300)
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testWithLoad(boolean separateHeartbeat) throws Exception {
diff --git
a/ratis-test/src/test/java/org/apache/ratis/retry/TestMultipleLinearRandomRetry.java
b/ratis-test/src/test/java/org/apache/ratis/retry/TestMultipleLinearRandomRetry.java
index eb4b7967c..77bcb70f7 100644
---
a/ratis-test/src/test/java/org/apache/ratis/retry/TestMultipleLinearRandomRetry.java
+++
b/ratis-test/src/test/java/org/apache/ratis/retry/TestMultipleLinearRandomRetry.java
@@ -21,13 +21,10 @@ import org.apache.ratis.BaseTest;
import org.apache.ratis.util.TimeDuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+@Timeout(value = 1)
public class TestMultipleLinearRandomRetry extends BaseTest {
- @Override
- public int getGlobalTimeoutSeconds() {
- return 1;
- }
-
@Test
public void testParseCommaSeparated() {
assertIllegalInput("");
diff --git
a/ratis-test/src/test/java/org/apache/ratis/retry/TestRetryPolicy.java
b/ratis-test/src/test/java/org/apache/ratis/retry/TestRetryPolicy.java
index cee8ee338..43b2fedd1 100644
--- a/ratis-test/src/test/java/org/apache/ratis/retry/TestRetryPolicy.java
+++ b/ratis-test/src/test/java/org/apache/ratis/retry/TestRetryPolicy.java
@@ -36,6 +36,7 @@ import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.Timestamp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
import java.io.IOException;
import java.util.ArrayList;
@@ -45,12 +46,8 @@ import java.util.Map;
import java.util.concurrent.TimeUnit;
/** Test {@link RetryPolicy}. */
+@Timeout(value = 1)
public class TestRetryPolicy extends BaseTest {
- @Override
- public int getGlobalTimeoutSeconds() {
- return 1;
- }
-
@Test
public void testRetryMultipleTimesWithFixedSleep() {
final int n = 4;