szetszwo commented on code in PR #1166:
URL: https://github.com/apache/ratis/pull/1166#discussion_r1797231645
##########
ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java:
##########
@@ -97,6 +100,79 @@ public void testBasicLeaderElection() throws Exception {
cluster.shutdown();
}
+ static class SleepCode implements CodeInjectionForTesting.Code {
+ private final long sleepMs;
+
+ SleepCode(long sleepMs) {
+ this.sleepMs = sleepMs;
+ }
+
+ @Override
+ public boolean execute(Object localId, Object remoteId, Object... args) {
+ try {
+ LOG.info("{}: Simulate RaftServer startup blocking", localId);
+ Thread.sleep(sleepMs);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return true;
+ }
+ }
+
+ @Test
+ public void testWaitServerReady() throws Exception {
+ final int sleepMs = 1000 + ThreadLocalRandom.current().nextInt(1000);
+ LOG.info("Running testWaitServerReady, sleep = {}ms", sleepMs);
+ CodeInjectionForTesting.put(RaftServerImpl.START_COMPLETE, new
SleepCode(sleepMs));
+ final MiniRaftCluster cluster = newCluster(1);
+ final Timestamp startTime = Timestamp.currentTime();
+ cluster.start();
+ LOG.info("Cluster started at {}ms", startTime.elapsedTimeMs());
+ final RaftGroupId groupId = cluster.getGroupId();
+ final RaftServerImpl server = (RaftServerImpl)
cluster.getServers().iterator().next().getDivision(groupId);
+ final boolean isRunning = server.isRunning();
+ LOG.info("{} isRunning at {}ms? {}", server.getId(),
startTime.elapsedTimeMs(), isRunning);
+
+ // Leader will be elected if the server is ready
+ Assertions.assertNotNull(waitForLeader(cluster), "No leader is elected.");
+ final long elapsedMs = startTime.elapsedTimeMs();
+ // allow a small difference to tolerate system timer inaccuracy
+ Assertions.assertTrue(elapsedMs > sleepMs - 10, () -> "elapseMs = " +
elapsedMs + " but sleepMs = " + sleepMs);
+ cluster.shutdown();
+ CodeInjectionForTesting.remove(RaftServerImpl.START_COMPLETE);
+ }
+
+ @Test
+ public void testAddServerForWaitReady() throws IOException,
InterruptedException {
+ LOG.info("Running testAddServerForWaitReady");
+ // normal startup cluster with 3 server
+ final MiniRaftCluster cluster = newCluster(3);
+ cluster.start();
+ RaftTestUtil.waitForLeader(cluster);
+ try (RaftClient client = cluster.createClient()) {
+ for (int i = 0; i < 10; ++i) {
+ RaftClientReply reply = client.io().send(new
RaftTestUtil.SimpleMessage("message_" + i));
+ Assertions.assertTrue(reply.isSuccess());
+ }
+ // add 3 new servers and wait longer time
+ CodeInjectionForTesting.put(RaftServerImpl.START_COMPLETE, new
SleepCode(2000));
Review Comment:
Okay, I fine to keep this test. Then, we need to add
`CodeInjectionForTesting.remove` at the end.
##########
ratis-server/src/test/java/org/apache/ratis/server/impl/MiniRaftCluster.java:
##########
@@ -347,7 +347,6 @@ public void start() throws IOException {
initServers();
startServers(servers.values());
-
Review Comment:
There still a whitespace change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]