Skip to content

Commit

Permalink
[close #570] mockserver: fix unstable mock server cluster setup (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
iosmanthus authored Mar 25, 2022
1 parent 7278d5a commit f4e7c30
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
18 changes: 10 additions & 8 deletions src/test/java/org/tikv/common/MockThreeStoresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public class MockThreeStoresTest extends PDMockServerTest {
public void setup() throws IOException {
super.setup();

int basePort;
try (ServerSocket s = new ServerSocket(0)) {
basePort = s.getLocalPort();
int[] ports = new int[3];
for (int i = 0; i < ports.length; i++) {
try (ServerSocket s = new ServerSocket(0)) {
ports[i] = s.getLocalPort();
}
}

ImmutableList<Metapb.Peer> peers =
Expand All @@ -65,17 +67,17 @@ public void setup() throws IOException {
stores =
ImmutableList.of(
Metapb.Store.newBuilder()
.setAddress("127.0.0.1:" + basePort)
.setAddress("127.0.0.1:" + ports[0])
.setVersion("5.0.0")
.setId(0x1)
.build(),
Metapb.Store.newBuilder()
.setAddress("127.0.0.1:" + (basePort + 1))
.setAddress("127.0.0.1:" + ports[1])
.setVersion("5.0.0")
.setId(0x2)
.build(),
Metapb.Store.newBuilder()
.setAddress("127.0.0.1:" + (basePort + 2))
.setAddress("127.0.0.1:" + ports[2])
.setVersion("5.0.0")
.setId(0x3)
.build());
Expand All @@ -101,9 +103,9 @@ public void setup() throws IOException {
region.getPeers(0),
region.getPeersList(),
stores.stream().map(TiStore::new).collect(Collectors.toList()));
for (int i = 0; i < 3; i++) {
for (int port : ports) {
KVMockServer server = new KVMockServer();
server.start(this.region, basePort + i);
server.start(this.region, port);
servers.add(server);
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/test/java/org/tikv/common/PDClientMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,28 @@ public class PDClientMockTest extends PDMockServerTest {
@Test
public void testCreate() throws Exception {
try (PDClient client = session.getPDClient()) {
assertEquals(LOCAL_ADDR + ":" + leader.port, client.getPdClientWrapper().getLeaderInfo());
assertEquals(
LOCAL_ADDR + ":" + leader.getPort(), client.getPdClientWrapper().getLeaderInfo());
assertEquals(CLUSTER_ID, client.getHeader().getClusterId());
}
}

@Test
public void testSwitchLeader() throws Exception {
try (PDClient client = session.getPDClient()) {
client.trySwitchLeader(HTTP + LOCAL_ADDR + ":" + (leader.port + 1));
// Switch leader to server 1
client.trySwitchLeader(HTTP + LOCAL_ADDR + ":" + pdServers.get(1).getPort());
assertEquals(
client.getPdClientWrapper().getLeaderInfo(), HTTP + LOCAL_ADDR + ":" + (leader.port + 1));
client.getPdClientWrapper().getLeaderInfo(),
HTTP + LOCAL_ADDR + ":" + pdServers.get(1).getPort());
}
tearDown();
setup(LOCAL_ADDR_IPV6);
try (PDClient client = session.getPDClient()) {
client.trySwitchLeader(HTTP + LOCAL_ADDR_IPV6 + ":" + (leader.port + 2));
client.trySwitchLeader(HTTP + LOCAL_ADDR_IPV6 + ":" + pdServers.get(2).getPort());
assertEquals(
client.getPdClientWrapper().getLeaderInfo(),
HTTP + LOCAL_ADDR_IPV6 + ":" + (leader.port + 2));
HTTP + LOCAL_ADDR_IPV6 + ":" + pdServers.get(2).getPort());
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/tikv/common/PDMockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
import org.tikv.kvproto.Pdpb.TsoResponse;

public class PDMockServer extends PDGrpc.PDImplBase {

public int port;
private int port;
private long clusterId;
private Server server;

Expand Down Expand Up @@ -148,4 +147,8 @@ public void stop() {
public long getClusterId() {
return clusterId;
}

public long getPort() {
return port;
}
}
20 changes: 11 additions & 9 deletions src/test/java/org/tikv/common/PDMockServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,30 @@ public void setup() throws IOException {
}

void setup(String addr) throws IOException {
int basePort;
try (ServerSocket s = new ServerSocket(0)) {
basePort = s.getLocalPort();
int[] ports = new int[3];
for (int i = 0; i < ports.length; i++) {
try (ServerSocket s = new ServerSocket(0)) {
ports[i] = s.getLocalPort();
}
}

for (int i = 0; i < 3; i++) {
for (int i = 0; i < ports.length; i++) {
PDMockServer server = new PDMockServer();
server.start(CLUSTER_ID, basePort + i);
server.start(CLUSTER_ID, ports[i]);
server.addGetMembersListener(
(request) ->
GrpcUtils.makeGetMembersResponse(
server.getClusterId(),
GrpcUtils.makeMember(1, "http://" + addr + ":" + basePort),
GrpcUtils.makeMember(2, "http://" + addr + ":" + (basePort + 1)),
GrpcUtils.makeMember(3, "http://" + addr + ":" + (basePort + 2))));
GrpcUtils.makeMember(1, "http://" + addr + ":" + ports[0]),
GrpcUtils.makeMember(2, "http://" + addr + ":" + ports[1]),
GrpcUtils.makeMember(3, "http://" + addr + ":" + ports[2])));
pdServers.add(server);
if (i == 0) {
leader = server;
}
}

TiConfiguration conf = TiConfiguration.createDefault(addr + ":" + leader.port);
TiConfiguration conf = TiConfiguration.createDefault(addr + ":" + ports[0]);
conf.setKvMode("RAW");
conf.setWarmUpEnable(false);
conf.setTimeout(2000);
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/tikv/common/SeekLeaderStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.tikv.raw.RawKVClient;

public class SeekLeaderStoreTest extends MockThreeStoresTest {

private RawKVClient createClient() {
return session.createRawClient();
}
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/tikv/common/SeekProxyStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.tikv.raw.RawKVClient;

public class SeekProxyStoreTest extends MockThreeStoresTest {

private RawKVClient createClient() {
return session.createRawClient();
}
Expand Down

0 comments on commit f4e7c30

Please sign in to comment.