Skip to content

Commit

Permalink
Return all subscriber when ip or port is null. (#13008)
Browse files Browse the repository at this point in the history
* [ISSUE #12940] Add a new param to support return all subscriber when ip or port is null.

* [ISSUE #12940] Add a new param to support return all subscriber when ip or port is null.

* [ISSUE #12940] add some unit tests.
  • Loading branch information
zhouchunhai authored Jan 8, 2025
1 parent 595bdc9 commit 3796395
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ public Result<List<ObjectNode>> getSubscribeClientList(
for (String clientId : allClientsSubscribeService) {
Client client = clientManager.getClient(clientId);
Subscriber subscriber = client.getSubscriber(service);
if (!Objects.equals(subscriber.getIp(), ip) || !Objects.equals(port, subscriber.getPort())) {
boolean ipMatch = Objects.isNull(ip) || Objects.equals(ip, subscriber.getIp());
boolean portMatch = Objects.isNull(port) || Objects.equals(port, subscriber.getPort());
if (!ipMatch || !portMatch) {
continue;
}
ObjectNode item = JacksonUtils.createEmptyJsonNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -47,6 +48,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -142,4 +144,30 @@ void testGetPublishedClientList() throws Exception {
.param("groupName", baseTestKey).param("serviceName", baseTestKey).param("ip", "127.0.0.1").param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
}

@Test
void testGetSubscribeClientList() throws Exception {
String baseTestKey = "nacos-getSubScribedClientList-test";
// ip port match
Service service = Service.newService(baseTestKey, baseTestKey, baseTestKey);
when(clientServiceIndexesManager.getAllClientsSubscribeService(service)).thenReturn(Arrays.asList("test"));
when(clientManager.getClient("test")).thenReturn(ipPortBasedClient);
Subscriber subscriber = mock(Subscriber.class);
when(subscriber.getIp()).thenReturn("127.0.0.1");
when(subscriber.getPort()).thenReturn(8848);
ipPortBasedClient.addServiceSubscriber(service, subscriber);
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
.param("ip", "127.0.0.1").param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
// ip port not match
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
.param("ip", "127.0.0.1").param("port", "8849");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(0));
// ip port is null
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey);
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
}
}

0 comments on commit 3796395

Please sign in to comment.