Skip to content

Commit 0557974

Browse files
authored
RATIS-2251. Migrate ratis-test tests to Junit 5 - Part 3. (#1227)
1 parent 8634542 commit 0557974

28 files changed

+431
-442
lines changed

ratis-server/src/test/java/org/apache/ratis/InstallSnapshotNotificationTests.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import org.apache.ratis.util.JavaUtils;
4040
import org.apache.ratis.util.Slf4jUtils;
4141
import org.apache.ratis.util.SizeInBytes;
42-
import org.junit.Assert;
43-
import org.junit.Test;
42+
import org.junit.jupiter.api.Assertions;
43+
import org.junit.jupiter.api.Test;
4444
import org.slf4j.Logger;
4545
import org.slf4j.LoggerFactory;
4646
import org.slf4j.event.Level;
@@ -194,7 +194,7 @@ private void testAddNewFollowers(CLUSTER cluster, int numRequests) throws Except
194194
try(final RaftClient client = cluster.createClient(leaderId)) {
195195
for (; i < numRequests; i++) {
196196
final RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
197-
Assert.assertTrue(reply.isSuccess());
197+
Assertions.assertTrue(reply.isSuccess());
198198
}
199199
}
200200

@@ -206,7 +206,7 @@ private void testAddNewFollowers(CLUSTER cluster, int numRequests) throws Except
206206
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
207207
nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
208208
JavaUtils.attemptRepeatedly(() -> {
209-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
209+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
210210
return null;
211211
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
212212
logs = LogSegmentPath.getLogSegmentPaths(leader.getRaftStorage());
@@ -220,7 +220,7 @@ private void testAddNewFollowers(CLUSTER cluster, int numRequests) throws Except
220220
// delete the log segments from the leader
221221
LOG.info("Delete logs {}", logs);
222222
for (LogSegmentPath path : logs) {
223-
FileUtils.deleteFully(path.getPath()); // the log may be already purged
223+
FileUtils.deleteFully(path.getPath()); // the log may be already puged
224224
}
225225

226226
// restart the peer
@@ -231,13 +231,13 @@ private void testAddNewFollowers(CLUSTER cluster, int numRequests) throws Except
231231

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

237237
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
238238
LOG.info("LeaderSnapshotInfo: {}", leaderSnapshotInfo.getTermIndex());
239239
final boolean set = LEADER_SNAPSHOT_INFO_REF.compareAndSet(null, leaderSnapshotInfo);
240-
Assert.assertTrue(set);
240+
Assertions.assertTrue(set);
241241

242242
// add two more peers
243243
final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
@@ -253,7 +253,7 @@ private void testAddNewFollowers(CLUSTER cluster, int numRequests) throws Except
253253
// leader snapshot.
254254
for (RaftServer.Division follower : cluster.getFollowers()) {
255255
final long expected = leaderSnapshotInfo.getIndex();
256-
Assert.assertEquals(expected, RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
256+
Assertions.assertEquals(expected, RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
257257
RaftSnapshotBaseTest.assertLogContent(follower, false);
258258
}
259259

@@ -279,7 +279,7 @@ private void testRestartFollower(CLUSTER cluster) throws Exception {
279279
try (final RaftClient client = cluster.createClient(leaderId)) {
280280
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
281281
final RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
282-
Assert.assertTrue(reply.isSuccess());
282+
Assertions.assertTrue(reply.isSuccess());
283283
}
284284
}
285285

@@ -290,7 +290,7 @@ private void testRestartFollower(CLUSTER cluster) throws Exception {
290290
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
291291
oldLeaderNextIndex - SNAPSHOT_TRIGGER_THRESHOLD, oldLeaderNextIndex);
292292
JavaUtils.attemptRepeatedly(() -> {
293-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
293+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
294294
return null;
295295
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
296296
}
@@ -300,7 +300,7 @@ private void testRestartFollower(CLUSTER cluster) throws Exception {
300300

301301
// generate some more traffic
302302
try (final RaftClient client = cluster.createClient(leader.getId())) {
303-
Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
303+
Assertions.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
304304
}
305305

306306
FIVE_SECONDS.sleep();
@@ -309,8 +309,8 @@ private void testRestartFollower(CLUSTER cluster) throws Exception {
309309
JavaUtils.attempt(() -> {
310310
final long newLeaderNextIndex = leader.getRaftLog().getNextIndex();
311311
LOG.info("{}: newLeaderNextIndex = {}", leaderId, newLeaderNextIndex);
312-
Assert.assertTrue(newLeaderNextIndex > oldLeaderNextIndex);
313-
Assert.assertEquals(newLeaderNextIndex, follower.getRaftLog().getNextIndex());
312+
Assertions.assertTrue(newLeaderNextIndex > oldLeaderNextIndex);
313+
Assertions.assertEquals(newLeaderNextIndex, follower.getRaftLog().getNextIndex());
314314
}, 10, ONE_SECOND, "followerNextIndex", LOG);
315315
}
316316

@@ -331,14 +331,14 @@ private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Except
331331

332332
// Let a few heartbeats pass.
333333
ONE_SECOND.sleep();
334-
Assert.assertEquals(0, numSnapshotRequests.get());
334+
Assertions.assertEquals(0, numSnapshotRequests.get());
335335

336336
// Generate data.
337337
try(final RaftClient client = cluster.createClient(leaderId)) {
338338
for (; i < 10; i++) {
339339
RaftClientReply
340340
reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
341-
Assert.assertTrue(reply.isSuccess());
341+
Assertions.assertTrue(reply.isSuccess());
342342
}
343343
}
344344

@@ -349,42 +349,42 @@ private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Except
349349

350350
// Take snapshot and check result.
351351
long snapshotIndex = cluster.getLeader().getStateMachine().takeSnapshot();
352-
Assert.assertEquals(20, snapshotIndex);
352+
Assertions.assertEquals(20, snapshotIndex);
353353
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
354-
Assert.assertEquals(20, leaderSnapshotInfo.getIndex());
354+
Assertions.assertEquals(20, leaderSnapshotInfo.getIndex());
355355
final boolean set = LEADER_SNAPSHOT_INFO_REF.compareAndSet(null, leaderSnapshotInfo);
356-
Assert.assertTrue(set);
356+
Assertions.assertTrue(set);
357357

358358
// Wait for the snapshot to be done.
359359
final RaftServer.Division leader = cluster.getLeader();
360360
final long nextIndex = leader.getRaftLog().getNextIndex();
361-
Assert.assertEquals(21, nextIndex);
361+
Assertions.assertEquals(21, nextIndex);
362362
// End index is exclusive.
363363
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
364364
0, nextIndex);
365365
JavaUtils.attemptRepeatedly(() -> {
366-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
366+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
367367
return null;
368368
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
369369

370370
// Clear all log files and reset cached log start index.
371371
long snapshotInstallIndex =
372372
leader.getRaftLog().onSnapshotInstalled(leader.getRaftLog().getLastCommittedIndex()).get();
373-
Assert.assertEquals(20, snapshotInstallIndex);
373+
Assertions.assertEquals(20, snapshotInstallIndex);
374374

375375
// Check that logs are gone.
376-
Assert.assertEquals(0,
376+
Assertions.assertEquals(0,
377377
LogSegmentPath.getLogSegmentPaths(leader.getRaftStorage()).size());
378-
Assert.assertEquals(RaftLog.INVALID_LOG_INDEX, leader.getRaftLog().getStartIndex());
378+
Assertions.assertEquals(RaftLog.INVALID_LOG_INDEX, leader.getRaftLog().getStartIndex());
379379

380380
// Allow some heartbeats to go through, then make sure none of them had
381381
// snapshot requests.
382382
ONE_SECOND.sleep();
383-
Assert.assertEquals(0, numSnapshotRequests.get());
383+
Assertions.assertEquals(0, numSnapshotRequests.get());
384384

385385
// Make sure leader and followers are still up to date.
386386
for (RaftServer.Division follower : cluster.getFollowers()) {
387-
Assert.assertEquals(
387+
Assertions.assertEquals(
388388
leader.getRaftLog().getNextIndex(),
389389
follower.getRaftLog().getNextIndex());
390390
}
@@ -400,7 +400,7 @@ private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Except
400400

401401
// Generate more data.
402402
try (final RaftClient client = cluster.createClient(leader.getId())) {
403-
Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
403+
Assertions.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
404404
}
405405

406406
// Make sure leader and followers are still up to date.
@@ -412,7 +412,7 @@ private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Except
412412
}
413413

414414
// Make sure each new peer got one snapshot notification.
415-
Assert.assertEquals(2, numSnapshotRequests.get());
415+
Assertions.assertEquals(2, numSnapshotRequests.get());
416416

417417
} finally {
418418
cluster.shutdown();
@@ -437,7 +437,7 @@ private void testInstallSnapshotInstalledEvent(CLUSTER cluster) throws Exception
437437
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
438438
RaftClientReply
439439
reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
440-
Assert.assertTrue(reply.isSuccess());
440+
Assertions.assertTrue(reply.isSuccess());
441441
}
442442
}
443443

@@ -448,7 +448,7 @@ private void testInstallSnapshotInstalledEvent(CLUSTER cluster) throws Exception
448448
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
449449
nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
450450
JavaUtils.attemptRepeatedly(() -> {
451-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
451+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
452452
return null;
453453
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
454454
logs = LogSegmentPath.getLogSegmentPaths(leader.getRaftStorage());
@@ -470,13 +470,13 @@ private void testInstallSnapshotInstalledEvent(CLUSTER cluster) throws Exception
470470

471471
// generate some more traffic
472472
try(final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
473-
Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
473+
Assertions.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
474474
}
475475

476476
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
477477
LOG.info("LeaderSnapshotInfo: {}", leaderSnapshotInfo.getTermIndex());
478478
final boolean set = LEADER_SNAPSHOT_INFO_REF.compareAndSet(null, leaderSnapshotInfo);
479-
Assert.assertTrue(set);
479+
Assertions.assertTrue(set);
480480

481481
// add one new peer
482482
final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(1, true, true);
@@ -490,7 +490,7 @@ private void testInstallSnapshotInstalledEvent(CLUSTER cluster) throws Exception
490490
// Check the installed snapshot index on each Follower matches with the
491491
// leader snapshot.
492492
for (RaftServer.Division follower : cluster.getFollowers()) {
493-
Assert.assertEquals(leaderSnapshotInfo.getIndex(),
493+
Assertions.assertEquals(leaderSnapshotInfo.getIndex(),
494494
RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
495495
}
496496

@@ -500,9 +500,9 @@ private void testInstallSnapshotInstalledEvent(CLUSTER cluster) throws Exception
500500
(SimpleStateMachine4Testing) cluster.getFollowers().get(0).getStateMachine();
501501
final File followerSnapshotFile = new File(followerStateMachine.getStateMachineDir(),
502502
leaderSnapshotFile.getName());
503-
Assert.assertEquals(numNotifyInstallSnapshotFinished.get(), 2);
504-
Assert.assertTrue(leaderSnapshotFile.exists());
505-
Assert.assertFalse(followerSnapshotFile.exists());
503+
Assertions.assertEquals(numNotifyInstallSnapshotFinished.get(), 2);
504+
Assertions.assertTrue(leaderSnapshotFile.exists());
505+
Assertions.assertFalse(followerSnapshotFile.exists());
506506

507507
// restart the peer and check if it can correctly handle conf change
508508
cluster.restartServer(cluster.getLeader().getId(), false);
@@ -535,7 +535,7 @@ private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exceptio
535535
for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
536536
RaftClientReply
537537
reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
538-
Assert.assertTrue(reply.isSuccess());
538+
Assertions.assertTrue(reply.isSuccess());
539539
}
540540
}
541541

@@ -546,15 +546,15 @@ private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exceptio
546546
final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster,
547547
nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
548548
JavaUtils.attemptRepeatedly(() -> {
549-
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
549+
Assertions.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
550550
return null;
551551
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
552552

553553
RaftSnapshotBaseTest.assertLeaderContent(cluster);
554554

555555
final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
556556
final boolean set = LEADER_SNAPSHOT_INFO_REF.compareAndSet(null, leaderSnapshotInfo);
557-
Assert.assertTrue(set);
557+
Assertions.assertTrue(set);
558558

559559
// add two more peers
560560
final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
@@ -568,12 +568,12 @@ private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exceptio
568568
// Check the installed snapshot index on each Follower matches with the
569569
// leader snapshot.
570570
for (RaftServer.Division follower : cluster.getFollowers()) {
571-
Assert.assertEquals(leaderSnapshotInfo.getIndex(),
571+
Assertions.assertEquals(leaderSnapshotInfo.getIndex(),
572572
RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
573573
}
574574

575575
// Make sure each new peer got at least one snapshot notification.
576-
Assert.assertTrue(2 <= numSnapshotRequests.get());
576+
Assertions.assertTrue(2 <= numSnapshotRequests.get());
577577
} finally {
578578
cluster.shutdown();
579579
}

ratis-server/src/test/java/org/apache/ratis/LogAppenderTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.ratis;
1919

2020
import static org.apache.ratis.RaftTestUtil.waitForLeader;
21-
import static org.junit.Assert.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import org.apache.ratis.RaftTestUtil.SimpleMessage;
2424
import org.apache.ratis.client.RaftClient;
@@ -40,8 +40,8 @@
4040
import org.apache.ratis.util.JavaUtils;
4141
import org.apache.ratis.util.Slf4jUtils;
4242
import org.apache.ratis.util.SizeInBytes;
43-
import org.junit.Assert;
44-
import org.junit.Test;
43+
import org.junit.jupiter.api.Assertions;
44+
import org.junit.jupiter.api.Test;
4545

4646
import java.io.IOException;
4747
import java.util.ArrayList;
@@ -200,7 +200,7 @@ void runTest(CLUSTER cluster) throws Exception {
200200
if (e != null) {
201201
throw e;
202202
}
203-
Assert.assertTrue(s.succeed.get());
203+
Assertions.assertTrue(s.succeed.get());
204204
}
205205
} finally {
206206
for (int i = 0; i < clients.size(); i ++) {
@@ -216,11 +216,11 @@ void runTest(CLUSTER cluster) throws Exception {
216216
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
217217
final EnumMap<LogEntryBodyCase, AtomicLong> counts = RaftTestUtil.countEntries(leaderLog);
218218
LOG.info("counts = " + counts);
219-
Assert.assertEquals(6 * numMsgs * numClients, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
219+
Assertions.assertEquals(6 * numMsgs * numClients, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
220220

221221
final LogEntryProto last = RaftTestUtil.getLastEntry(LogEntryBodyCase.STATEMACHINELOGENTRY, leaderLog);
222222
LOG.info("last = {}", LogProtoUtils.toLogEntryString(last));
223-
Assert.assertNotNull(last);
224-
Assert.assertTrue(last.getIndex() <= leader.getInfo().getLastAppliedIndex());
223+
Assertions.assertNotNull(last);
224+
Assertions.assertTrue(last.getIndex() <= leader.getInfo().getLastAppliedIndex());
225225
}
226226
}

ratis-server/src/test/java/org/apache/ratis/MessageStreamApiTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
3232
import org.apache.ratis.util.Slf4jUtils;
3333
import org.apache.ratis.util.SizeInBytes;
34-
import org.junit.Assert;
35-
import org.junit.Test;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.Test;
3636
import org.slf4j.event.Level;
3737

3838
import java.nio.charset.StandardCharsets;
@@ -75,11 +75,11 @@ void runTestStream(CLUSTER cluster) throws Exception {
7575
try(RaftClient client = cluster.createClient()) {
7676
final String k1 = k.substring(0, endOfRequest);
7777
final RaftClientReply r1= client.io().sendReadOnly(new SimpleMessage(k1));
78-
Assert.assertTrue(r1.isSuccess());
78+
Assertions.assertTrue(r1.isSuccess());
7979

8080
final String k2 = k.substring(endOfRequest);
8181
final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
82-
Assert.assertTrue(r2.isSuccess());
82+
Assertions.assertTrue(r2.isSuccess());
8383
}
8484
}
8585

@@ -104,24 +104,24 @@ void runTestStreamAsync(CLUSTER cluster) throws Exception {
104104
final String s = (char)('A' + i) + "1234567";
105105
LOG.info("s=" + s);
106106
final ByteString b = ByteString.copyFrom(s, StandardCharsets.UTF_8);
107-
Assert.assertEquals(8, b.size());
107+
Assertions.assertEquals(8, b.size());
108108
for(int j = 0; j < 128; j++) {
109109
bytes = bytes.concat(b);
110110
}
111111
i++;
112-
Assert.assertEquals(i*SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
112+
Assertions.assertEquals(i*SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
113113
}
114114

115115
try(RaftClient client = cluster.createClient()) {
116116
final RaftClientReply reply = client.getMessageStreamApi().streamAsync(Message.valueOf(bytes)).get();
117-
Assert.assertTrue(reply.isSuccess());
117+
Assertions.assertTrue(reply.isSuccess());
118118
}
119119

120120

121121
// check if all the parts are streamed as a single message.
122122
try(RaftClient client = cluster.createClient()) {
123123
final RaftClientReply reply = client.io().sendReadOnly(new SimpleMessage(bytes.toString(StandardCharsets.UTF_8)));
124-
Assert.assertTrue(reply.isSuccess());
124+
Assertions.assertTrue(reply.isSuccess());
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)