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 0e247578b RATIS-1972. Add junit 5 dependencies in ratis-server. (#1003)
0e247578b is described below
commit 0e247578bf46cd0ffdc9863da3ff77ba21753a37
Author: Nandakumar Vadivelu <[email protected]>
AuthorDate: Tue Jan 23 07:27:50 2024 +0530
RATIS-1972. Add junit 5 dependencies in ratis-server. (#1003)
---
.../src/test/java/org/apache/ratis/BaseTest.java | 38 ++++++++++++++++++----
ratis-examples/pom.xml | 5 +++
ratis-server/pom.xml | 25 ++++++++++++++
.../ratis/server/impl/TestLogAppenderMetrics.java | 20 ++++++------
.../ratis/server/impl/TestRetryCacheMetrics.java | 12 +++----
.../server/metrics/TestLeaderElectionMetrics.java | 14 ++++----
ratis-test/pom.xml | 5 +++
7 files changed, 90 insertions(+), 29 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 821369461..fb34d64f1 100644
--- a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
+++ b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
@@ -27,10 +27,13 @@ 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.Assert;
import org.junit.Rule;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.Timeout;
import org.junit.rules.TestName;
-import org.junit.rules.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
@@ -47,6 +50,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
+@Timeout(value = 100)
public abstract class BaseTest {
public final Logger LOG = LoggerFactory.getLogger(getClass());
@@ -80,7 +84,24 @@ public abstract class BaseTest {
return peersWithPriority;
}
+
+ /*
+ * Junit 4 reference will be removed and the code will be refactored 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();
+ }
+
+ // @After annotation is retained to support junit 4 tests.
@After
+ @AfterEach
public void assertNoFailures() {
final Throwable e = firstException.get();
if (e != null) {
@@ -90,9 +111,12 @@ public abstract class BaseTest {
ExitUtils.assertNotTerminated();
}
+ // Retained to support junit 4 tests.
@Rule
- public final Timeout globalTimeout = new Timeout(getGlobalTimeoutSeconds(),
TimeUnit.SECONDS );
+ 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();
@@ -122,7 +146,9 @@ public abstract class BaseTest {
}
public File getTestDir() {
- return new File(getClassTestDir(), testName.getMethodName());
+ // This will work for both junit 4 and 5.
+ final String name = testCaseName != null ? testCaseName :
testName.getMethodName();
+ return new File(getClassTestDir(), name);
}
@SafeVarargs
@@ -135,13 +161,13 @@ public abstract class BaseTest {
description, expectedThrowableClass.getSimpleName(),
StringUtils.array2String(expectedCauseClasses,
Class::getSimpleName));
}
- Assert.assertEquals(expectedThrowableClass, t.getClass());
+ Assertions.assertEquals(expectedThrowableClass, t.getClass());
for (Class<? extends Throwable> expectedCause : expectedCauseClasses) {
final Throwable previous = t;
t = Objects.requireNonNull(previous.getCause(),
() -> "previous.getCause() == null for previous=" + previous);
- Assert.assertEquals(expectedCause, t.getClass());
+ Assertions.assertEquals(expectedCause, t.getClass());
}
}
diff --git a/ratis-examples/pom.xml b/ratis-examples/pom.xml
index 4078f6260..54691d6f1 100644
--- a/ratis-examples/pom.xml
+++ b/ratis-examples/pom.xml
@@ -122,6 +122,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/ratis-server/pom.xml b/ratis-server/pom.xml
index 3756e3e57..38af72d9d 100644
--- a/ratis-server/pom.xml
+++ b/ratis-server/pom.xml
@@ -70,6 +70,31 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.vintage</groupId>
+ <artifactId>junit-vintage-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
index 5c78db42f..4bd075ef6 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
@@ -27,11 +27,11 @@ import org.apache.ratis.protocol.RaftGroupMemberId;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.metrics.LogAppenderMetrics;
import org.apache.ratis.util.Timestamp;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
public class TestLogAppenderMetrics {
@@ -39,7 +39,7 @@ public class TestLogAppenderMetrics {
private RaftPeerId raftPeerId;
private MyFollowerInfo followerInfo;
- @Before
+ @BeforeEach
public void setup() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
raftPeerId = RaftPeerId.valueOf("TestId");
@@ -55,19 +55,19 @@ public class TestLogAppenderMetrics {
public void testLogAppenderGauges() {
Gauge nextIndex = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_NEXT_INDEX,
raftPeerId.toString()))).values().iterator().next();
- Assert.assertEquals(100L, nextIndex.getValue());
+ Assertions.assertEquals(100L, nextIndex.getValue());
Gauge matchIndex = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_MATCH_INDEX,
raftPeerId.toString()))).values().iterator().next();
- Assert.assertEquals(0L, matchIndex.getValue());
+ Assertions.assertEquals(0L, matchIndex.getValue());
Gauge rpcTime = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_RPC_RESP_TIME,
raftPeerId.toString()))).values().iterator().next();
- Assert.assertTrue(((Long) rpcTime.getValue()) > 0);
+ Assertions.assertTrue(((Long) rpcTime.getValue()) > 0);
followerInfo.updateNextIndex(200L);
followerInfo.updateMatchIndex(100L);
followerInfo.updateLastRpcResponseTime();
- Assert.assertEquals(200L, nextIndex.getValue());
- Assert.assertEquals(100L, matchIndex.getValue());
- Assert.assertNotNull(rpcTime.getValue());
+ Assertions.assertEquals(200L, nextIndex.getValue());
+ Assertions.assertEquals(100L, matchIndex.getValue());
+ Assertions.assertNotNull(rpcTime.getValue());
}
private static class MyFollowerInfo {
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
index b25a50bf9..0d779a2dc 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
@@ -19,7 +19,7 @@
package org.apache.ratis.server.impl;
import static org.apache.ratis.server.metrics.RaftServerMetricsImpl.*;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.ratis.metrics.impl.RatisMetricRegistryImpl;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
@@ -30,9 +30,9 @@ import org.apache.ratis.protocol.RaftGroupMemberId;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.server.metrics.RaftServerMetricsImpl;
-import org.junit.After;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
import java.util.Map;
@@ -43,7 +43,7 @@ public class TestRetryCacheMetrics {
private static RatisMetricRegistryImpl ratisMetricRegistry;
private static RetryCacheImpl retryCache;
- @BeforeClass
+ @BeforeAll
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
@@ -56,7 +56,7 @@ public class TestRetryCacheMetrics {
ratisMetricRegistry = (RatisMetricRegistryImpl)
raftServerMetrics.getRegistry();
}
- @After
+ @AfterEach
public void tearDown() {
retryCache.close();
checkEntryCount(0);
diff --git
a/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
b/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
index a39612c57..bba5d9231 100644
---
a/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
+++
b/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
@@ -20,8 +20,8 @@ package org.apache.ratis.server.metrics;
import static
org.apache.ratis.server.metrics.LeaderElectionMetrics.LAST_LEADER_ELECTION_ELAPSED_TIME;
import static
org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_TIMEOUT_COUNT_METRIC;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.ratis.metrics.impl.RatisMetricRegistryImpl;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
@@ -29,8 +29,8 @@ import org.apache.ratis.BaseTest;
import org.apache.ratis.protocol.RaftGroupId;
import org.apache.ratis.protocol.RaftGroupMemberId;
import org.apache.ratis.protocol.RaftPeerId;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
import java.util.SortedMap;
@@ -42,7 +42,7 @@ public class TestLeaderElectionMetrics extends BaseTest {
private static LeaderElectionMetrics leaderElectionMetrics;
private static RatisMetricRegistryImpl ratisMetricRegistry;
- @BeforeClass
+ @BeforeAll
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
@@ -58,14 +58,14 @@ public class TestLeaderElectionMetrics extends BaseTest {
(s, metric) -> s.contains(LAST_LEADER_ELECTION_ELAPSED_TIME));
LOG.info("{} gauges: {}", LAST_LEADER_ELECTION_ELAPSED_TIME, gauges);
final Long leaderElectionLatency =
(Long)gauges.values().iterator().next().getValue();
- assertTrue("leaderElectionLatency = " + leaderElectionLatency,
leaderElectionLatency >= 0L);
+ assertTrue(leaderElectionLatency >= 0L, "leaderElectionLatency = " +
leaderElectionLatency);
}
@Test
public void testOnLeaderElectionTimeout() throws Exception {
long numLeaderElectionTimeouts = ratisMetricRegistry.counter(
LEADER_ELECTION_TIMEOUT_COUNT_METRIC).getCount();
- assertTrue(numLeaderElectionTimeouts == 0);
+ assertEquals(0, numLeaderElectionTimeouts);
leaderElectionMetrics.onLeaderElectionTimeout();
numLeaderElectionTimeouts =
ratisMetricRegistry.counter(LEADER_ELECTION_TIMEOUT_COUNT_METRIC).getCount();
assertEquals(1, numLeaderElectionTimeouts);
diff --git a/ratis-test/pom.xml b/ratis-test/pom.xml
index 40b6b2ec2..2021e06ad 100644
--- a/ratis-test/pom.xml
+++ b/ratis-test/pom.xml
@@ -136,6 +136,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>