mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
polish #761 update pkg name & maven coordinate for Greenwich
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -34,11 +34,6 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
|
||||
import org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import org.springframework.cloud.alibaba.sentinel.endpoint.SentinelEndpoint;
|
||||
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpRequest;
|
||||
@@ -50,6 +45,11 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.log.LogBase;
|
@@ -14,31 +14,33 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
@@ -14,7 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -22,14 +26,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
|
||||
import com.alibaba.cloud.sentinel.datasource.RuleType;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
@@ -52,7 +53,7 @@ import static org.junit.Assert.assertNull;
|
||||
|
||||
"spring.cloud.sentinel.datasource.ds5.file.file=classpath: param-flow.json",
|
||||
"spring.cloud.sentinel.datasource.ds5.file.data-type=custom",
|
||||
"spring.cloud.sentinel.datasource.ds5.file.converter-class=org.springframework.cloud.alibaba.sentinel.TestConverter",
|
||||
"spring.cloud.sentinel.datasource.ds5.file.converter-class=TestConverter",
|
||||
"spring.cloud.sentinel.datasource.ds5.file.rule-type=param-flow" })
|
||||
public class SentinelDataSourceTests {
|
||||
|
@@ -14,11 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -26,7 +31,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alibaba.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -35,13 +39,10 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
@@ -93,7 +94,7 @@ public class SentinelFeignTests {
|
||||
rule4.setLimitApp("default");
|
||||
rule4.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
|
||||
rule4.setStrategy(RuleConstant.STRATEGY_DIRECT);
|
||||
FlowRuleManager.loadRules(Arrays.asList(rule1, rule2, rule3,rule4));
|
||||
FlowRuleManager.loadRules(Arrays.asList(rule1, rule2, rule3, rule4));
|
||||
}
|
||||
|
||||
@Test
|
@@ -14,14 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
|
||||
import org.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -30,7 +28,10 @@ import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
@@ -85,10 +86,10 @@ public class SentinelRestTemplateTests {
|
||||
new AnnotationConfigApplicationContext(TestConfig9.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalWithoutParam() {
|
||||
new AnnotationConfigApplicationContext(TestConfig10.class);
|
||||
}
|
||||
@Test
|
||||
public void testNormalWithoutParam() {
|
||||
new AnnotationConfigApplicationContext(TestConfig10.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig1 {
|
||||
@@ -225,26 +226,26 @@ public class SentinelRestTemplateTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfig10 {
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
return new SentinelBeanPostProcessor(applicationContext);
|
||||
}
|
||||
@Configuration
|
||||
public static class TestConfig10 {
|
||||
@Bean
|
||||
SentinelBeanPostProcessor sentinelBeanPostProcessor(
|
||||
ApplicationContext applicationContext) {
|
||||
return new SentinelBeanPostProcessor(applicationContext);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
RestTemplate restTemplate2() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
RestTemplate restTemplate2() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExceptionUtil {
|
||||
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
@@ -14,16 +14,17 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sentinel.endpoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
|
||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
|
||||
/**
|
||||
* Test cases for {@link SentinelHealthIndicator}.
|
||||
*
|
||||
* @author cdfive
|
||||
*/
|
||||
public class SentinelHealthIndicatorTests {
|
||||
|
||||
private SentinelHealthIndicator sentinelHealthIndicator;
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
private SentinelProperties sentinelProperties;
|
||||
|
||||
private HeartbeatSender heartbeatSender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
beanFactory = mock(DefaultListableBeanFactory.class);
|
||||
sentinelProperties = mock(SentinelProperties.class);
|
||||
sentinelHealthIndicator = new SentinelHealthIndicator(beanFactory,
|
||||
sentinelProperties);
|
||||
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "");
|
||||
|
||||
heartbeatSender = mock(HeartbeatSender.class);
|
||||
Field heartbeatSenderField = ReflectionUtils
|
||||
.findField(HeartbeatSenderProvider.class, "heartbeatSender");
|
||||
heartbeatSenderField.setAccessible(true);
|
||||
ReflectionUtils.setField(heartbeatSenderField, null, heartbeatSender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelNotEnabled() {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(false);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails().get("enabled")).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardNotConfigured() {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails().get("dashboard")).isEqualTo(Status.UNKNOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardConfiguredSuccess() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardConfiguredFailed() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(false);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails().get("dashboard")).isEqualTo(
|
||||
new Status(Status.DOWN.getCode(), "localhost:8080 can't be connected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDataSourceSuccess() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
|
||||
|
||||
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
|
||||
|
||||
when(beanFactory.getBeansOfType(AbstractDataSource.class))
|
||||
.thenReturn(dataSourceMap);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health
|
||||
.getDetails().get("dataSource");
|
||||
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource"))
|
||||
.isEqualTo(Status.UP);
|
||||
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
|
||||
.isEqualTo(Status.UP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDataSourceFailed() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
|
||||
|
||||
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
|
||||
when(fileDataSource2.loadConfig())
|
||||
.thenThrow(new RuntimeException("fileDataSource2 error"));
|
||||
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
|
||||
|
||||
when(beanFactory.getBeansOfType(AbstractDataSource.class))
|
||||
.thenReturn(dataSourceMap);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health
|
||||
.getDetails().get("dataSource");
|
||||
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource"))
|
||||
.isEqualTo(Status.UP);
|
||||
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
|
||||
.isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error"));
|
||||
}
|
||||
}
|
@@ -1,162 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel.endpoint;
|
||||
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
|
||||
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
|
||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test cases for {@link SentinelHealthIndicator}.
|
||||
*
|
||||
* @author cdfive
|
||||
*/
|
||||
public class SentinelHealthIndicatorTests {
|
||||
|
||||
private SentinelHealthIndicator sentinelHealthIndicator;
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
private SentinelProperties sentinelProperties;
|
||||
|
||||
private HeartbeatSender heartbeatSender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
beanFactory = mock(DefaultListableBeanFactory.class);
|
||||
sentinelProperties = mock(SentinelProperties.class);
|
||||
sentinelHealthIndicator = new SentinelHealthIndicator(beanFactory, sentinelProperties);
|
||||
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "");
|
||||
|
||||
heartbeatSender = mock(HeartbeatSender.class);
|
||||
Field heartbeatSenderField = ReflectionUtils.findField(HeartbeatSenderProvider.class, "heartbeatSender");
|
||||
heartbeatSenderField.setAccessible(true);
|
||||
ReflectionUtils.setField(heartbeatSenderField, null, heartbeatSender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelNotEnabled() {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(false);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails().get("enabled")).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardNotConfigured() {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails().get("dashboard")).isEqualTo(Status.UNKNOWN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardConfiguredSuccess() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDashboardConfiguredFailed() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(false);
|
||||
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails().get("dashboard")).isEqualTo(new Status(Status.DOWN.getCode(), "localhost:8080 can't be connected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDataSourceSuccess() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
|
||||
|
||||
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
|
||||
|
||||
when(beanFactory.getBeansOfType(AbstractDataSource.class)).thenReturn(dataSourceMap);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health.getDetails().get("dataSource");
|
||||
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource")).isEqualTo(Status.UP);
|
||||
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource")).isEqualTo(Status.UP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelDataSourceFailed() throws Exception {
|
||||
when(sentinelProperties.isEnabled()).thenReturn(true);
|
||||
SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "localhost:8080");
|
||||
when(heartbeatSender.sendHeartbeat()).thenReturn(true);
|
||||
|
||||
Map<String, AbstractDataSource> dataSourceMap = new HashMap<>();
|
||||
|
||||
FileRefreshableDataSource fileDataSource1 = mock(FileRefreshableDataSource.class);
|
||||
dataSourceMap.put("ds1-sentinel-file-datasource", fileDataSource1);
|
||||
|
||||
FileRefreshableDataSource fileDataSource2 = mock(FileRefreshableDataSource.class);
|
||||
when(fileDataSource2.loadConfig()).thenThrow(new RuntimeException("fileDataSource2 error"));
|
||||
dataSourceMap.put("ds2-sentinel-file-datasource", fileDataSource2);
|
||||
|
||||
when(beanFactory.getBeansOfType(AbstractDataSource.class)).thenReturn(dataSourceMap);
|
||||
|
||||
Health health = sentinelHealthIndicator.health();
|
||||
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health.getDetails().get("dataSource");
|
||||
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource")).isEqualTo(Status.UP);
|
||||
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource")).isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user