Add condition to match serviceKey. (#13869)

This commit is contained in:
kaze 2024-03-12 17:26:18 +08:00 committed by GitHub
parent b2c66a6d99
commit 0553d70899
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 4 deletions

View File

@ -58,7 +58,8 @@ public class ChangeTelnet implements BaseCommand {
for (Exporter<?> exporter : dubboProtocol.getExporters()) {
if (message.equals(exporter.getInvoker().getInterface().getSimpleName())
|| message.equals(exporter.getInvoker().getInterface().getName())
|| message.equals(exporter.getInvoker().getUrl().getPath())) {
|| message.equals(exporter.getInvoker().getUrl().getPath())
|| message.equals(exporter.getInvoker().getUrl().getServiceKey())) {
found = true;
break;
}

View File

@ -85,7 +85,8 @@ public class CountTelnet implements BaseCommand {
for (Exporter<?> exporter : dubboProtocol.getExporters()) {
if (service.equals(exporter.getInvoker().getInterface().getSimpleName())
|| service.equals(exporter.getInvoker().getInterface().getName())
|| service.equals(exporter.getInvoker().getUrl().getPath())) {
|| service.equals(exporter.getInvoker().getUrl().getPath())
|| service.equals(exporter.getInvoker().getUrl().getServiceKey())) {
invoker = exporter.getInvoker();
break;
}

View File

@ -71,7 +71,7 @@ class ChangeTelnetTest {
mockChannel.attr(ChangeTelnet.SERVICE_KEY).set("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService");
given(mockCommandContext.getRemote()).willReturn(mockChannel);
given(mockInvoker.getInterface()).willReturn(DemoService.class);
given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20884/demo"));
given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20884/demo?group=g&version=1.0.0"));
}
@AfterEach
@ -110,6 +110,15 @@ class ChangeTelnetTest {
assertEquals("Used the demo as default.\r\nYou can cancel default service by command: cd /", result);
}
@Test
void testChangeServiceKey() {
ExtensionLoader.getExtensionLoader(Protocol.class)
.getExtension(DubboProtocol.NAME)
.export(mockInvoker);
String result = change.execute(mockCommandContext, new String[] {"g/demo:1.0.0"});
assertEquals("Used the g/demo:1.0.0 as default.\r\nYou can cancel default service by command: cd /", result);
}
@Test
void testChangeMessageNull() {
String result = change.execute(mockCommandContext, null);

View File

@ -52,7 +52,7 @@ class CountTelnetTest {
private CommandContext mockCommandContext;
private CountDownLatch latch;
private final URL url = URL.valueOf("dubbo://127.0.0.1:20884/demo");
private final URL url = URL.valueOf("dubbo://127.0.0.1:20884/demo?group=g&version=1.0.0");
@BeforeEach
public void setUp() {
@ -70,12 +70,14 @@ class CountTelnetTest {
public void tearDown() {
FrameworkModel.destroyAll();
mockChannel.close();
RpcStatus.removeStatus(url);
reset(mockInvoker, mockCommandContext);
}
@Test
void test() throws Exception {
String methodName = "sayHello";
RpcStatus.removeStatus(url, methodName);
String[] args = new String[] {"org.apache.dubbo.qos.legacy.service.DemoService", "sayHello", "1"};
ExtensionLoader.getExtensionLoader(Protocol.class)
@ -94,6 +96,28 @@ class CountTelnetTest {
assertThat(sb.toString(), containsString(buildTable(methodName, 10, 10, "1", "0", "0")));
}
@Test
void testCountByServiceKey() throws Exception {
String methodName = "sayHello";
RpcStatus.removeStatus(url, methodName);
String[] args = new String[] {"g/demo:1.0.0", "sayHello", "1"};
ExtensionLoader.getExtensionLoader(Protocol.class)
.getExtension(DubboProtocol.NAME)
.export(mockInvoker);
RpcStatus.beginCount(url, methodName);
RpcStatus.endCount(url, methodName, 10L, true);
count.execute(mockCommandContext, args);
latch.await();
StringBuilder sb = new StringBuilder();
for (Object o : mockChannel.getReceivedObjects()) {
sb.append(o.toString());
}
assertThat(sb.toString(), containsString(buildTable(methodName, 10, 10, "1", "0", "0")));
}
public static String buildTable(
String methodName, long averageElapsed, long maxElapsed, String total, String failed, String active) {
List<String> header = new LinkedList<>();