Skip to content

Commit 0091c1c

Browse files
authored
RATIS-2124. Remove the use of org.junit.Rule. (#1232)
1 parent 59f1992 commit 0091c1c

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

ratis-common/src/test/java/org/apache/ratis/BaseTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
import org.apache.ratis.util.function.CheckedRunnable;
2929
import org.junit.After;
3030
import org.junit.Before;
31-
import org.junit.Rule;
3231
import org.junit.jupiter.api.AfterEach;
3332
import org.junit.jupiter.api.Assertions;
3433
import org.junit.jupiter.api.Assumptions;
3534
import org.junit.jupiter.api.BeforeEach;
3635
import org.junit.jupiter.api.TestInfo;
3736
import org.junit.jupiter.api.Timeout;
38-
import org.junit.rules.TestName;
3937
import org.slf4j.Logger;
4038
import org.slf4j.LoggerFactory;
4139
import org.slf4j.event.Level;
@@ -89,6 +87,7 @@ public void setup(TestInfo testInfo) {
8987

9088
// @Before annotation is retained to support junit 4 tests.
9189
@Before
90+
@BeforeEach
9291
public void checkAssumptions() {
9392
final int leaks = ReferenceCountedLeakDetector.getLeakDetector().getLeakCount();
9493
Assumptions.assumeFalse(0 < leaks, () -> "numLeaks " + leaks + " > 0");
@@ -112,10 +111,6 @@ public void assertNoFailures() {
112111
ExitUtils.assertNotTerminated();
113112
}
114113

115-
// Retained to support junit 4 tests.
116-
@Rule
117-
public final TestName testName = new TestName();
118-
119114
private static final Supplier<File> ROOT_TEST_DIR = JavaUtils.memoize(
120115
() -> JavaUtils.callAsUnchecked(() -> {
121116
final File dir = new File(System.getProperty("test.build.data", "target/test/data"),
@@ -139,8 +134,7 @@ public File getClassTestDir() {
139134

140135
public File getTestDir() {
141136
// This will work for both junit 4 and 5.
142-
final String name = testCaseName != null ? testCaseName : testName.getMethodName();
143-
return new File(getClassTestDir(), name);
137+
return new File(getClassTestDir(), testCaseName);
144138
}
145139

146140
@SafeVarargs

ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
import org.apache.ratis.util.JavaUtils;
5151
import org.apache.ratis.util.LifeCycle;
5252
import org.apache.ratis.util.Slf4jUtils;
53-
import org.junit.After;
54-
import org.junit.Assert;
55-
import org.junit.Before;
56-
import org.junit.Test;
53+
import org.junit.jupiter.api.Assertions;
54+
import org.junit.jupiter.api.AfterEach;
55+
import org.junit.jupiter.api.BeforeEach;
56+
import org.junit.jupiter.api.Test;
5757
import org.slf4j.Logger;
5858
import org.slf4j.LoggerFactory;
5959

@@ -97,24 +97,24 @@ public static void assertLogContent(RaftServer.Division server, boolean isLeader
9797
final RaftLog log = server.getRaftLog();
9898
final long lastIndex = log.getLastEntryTermIndex().getIndex();
9999
final LogEntryProto e = getLogUnsafe(log, lastIndex);
100-
Assert.assertTrue(e.hasMetadataEntry());
100+
Assertions.assertTrue(e.hasMetadataEntry());
101101

102102
JavaUtils.attemptRepeatedly(() -> {
103-
Assert.assertEquals(log.getLastCommittedIndex() - 1, e.getMetadataEntry().getCommitIndex());
103+
Assertions.assertEquals(log.getLastCommittedIndex() - 1, e.getMetadataEntry().getCommitIndex());
104104
return null;
105105
}, 50, BaseTest.HUNDRED_MILLIS, "CheckMetadataEntry", LOG);
106106

107107
SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(server);
108108
if (isLeader) {
109-
Assert.assertTrue("Not notified as a leader", simpleStateMachine.isNotifiedAsLeader());
109+
Assertions.assertTrue(simpleStateMachine.isNotifiedAsLeader(), "Not notified as a leader");
110110
}
111111
final LogEntryProto[] entries = simpleStateMachine.getContent();
112112
long message = 0;
113113
for (int i = 0; i < entries.length; i++) {
114114
LOG.info("{}) {} {}", i, message, entries[i].toString().replace("\n", ", "));
115115
if (entries[i].hasStateMachineLogEntry()) {
116116
final SimpleMessage m = new SimpleMessage("m" + message++);
117-
Assert.assertArrayEquals(m.getContent().toByteArray(),
117+
Assertions.assertArrayEquals(m.getContent().toByteArray(),
118118
entries[i].getStateMachineLogEntry().getLogData().toByteArray());
119119
}
120120
}
@@ -124,7 +124,7 @@ public static void assertLogContent(RaftServer.Division server, boolean isLeader
124124

125125
public abstract MiniRaftCluster.Factory<?> getFactory();
126126

127-
@Before
127+
@BeforeEach
128128
public void setup() throws IOException {
129129
final RaftProperties prop = new RaftProperties();
130130
prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
@@ -136,7 +136,7 @@ public void setup() throws IOException {
136136
cluster.start();
137137
}
138138

139-
@After
139+
@AfterEach
140140
public void tearDown() {
141141
if (cluster != null) {
142142
cluster.shutdown();
@@ -156,7 +156,7 @@ public void testRestartPeer() throws Exception {
156156
try(final RaftClient client = cluster.createClient(leaderId)) {
157157
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
158158
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
159-
Assert.assertTrue(reply.isSuccess());
159+
Assertions.assertTrue(reply.isSuccess());
160160
}
161161
}
162162

@@ -165,7 +165,7 @@ public void testRestartPeer() throws Exception {
165165
// wait for the snapshot to be done
166166
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
167167
JavaUtils.attemptRepeatedly(() -> {
168-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
168+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
169169
return null;
170170
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
171171

@@ -203,7 +203,7 @@ public void testBasicInstallSnapshot() throws Exception {
203203
try(final RaftClient client = cluster.createClient(leaderId)) {
204204
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
205205
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
206-
Assert.assertTrue(reply.isSuccess());
206+
Assertions.assertTrue(reply.isSuccess());
207207
}
208208
}
209209

@@ -212,7 +212,7 @@ public void testBasicInstallSnapshot() throws Exception {
212212
LOG.info("nextIndex = {}", nextIndex);
213213
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
214214
JavaUtils.attemptRepeatedly(() -> {
215-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
215+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
216216
return null;
217217
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
218218
verifyTakeSnapshotMetric(cluster.getLeader());
@@ -234,7 +234,7 @@ public void testBasicInstallSnapshot() throws Exception {
234234

235235
// generate some more traffic
236236
try(final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
237-
Assert.assertTrue(client.io().send(new SimpleMessage("m" + i)).isSuccess());
237+
Assertions.assertTrue(client.io().send(new SimpleMessage("m" + i)).isSuccess());
238238
}
239239

240240
// add two more peers
@@ -248,7 +248,7 @@ public void testBasicInstallSnapshot() throws Exception {
248248
for (String newPeer : newPeers) {
249249
final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
250250
SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
251-
Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
251+
Assertions.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
252252
}
253253

254254
// Verify installSnapshot counter on leader before restart.
@@ -263,7 +263,7 @@ public void testBasicInstallSnapshot() throws Exception {
263263
assertLeaderContent(cluster);
264264

265265
// verify that snapshot was taken when stopping the server
266-
Assert.assertTrue(count < timer.getCount());
266+
Assertions.assertTrue(count < timer.getCount());
267267
} finally {
268268
cluster.shutdown();
269269
}
@@ -284,7 +284,7 @@ public void testInstallSnapshotDuringBootstrap() throws Exception {
284284
try(final RaftClient client = cluster.createClient(leaderId)) {
285285
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
286286
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
287-
Assert.assertTrue(reply.isSuccess());
287+
Assertions.assertTrue(reply.isSuccess());
288288
}
289289
}
290290

@@ -293,7 +293,7 @@ public void testInstallSnapshotDuringBootstrap() throws Exception {
293293
LOG.info("nextIndex = {}", nextIndex);
294294
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
295295
JavaUtils.attemptRepeatedly(() -> {
296-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
296+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
297297
return null;
298298
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
299299
verifyTakeSnapshotMetric(cluster.getLeader());
@@ -311,7 +311,7 @@ public void testInstallSnapshotDuringBootstrap() throws Exception {
311311
for (String newPeer : newPeers) {
312312
final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
313313
SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
314-
Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
314+
Assertions.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
315315
}
316316

317317
// Verify installSnapshot counter on leader
@@ -325,23 +325,23 @@ public void testInstallSnapshotDuringBootstrap() throws Exception {
325325
protected void verifyInstallSnapshotMetric(RaftServer.Division leader) {
326326
final LongCounter installSnapshotCounter = ((RaftServerMetricsImpl)leader.getRaftServerMetrics())
327327
.getNumInstallSnapshot();
328-
Assert.assertNotNull(installSnapshotCounter);
329-
Assert.assertTrue(installSnapshotCounter.getCount() >= 1);
328+
Assertions.assertNotNull(installSnapshotCounter);
329+
Assertions.assertTrue(installSnapshotCounter.getCount() >= 1);
330330
}
331331

332332
private static void verifyTakeSnapshotMetric(RaftServer.Division leader) {
333333
Timer timer = getTakeSnapshotTimer(leader);
334-
Assert.assertTrue(timer.getCount() > 0);
334+
Assertions.assertTrue(timer.getCount() > 0);
335335
}
336336

337337
private static Timer getTakeSnapshotTimer(RaftServer.Division leader) {
338338
MetricRegistryInfo info = new MetricRegistryInfo(leader.getMemberId().toString(),
339339
RATIS_APPLICATION_NAME_METRICS,
340340
RATIS_STATEMACHINE_METRICS, RATIS_STATEMACHINE_METRICS_DESC);
341341
Optional<RatisMetricRegistry> opt = MetricRegistries.global().get(info);
342-
Assert.assertTrue(opt.isPresent());
342+
Assertions.assertTrue(opt.isPresent());
343343
RatisMetricRegistry metricRegistry = opt.get();
344-
Assert.assertNotNull(metricRegistry);
344+
Assertions.assertNotNull(metricRegistry);
345345
return ((DefaultTimekeeperImpl)metricRegistry.timer(STATEMACHINE_TAKE_SNAPSHOT_TIMER)).getTimer();
346346
}
347347
}

0 commit comments

Comments
 (0)