Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,24 +42,20 @@ public void handle(final MetaData metaData) {
MetaData metaExist = META_DATA.get(metaData.getPath());
List<TarsInvokePrx> 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
public void remove(final MetaData metaData) {
metaData.updateContextPath();
List<TarsInvokePrx> prxList = ApplicationConfigCache.getInstance()
.get(metaData.getPath()).getTarsInvokePrxList();
List<TarsInvokePrx> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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<ApplicationConfigCache> 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<ApplicationConfigCache> 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<ApplicationConfigCache> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Loading