|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hugegraph.pd.raft; |
| 19 | + |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | + |
| 25 | +import org.apache.hugegraph.pd.config.PDConfig; |
| 26 | +import org.apache.hugegraph.testutil.Whitebox; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import com.alipay.sofa.jraft.Node; |
| 33 | +import com.alipay.sofa.jraft.entity.PeerId; |
| 34 | +import com.alipay.sofa.jraft.util.Endpoint; |
| 35 | + |
| 36 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 37 | +import static org.mockito.ArgumentMatchers.anyString; |
| 38 | +import static org.mockito.ArgumentMatchers.eq; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
| 42 | +public class RaftEngineLeaderAddressTest { |
| 43 | + |
| 44 | + private static final String LEADER_IP = "10.0.0.1"; |
| 45 | + private static final int GRPC_PORT = 8686; |
| 46 | + private static final String LEADER_GRPC_ADDRESS = "10.0.0.1:8686"; |
| 47 | + |
| 48 | + private Node originalRaftNode; |
| 49 | + private RaftRpcClient originalRaftRpcClient; |
| 50 | + private PDConfig.Raft originalConfig; |
| 51 | + |
| 52 | + private Node mockNode; |
| 53 | + private RaftRpcClient mockRpcClient; |
| 54 | + private PDConfig.Raft mockConfig; |
| 55 | + private PeerId mockLeader; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + RaftEngine engine = RaftEngine.getInstance(); |
| 60 | + |
| 61 | + // Save originals |
| 62 | + originalRaftNode = engine.getRaftNode(); |
| 63 | + originalRaftRpcClient = Whitebox.getInternalState(engine, "raftRpcClient"); |
| 64 | + originalConfig = Whitebox.getInternalState(engine, "config"); |
| 65 | + |
| 66 | + // Build mock leader PeerId with real Endpoint |
| 67 | + mockLeader = mock(PeerId.class); |
| 68 | + Endpoint endpoint = new Endpoint(LEADER_IP, 8610); |
| 69 | + when(mockLeader.getEndpoint()).thenReturn(endpoint); |
| 70 | + |
| 71 | + // Build mock Node that reports itself as follower with a known leader |
| 72 | + mockNode = mock(Node.class); |
| 73 | + when(mockNode.isLeader(true)).thenReturn(false); |
| 74 | + when(mockNode.getLeaderId()).thenReturn(mockLeader); |
| 75 | + |
| 76 | + // Build mock config |
| 77 | + // Use a short default timeout (100ms); specific tests may override getRpcTimeout() |
| 78 | + mockConfig = mock(PDConfig.Raft.class); |
| 79 | + when(mockConfig.getGrpcAddress()).thenReturn("127.0.0.1:" + GRPC_PORT); |
| 80 | + when(mockConfig.getGrpcPort()).thenReturn(GRPC_PORT); |
| 81 | + when(mockConfig.getRpcTimeout()).thenReturn(100); |
| 82 | + |
| 83 | + // Build mock RpcClient |
| 84 | + mockRpcClient = mock(RaftRpcClient.class); |
| 85 | + |
| 86 | + // Inject mocks |
| 87 | + Whitebox.setInternalState(engine, "raftNode", mockNode); |
| 88 | + Whitebox.setInternalState(engine, "raftRpcClient", mockRpcClient); |
| 89 | + Whitebox.setInternalState(engine, "config", mockConfig); |
| 90 | + } |
| 91 | + |
| 92 | + @After |
| 93 | + public void tearDown() { |
| 94 | + RaftEngine engine = RaftEngine.getInstance(); |
| 95 | + Whitebox.setInternalState(engine, "raftNode", originalRaftNode); |
| 96 | + Whitebox.setInternalState(engine, "raftRpcClient", originalRaftRpcClient); |
| 97 | + Whitebox.setInternalState(engine, "config", originalConfig); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testSuccessReturnsGrpcAddress() throws Exception { |
| 102 | + // RPC succeeds and returns a valid gRPC address |
| 103 | + RaftRpcProcessor.GetMemberResponse response = |
| 104 | + mock(RaftRpcProcessor.GetMemberResponse.class); |
| 105 | + when(response.getGrpcAddress()).thenReturn(LEADER_GRPC_ADDRESS); |
| 106 | + |
| 107 | + CompletableFuture<RaftRpcProcessor.GetMemberResponse> future = |
| 108 | + CompletableFuture.completedFuture(response); |
| 109 | + when(mockRpcClient.getGrpcAddress(anyString())).thenReturn(future); |
| 110 | + |
| 111 | + String result = RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 112 | + Assert.assertEquals(LEADER_GRPC_ADDRESS, result); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testTimeoutFallsBackToDerivedAddress() throws Exception { |
| 117 | + // RPC times out — should fall back to leaderIp:grpcPort |
| 118 | + CompletableFuture<RaftRpcProcessor.GetMemberResponse> future = |
| 119 | + mock(CompletableFuture.class); |
| 120 | + when(future.get(anyLong(), eq(TimeUnit.MILLISECONDS))) |
| 121 | + .thenThrow(new TimeoutException("simulated timeout")); |
| 122 | + when(mockRpcClient.getGrpcAddress(anyString())).thenReturn(future); |
| 123 | + |
| 124 | + String result = RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 125 | + Assert.assertEquals(LEADER_IP + ":" + GRPC_PORT, result); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testRpcExceptionFallsBackToDerivedAddress() throws Exception { |
| 130 | + // RPC throws ExecutionException — should fall back to leaderIp:grpcPort |
| 131 | + CompletableFuture<RaftRpcProcessor.GetMemberResponse> future = |
| 132 | + mock(CompletableFuture.class); |
| 133 | + when(future.get(anyLong(), eq(TimeUnit.MILLISECONDS))) |
| 134 | + .thenThrow(new ExecutionException("simulated rpc failure", |
| 135 | + new RuntimeException("bolt error"))); |
| 136 | + when(mockRpcClient.getGrpcAddress(anyString())).thenReturn(future); |
| 137 | + |
| 138 | + String result = RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 139 | + Assert.assertEquals(LEADER_IP + ":" + GRPC_PORT, result); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testNullResponseFallsBackToDerivedAddress() throws Exception { |
| 144 | + // RPC returns null response — should fall back to leaderIp:grpcPort |
| 145 | + CompletableFuture<RaftRpcProcessor.GetMemberResponse> future = |
| 146 | + CompletableFuture.completedFuture(null); |
| 147 | + when(mockRpcClient.getGrpcAddress(anyString())).thenReturn(future); |
| 148 | + |
| 149 | + String result = RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 150 | + Assert.assertEquals(LEADER_IP + ":" + GRPC_PORT, result); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testNullGrpcAddressInResponseFallsBackToDerivedAddress() throws Exception { |
| 155 | + // RPC returns a response but grpcAddress field is null — should fall back |
| 156 | + RaftRpcProcessor.GetMemberResponse response = |
| 157 | + mock(RaftRpcProcessor.GetMemberResponse.class); |
| 158 | + when(response.getGrpcAddress()).thenReturn(null); |
| 159 | + |
| 160 | + CompletableFuture<RaftRpcProcessor.GetMemberResponse> future = |
| 161 | + CompletableFuture.completedFuture(response); |
| 162 | + when(mockRpcClient.getGrpcAddress(anyString())).thenReturn(future); |
| 163 | + |
| 164 | + String result = RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 165 | + Assert.assertEquals(LEADER_IP + ":" + GRPC_PORT, result); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testNullLeaderAfterWaitThrowsExecutionException() throws Exception { |
| 170 | + // Use 0ms timeout so waitingForLeader(0) skips the wait loop and returns immediately |
| 171 | + when(mockConfig.getRpcTimeout()).thenReturn(0); |
| 172 | + // Leader is still null after waitingForLeader() — should throw ExecutionException |
| 173 | + when(mockNode.getLeaderId()).thenReturn(null); |
| 174 | + |
| 175 | + try { |
| 176 | + RaftEngine.getInstance().getLeaderGrpcAddress(); |
| 177 | + Assert.fail("Expected ExecutionException"); |
| 178 | + } catch (ExecutionException e) { |
| 179 | + Assert.assertTrue(e.getCause() instanceof IllegalStateException); |
| 180 | + Assert.assertEquals("Leader is not ready", e.getCause().getMessage()); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments