use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector in project spring-cloud-tencent by Tencent.
the class PolarisCachingSpringLoadBalanceFactoryTest method test.
@Test
public void test() {
PolarisCachingSpringLoadBalanceFactory polarisCachingSpringLoadBalanceFactory = new PolarisCachingSpringLoadBalanceFactory(factory, null);
DefaultClientConfigImpl config1 = new DefaultClientConfigImpl();
config1.loadDefaultValues();
config1.setClientName(service1);
DefaultClientConfigImpl config2 = new DefaultClientConfigImpl();
config2.loadDefaultValues();
config2.setClientName(service2);
when(factory.getClientConfig(service1)).thenReturn(config1);
when(factory.getClientConfig(service2)).thenReturn(config2);
ILoadBalancer loadBalancer = new SimpleLoadBalancer();
when(factory.getLoadBalancer(service1)).thenReturn(loadBalancer);
when(factory.getLoadBalancer(service2)).thenReturn(loadBalancer);
ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
when(factory.getInstance(service1, ServerIntrospector.class)).thenReturn(serverIntrospector);
when(factory.getInstance(service2, ServerIntrospector.class)).thenReturn(serverIntrospector);
// load balancer for service1
FeignLoadBalancer feignLoadBalancer = polarisCachingSpringLoadBalanceFactory.create(service1);
Assert.assertNotNull(feignLoadBalancer);
verify(factory).getClientConfig(service1);
verify(factory, times(0)).getClientConfig(service2);
verify(factory).getLoadBalancer(service1);
verify(factory, times(0)).getLoadBalancer(service2);
verify(factory).getInstance(service1, ServerIntrospector.class);
verify(factory, times(0)).getInstance(service2, ServerIntrospector.class);
Assert.assertEquals(loadBalancer, feignLoadBalancer.getLoadBalancer());
Assert.assertEquals(service1, feignLoadBalancer.getClientName());
// load balancer for service2
FeignLoadBalancer feignLoadBalancer2 = polarisCachingSpringLoadBalanceFactory.create(service2);
// load balancer for service1 again
feignLoadBalancer = polarisCachingSpringLoadBalanceFactory.create(service1);
Assert.assertNotNull(feignLoadBalancer);
verify(factory).getClientConfig(service1);
verify(factory).getClientConfig(service2);
verify(factory).getLoadBalancer(service1);
verify(factory).getLoadBalancer(service2);
verify(factory).getInstance(service1, ServerIntrospector.class);
verify(factory).getInstance(service2, ServerIntrospector.class);
Assert.assertEquals(loadBalancer, feignLoadBalancer2.getLoadBalancer());
Assert.assertEquals(service2, feignLoadBalancer2.getClientName());
}
use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector in project spring-cloud-tencent by Tencent.
the class PolarisFeignLoadBalancerTest method testHasRouterContext.
@Test
public void testHasRouterContext() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadDefaultValues();
ILoadBalancer loadBalancer = new SimpleLoadBalancer();
ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
Map<String, String> labels = new HashMap<>();
labels.put("k1", "v1");
labels.put("k2", "v2");
List<String> headerValues = new ArrayList<>();
headerValues.add(JacksonUtils.serialize2Json(labels));
Map<String, Collection<String>> headers = new HashMap<>();
headers.put(RouterConstants.ROUTER_LABEL_HEADER, headerValues);
// mock ApplicationContextAwareUtils#getProperties
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
// mock MetadataContextHolder#get
try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
Assert.assertNotNull(routerContext);
Map<String, String> routerLabels = routerContext.getLabels(PolarisRouterContext.RULE_ROUTER_LABELS);
Assert.assertNotNull(routerLabels);
Assert.assertEquals("v1", routerLabels.get("k1"));
Assert.assertEquals("v2", routerLabels.get("k2"));
Assert.assertNull(routerLabels.get("k3"));
}
}
}
use of org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector in project spring-cloud-tencent by Tencent.
the class PolarisFeignLoadBalancerTest method testHasNoneRouterContext.
@Test
public void testHasNoneRouterContext() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.loadDefaultValues();
ILoadBalancer loadBalancer = new SimpleLoadBalancer();
ServerIntrospector serverIntrospector = new DefaultServerIntrospector();
PolarisFeignLoadBalancer polarisFeignLoadBalancer = new PolarisFeignLoadBalancer(loadBalancer, config, serverIntrospector);
Map<String, Collection<String>> headers = new HashMap<>();
// mock ApplicationContextAwareUtils#getProperties
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class)) {
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())).thenReturn("unit-test");
MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
// mock MetadataContextHolder#get
try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
PolarisRouterContext routerContext = polarisFeignLoadBalancer.buildRouterContext(headers);
Assert.assertNull(routerContext);
}
}
}
Aggregations