WangzJi commented on code in PR #7779:
URL: https://github.com/apache/incubator-seata/pull/7779#discussion_r2576796751
##########
discovery/seata-discovery-raft/src/test/java/org/apache/seata/discovery/registry/raft/RaftRegistryServiceImplTest.java:
##########
@@ -167,13 +189,1171 @@ public void testSelectEndpoint()
MetadataResponse metadataResponse = objectMapper.readValue(jsonString,
MetadataResponse.class);
List<Node> nodes = metadataResponse.getNodes();
+ // Verify endpoint selection works and returns valid endpoints
for (Node node : nodes) {
String controlEndpointStr = (String)
selectControlEndpointStrMethod.invoke(null, node);
- ;
String transactionEndpointStr = (String)
selectTransactionEndpointStrMethod.invoke(null, node);
- ;
-
Assertions.assertTrue(controlEndpointStr.contains("10.10.105.7:3007"));
-
Assertions.assertTrue(transactionEndpointStr.contains("10.10.105.7:3009"));
+
+ // Verify endpoints are properly formatted
+ assertNotNull(controlEndpointStr, "Control endpoint should not be
null");
+ assertNotNull(transactionEndpointStr, "Transaction endpoint should
not be null");
+ assertTrue(controlEndpointStr.contains(":"), "Control endpoint
should contain port");
+ assertTrue(transactionEndpointStr.contains(":"), "Transaction
endpoint should contain port");
+ }
+ }
+
+ /**
+ * Test singleton pattern
+ */
+ @Test
+ public void getInstanceTest() {
+ RaftRegistryServiceImpl instance1 =
RaftRegistryServiceImpl.getInstance();
+ RaftRegistryServiceImpl instance2 =
RaftRegistryServiceImpl.getInstance();
+ assertNotNull(instance1);
+ assertSame(instance1, instance2, "getInstance should return the same
instance");
+ }
+
+ /**
+ * Test register method (empty implementation)
+ */
+ @Test
+ public void registerTest() throws Exception {
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+ InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8091);
+ // Should not throw exception even though it's an empty implementation
+ assertDoesNotThrow(() -> instance.register(address));
+ }
+
+ /**
+ * Test unregister method (empty implementation)
+ */
+ @Test
+ public void unregisterTest() throws Exception {
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+ InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8091);
+ // Should not throw exception even though it's an empty implementation
+ assertDoesNotThrow(() -> instance.unregister(address));
+ }
+
+ /**
+ * Test subscribe method (empty implementation)
+ */
+ @Test
+ public void subscribeTest() throws Exception {
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+ // Should not throw exception even though it's an empty implementation
+ assertDoesNotThrow(() -> instance.subscribe("default", null));
+ }
+
+ /**
+ * Test unsubscribe method (empty implementation)
+ */
+ @Test
+ public void unsubscribeTest() throws Exception {
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+ // Should not throw exception even though it's an empty implementation
+ assertDoesNotThrow(() -> instance.unsubscribe("default", null));
+ }
+
+ /**
+ * Test close method
+ */
+ @Test
+ public void closeTest() throws NoSuchFieldException,
IllegalAccessException {
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+
+ Field closedField =
RaftRegistryServiceImpl.class.getDeclaredField("CLOSED");
+ closedField.setAccessible(true);
+ AtomicBoolean closed = (AtomicBoolean) closedField.get(null);
+
+ assertFalse(closed.get(), "CLOSED should be false initially");
+
+ instance.close();
+
+ assertTrue(closed.get(), "CLOSED should be true after close");
+ }
+
+ /**
+ * Test selectEndpoint with unsupported type
+ */
+ @Test
+ public void selectEndpointUnsupportedTypeTest() throws Exception {
+ String jsonString =
+
"{\"control\":{\"host\":\"localhost\",\"port\":7091},\"transaction\":{\"host\":\"localhost\",\"port\":8091}}";
+ ObjectMapper objectMapper = new ObjectMapper();
+ Node node = objectMapper.readValue(jsonString, Node.class);
+
+ Method selectEndpointMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("selectEndpoint", String.class,
Node.class);
+ selectEndpointMethod.setAccessible(true);
+
+ assertThrows(
+ NotSupportYetException.class,
+ () -> {
+ try {
+ selectEndpointMethod.invoke(null, "unsupported", node);
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ },
+ "Should throw NotSupportYetException for unsupported type");
+ }
+
+ /**
+ * Test selectExternalEndpoint with empty metadata
+ */
+ @Test
+ public void selectExternalEndpointEmptyMetadataTest() throws Exception {
+ Node node = new Node();
+ node.setMetadata(new HashMap<>());
+
+ Method selectExternalEndpointMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("selectExternalEndpoint",
Node.class, String[].class);
+ selectExternalEndpointMethod.setAccessible(true);
+
+ assertThrows(
+ ParseEndpointException.class,
+ () -> {
+ try {
+ selectExternalEndpointMethod.invoke(null, node, new
String[] {"10.10.*"});
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ },
+ "Should throw ParseEndpointException when metadata is empty");
+ }
+
+ /**
+ * Test selectExternalEndpoint with null metadata
+ */
+ @Test
+ public void selectExternalEndpointNullMetadataTest() throws Exception {
+ Node node = new Node();
+ node.setMetadata(null);
+
+ Method selectExternalEndpointMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("selectExternalEndpoint",
Node.class, String[].class);
+ selectExternalEndpointMethod.setAccessible(true);
+
+ assertThrows(
+ ParseEndpointException.class,
+ () -> {
+ try {
+ selectExternalEndpointMethod.invoke(null, node, new
String[] {"10.10.*"});
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ },
+ "Should throw ParseEndpointException when metadata is null");
+ }
+
+ /**
+ * Test selectExternalEndpoint with empty external endpoints
+ */
+ @Test
+ public void selectExternalEndpointEmptyExternalListTest() throws Exception
{
+ Node node = new Node();
+ Map<String, Object> metadata = new HashMap<>();
+ metadata.put("external", new ArrayList<>());
+ node.setMetadata(metadata);
+
+ Method selectExternalEndpointMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("selectExternalEndpoint",
Node.class, String[].class);
+ selectExternalEndpointMethod.setAccessible(true);
+
+ assertThrows(
+ ParseEndpointException.class,
+ () -> {
+ try {
+ selectExternalEndpointMethod.invoke(null, node, new
String[] {"10.10.*"});
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ },
+ "Should throw ParseEndpointException when external endpoints
list is empty");
+ }
+
+ /**
+ * Test selectExternalEndpoint with no matching network
+ */
+ @Test
+ public void selectExternalEndpointNoMatchingNetworkTest() throws Exception
{
+ Node node = new Node();
+ Map<String, Object> metadata = new HashMap<>();
+ List<LinkedHashMap<String, Object>> externalList = new ArrayList<>();
+ LinkedHashMap<String, Object> external = new LinkedHashMap<>();
+ external.put("host", "192.168.1.1");
+ external.put("controlPort", 7091);
+ external.put("transactionPort", 8091);
+ externalList.add(external);
+ metadata.put("external", externalList);
+ node.setMetadata(metadata);
+
+ Method selectExternalEndpointMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("selectExternalEndpoint",
Node.class, String[].class);
+ selectExternalEndpointMethod.setAccessible(true);
+
+ assertThrows(
+ ParseEndpointException.class,
+ () -> {
+ try {
+ selectExternalEndpointMethod.invoke(null, node, new
String[] {"10.10.*"});
+ } catch (InvocationTargetException e) {
+ throw e.getCause();
+ }
+ },
+ "Should throw ParseEndpointException when no external endpoint
matches preferred network");
+ }
+
+ /**
+ * Test isPreferredNetwork method
+ */
+ @Test
+ public void isPreferredNetworkTest() throws Exception {
+ Method isPreferredNetworkMethod =
+
RaftRegistryServiceImpl.class.getDeclaredMethod("isPreferredNetwork",
String.class, List.class);
+ isPreferredNetworkMethod.setAccessible(true);
+
+ // Test with prefix match
+ boolean result = (boolean) isPreferredNetworkMethod.invoke(null,
"10.10.105.7", Arrays.asList("10.10.*"));
+ assertTrue(result, "Should match with prefix 10.10.*");
+
+ // Test with regex match
+ result = (boolean) isPreferredNetworkMethod.invoke(null,
"192.168.1.1", Arrays.asList("192\\.168\\..*"));
+ assertTrue(result, "Should match with regex pattern");
+
+ // Test with no match
+ result = (boolean) isPreferredNetworkMethod.invoke(null, "172.16.0.1",
Arrays.asList("10.10.*", "192.168.*"));
+ assertFalse(result, "Should not match when IP doesn't match any
pattern");
+
+ // Test with blank pattern
+ result = (boolean) isPreferredNetworkMethod.invoke(null, "10.10.1.1",
Arrays.asList("", "10.10.*"));
+ assertTrue(result, "Should skip blank pattern and match valid one");
+ }
+
+ /**
+ * Test lookup method with metadata already present
+ */
+ @Test
+ public void lookupWithExistingMetadataTest() throws Exception {
+ String jsonString =
+
"{\"nodes\":[{\"control\":{\"host\":\"localhost\",\"port\":7091},\"transaction\":{\"host\":\"localhost\",\"port\":8091},\"group\":\"default\",\"role\":\"LEADER\"}],\"storeMode\":\"raft\",\"term\":1}";
+
+ RaftRegistryServiceImpl instance =
RaftRegistryServiceImpl.getInstance();
+
+ // Setup metadata
+ Field metadataField =
RaftRegistryServiceImpl.class.getDeclaredField("METADATA");
+ metadataField.setAccessible(true);
+ Metadata metadata = (Metadata) metadataField.get(null);
+
+ ObjectMapper objectMapper = new ObjectMapper();
+ MetadataResponse metadataResponse = objectMapper.readValue(jsonString,
MetadataResponse.class);
+ metadata.refreshMetadata("default", metadataResponse);
+
+ List<InetSocketAddress> result = instance.lookup("tx");
+
+ assertNotNull(result);
+ assertFalse(result.isEmpty(), "Should return non-empty list when
metadata exists");
Review Comment:
Good catch! Updated.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]