Skip to content

Commit 3796395

Browse files
authored
Return all subscriber when ip or port is null. (#13008)
* [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.
1 parent 595bdc9 commit 3796395

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ public Result<List<ObjectNode>> getSubscribeClientList(
269269
for (String clientId : allClientsSubscribeService) {
270270
Client client = clientManager.getClient(clientId);
271271
Subscriber subscriber = client.getSubscriber(service);
272-
if (!Objects.equals(subscriber.getIp(), ip) || !Objects.equals(port, subscriber.getPort())) {
272+
boolean ipMatch = Objects.isNull(ip) || Objects.equals(ip, subscriber.getIp());
273+
boolean portMatch = Objects.isNull(port) || Objects.equals(port, subscriber.getPort());
274+
if (!ipMatch || !portMatch) {
273275
continue;
274276
}
275277
ObjectNode item = JacksonUtils.createEmptyJsonNode();

naming/src/test/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2Test.java

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
2828
import com.alibaba.nacos.naming.core.v2.pojo.Service;
2929
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
30+
import com.alibaba.nacos.naming.pojo.Subscriber;
3031
import com.fasterxml.jackson.databind.JsonNode;
3132
import org.junit.jupiter.api.BeforeEach;
3233
import org.junit.jupiter.api.Test;
@@ -47,6 +48,7 @@
4748

4849
import static org.junit.jupiter.api.Assertions.assertEquals;
4950
import static org.mockito.ArgumentMatchers.anyString;
51+
import static org.mockito.Mockito.mock;
5052
import static org.mockito.Mockito.when;
5153

5254
@ExtendWith(MockitoExtension.class)
@@ -142,4 +144,30 @@ void testGetPublishedClientList() throws Exception {
142144
.param("groupName", baseTestKey).param("serviceName", baseTestKey).param("ip", "127.0.0.1").param("port", "8848");
143145
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
144146
}
147+
148+
@Test
149+
void testGetSubscribeClientList() throws Exception {
150+
String baseTestKey = "nacos-getSubScribedClientList-test";
151+
// ip port match
152+
Service service = Service.newService(baseTestKey, baseTestKey, baseTestKey);
153+
when(clientServiceIndexesManager.getAllClientsSubscribeService(service)).thenReturn(Arrays.asList("test"));
154+
when(clientManager.getClient("test")).thenReturn(ipPortBasedClient);
155+
Subscriber subscriber = mock(Subscriber.class);
156+
when(subscriber.getIp()).thenReturn("127.0.0.1");
157+
when(subscriber.getPort()).thenReturn(8848);
158+
ipPortBasedClient.addServiceSubscriber(service, subscriber);
159+
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
160+
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
161+
.param("ip", "127.0.0.1").param("port", "8848");
162+
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
163+
// ip port not match
164+
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
165+
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
166+
.param("ip", "127.0.0.1").param("port", "8849");
167+
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(0));
168+
// ip port is null
169+
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
170+
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey);
171+
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
172+
}
145173
}

0 commit comments

Comments
 (0)