Skip to content

Commit

Permalink
test: basic vote test
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Dec 6, 2024
1 parent 235cc89 commit 60516ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.dtprj.dongting.raft.impl;

import static com.github.dtprj.dongting.util.Tick.tick;

/**
* @author huangli
*/
Expand All @@ -24,13 +26,13 @@ public static void updateNodeManager(NodeManager nodeManager) {
}

public static void updateMemberManager(MemberManager memberManager) {
memberManager.daemonSleepInterval = 1;
memberManager.daemonSleepInterval = 11;
}

public static void updateVoteManager(VoteManager voteManager) {
voteManager.firstDelayMin = 0;
voteManager.firstDelayMax = 1;
voteManager.firstDelayMax = 3;
voteManager.checkIntervalMin = 1;
voteManager.checkIntervalMax = 5;
voteManager.checkIntervalMax = tick(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected static class ServerInfo {
public GroupComponents gc;
}

protected ServerInfo createServer(int nodeId, String servers, String nodeIdOfMembers) {
protected ServerInfo createServer(int nodeId, String servers, String nodeIdOfMembers, String nodeIdOfObservers) {
int replicatePort = 4000 + nodeId;
int servicePort = 5000 + nodeId;
int groupId = 1;
Expand All @@ -56,8 +56,9 @@ protected ServerInfo createServer(int nodeId, String servers, String nodeIdOfMem
serverConfig.setReplicatePort(replicatePort);
serverConfig.setServicePort(servicePort);
serverConfig.setElectTimeout(tick(10));
serverConfig.setHeartbeatInterval(tick(4));

RaftGroupConfig groupConfig = RaftGroupConfig.newInstance(groupId, nodeIdOfMembers, "");
RaftGroupConfig groupConfig = RaftGroupConfig.newInstance(groupId, nodeIdOfMembers, nodeIdOfObservers);
groupConfig.setDataDir(DATA_DIR + "-" + nodeId);

DefaultRaftFactory raftFactory = createRaftFactory(nodeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,51 @@
*/
package com.github.dtprj.dongting.raft.server;

import com.github.dtprj.dongting.common.DtTime;
import com.github.dtprj.dongting.raft.test.TestUtil;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author huangli
*/
public class VoteTest extends ServerTestBase {

@Test
void test() throws Exception {
String servers = "1,127.0.0.1:4001;2,127.0.0.1:4002;3,127.0.0.1:4003";
String members = "1,2,3";
ServerInfo s1 = createServer(1, servers, members);
ServerInfo s2 = createServer(2, servers, members);
ServerInfo s3 = createServer(3, servers, members);

s1.raftServer.getAllMemberReadyFuture().get();
s2.raftServer.getAllMemberReadyFuture().get();
s3.raftServer.getAllMemberReadyFuture().get();

private int waitLeaderElectAndGetLeaderId(ServerInfo... servers) {
AtomicInteger leaderId = new AtomicInteger();
TestUtil.waitUtil(() -> {
int leader = 0;
if (s1.raftServer.getRaftGroup(1).isLeader()) {
leader++;
}
if (s2.raftServer.getRaftGroup(1).isLeader()) {
leader++;
}
if (s3.raftServer.getRaftGroup(1).isLeader()) {
leader++;
for (ServerInfo server : servers) {
if (server.raftServer.getRaftGroup(1).isLeader()) {
leader++;
leaderId.set(server.nodeId);
}
}
return leader == 1;
});
return leaderId.get();
}

@Test
void test() throws Exception {
String servers = "1,127.0.0.1:4001;2,127.0.0.1:4002;3,127.0.0.1:4003;4,127.0.0.1:4004";
String members = "1,2,3";
String observers = "4";
ServerInfo[] sis = new ServerInfo[4];
sis[0] = createServer(1, servers, members, observers);
sis[1] = createServer(2, servers, members, observers);
sis[2] = createServer(3, servers, members, observers);
sis[3] = createServer(4, servers, members, observers);

for (ServerInfo si : sis) {
si.raftServer.getAllMemberReadyFuture().get();
}

DtTime timeout = new DtTime(5, TimeUnit.SECONDS);
s1.raftServer.stop(timeout);
s2.raftServer.stop(timeout);
s3.raftServer.stop(timeout);
int leader = waitLeaderElectAndGetLeaderId(sis);
assertTrue(leader != 4);
// DtTime timeout = new DtTime(5, TimeUnit.SECONDS);
// sis[leader-1].raftServer.stop(timeout);
}
}

0 comments on commit 60516ce

Please sign in to comment.