diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandler.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandler.java index 418e0f2..0772761 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandler.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandler.java @@ -28,7 +28,6 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.ConcurrentMap; -import java.util.stream.Collectors; /** * The tars metadata handler. @@ -43,13 +42,12 @@ public void handle(final MetaData metaData) { MetaData metaExist = META_DATA.get(metaData.getPath()); List prxList = ApplicationConfigCache.getInstance() .get(metaData.getPath()).getTarsInvokePrxList(); - boolean exist = prxList.stream().anyMatch(tarsInvokePrx -> tarsInvokePrx.getHost().equals(metaData.getAppName())); + // Proxies are keyed by metadata path; their hosts identify upstream addresses, not applications. + boolean exist = Objects.equals(metaExist, metaData) && CollectionUtils.isNotEmpty(prxList); if (!exist) { ApplicationConfigCache.getInstance().initPrx(metaData); } - if (Objects.isNull(metaExist)) { - META_DATA.put(metaData.getPath(), metaData); - } + META_DATA.put(metaData.getPath(), metaData); } @Override @@ -57,10 +55,7 @@ public void remove(final MetaData metaData) { metaData.updateContextPath(); List prxList = ApplicationConfigCache.getInstance() .get(metaData.getPath()).getTarsInvokePrxList(); - List removePrxList = prxList.stream() - .filter(tarsInvokePrx -> tarsInvokePrx.getHost().equals(metaData.getAppName())) - .collect(Collectors.toList()); - prxList.removeAll(removePrxList); + prxList.clear(); if (CollectionUtils.isEmpty(prxList)) { META_DATA.remove(metaData.getPath()); } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java index 1441959..8d12120 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java @@ -66,7 +66,7 @@ public final class PrxInfoUtil { })); PRIMITIVE_TYPE.put("boolean", new PrimitiveType(boolean.class, o -> { if (o instanceof String) { - return Byte.valueOf((String) o); + return Boolean.valueOf((String) o); } return o; })); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandlerTest.java index 3377e78..0802a05 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandlerTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsMetaDataHandlerTest.java @@ -20,10 +20,21 @@ import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.MetaData; import org.apache.shenyu.common.enums.RpcTypeEnum; +import org.apache.shenyu.plugin.tars.cache.ApplicationConfigCache; +import org.apache.shenyu.plugin.tars.proxy.TarsInvokePrx; +import org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.MockedStatic; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * Test case for {@link org.apache.shenyu.plugin.tars.handler.TarsMetaDataHandler}. @@ -44,6 +55,69 @@ public void setUp() { tarsMetaDataHandler = new TarsMetaDataHandler(); } + @Test + public void testApplicationNameDoesNotNeedToEqualUpstreamHost() { + metaData.setAppName("HelloApp"); + metaData.setPath("/dedup"); + ApplicationConfigCache cache = mock(ApplicationConfigCache.class); + TarsInvokePrxList proxies = new TarsInvokePrxList(); + proxies.getTarsInvokePrxList().add(new TarsInvokePrx(new Object(), "192.0.2.1:1080")); + try (MockedStatic singleton = mockStatic(ApplicationConfigCache.class)) { + singleton.when(ApplicationConfigCache::getInstance).thenReturn(cache); + when(cache.get(metaData.getPath())).thenReturn(proxies); + tarsMetaDataHandler.handle(metaData); + tarsMetaDataHandler.handle(metaData); + verify(cache).initPrx(metaData); + tarsMetaDataHandler.remove(metaData); + assertTrue(proxies.getTarsInvokePrxList().isEmpty()); + } + } + + @Test + public void testChangedDescriptorInitializesOnceForTheSamePath() { + final String path = "/descriptor-update"; + final MetaData initial = new MetaData(); + initial.setPath(path); + initial.setAppName("OriginalApp"); + final MetaData updated = new MetaData(); + updated.setPath(path); + updated.setAppName("ReplacementApp"); + final MetaData replay = new MetaData(); + replay.setPath(path); + replay.setAppName("ReplacementApp"); + + final ApplicationConfigCache cache = mock(ApplicationConfigCache.class); + final TarsInvokePrxList proxies = new TarsInvokePrxList(); + proxies.getTarsInvokePrxList().add(new TarsInvokePrx(new Object(), "192.0.2.1:1080")); + try (MockedStatic singleton = mockStatic(ApplicationConfigCache.class)) { + singleton.when(ApplicationConfigCache::getInstance).thenReturn(cache); + when(cache.get(path)).thenReturn(proxies); + try { + tarsMetaDataHandler.handle(initial); + tarsMetaDataHandler.handle(updated); + tarsMetaDataHandler.handle(replay); + verify(cache).initPrx(initial); + verify(cache).initPrx(updated); + } finally { + tarsMetaDataHandler.remove(replay); + } + } + } + + @Test + public void testEmptyProxyListRetriesInitialization() { + metaData.setPath("/retry"); + ApplicationConfigCache cache = mock(ApplicationConfigCache.class); + try (MockedStatic singleton = mockStatic(ApplicationConfigCache.class)) { + singleton.when(ApplicationConfigCache::getInstance).thenReturn(cache); + when(cache.get(metaData.getPath())).thenReturn(new TarsInvokePrxList()); + tarsMetaDataHandler.handle(metaData); + tarsMetaDataHandler.handle(metaData); + verify(cache, times(2)).initPrx(metaData); + tarsMetaDataHandler.remove(metaData); + } + } + @Test public void testOnSubscribe() { tarsMetaDataHandler.handle(metaData); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtilTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtilTest.java index 431756b..0710187 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtilTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtilTest.java @@ -70,6 +70,21 @@ public void testGetObjectName() { assertEquals("serviceName@tcp -h 127.0.0.1 -p 8080", result); } + @Test + public void testBooleanParametersFromStringsAndJsonBooleans() { + assertArrayEquals(new Object[]{true, false, true, false}, + PrxInfoUtil.getParamArray(new Class[]{boolean.class, boolean.class, boolean.class, boolean.class}, + new String[]{"stringTrue", "stringFalse", "jsonTrue", "jsonFalse"}, + "{\"stringTrue\":\"true\",\"stringFalse\":\"false\",\"jsonTrue\":true,\"jsonFalse\":false}")); + } + + @Test + public void testStringByteAndBooleanParametersKeepTheirTypes() { + assertArrayEquals(new Object[]{Byte.valueOf("1"), Boolean.TRUE}, + PrxInfoUtil.getParamArray(new Class[]{byte.class, boolean.class}, new String[]{"count", "enabled"}, + "{\"count\":\"1\",\"enabled\":\"true\"}")); + } + @Test public void testGetParamArray() { assertArrayEquals(new Object[]{11, Double.valueOf("1.321321312"), Long.valueOf("131231312"), Short.valueOf("11"), Byte.valueOf("0"), false, 'a', 1.321321312F},