Skip to content

Commit

Permalink
test: 添加测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
yukdawn committed Apr 6, 2022
1 parent 6fe63fa commit 7f8e34d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<okio.version>1.17.5</okio.version>
<sentinel.version>1.6.3</sentinel.version>
<jackson.version>2.12.6.1</jackson.version>
<resilience4j-spring.version>1.7.1</resilience4j-spring.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -163,7 +164,7 @@
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring</artifactId>
<version>1.7.1</version>
<version>${resilience4j-spring.version}</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.TimeUnit;

import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeRuleRegister;
import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegradeRuleRegister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
Expand All @@ -13,6 +14,7 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down Expand Up @@ -128,6 +130,13 @@ public RetrofitConfigBean retrofitConfigBean(ObjectProvider<DegradeRuleRegister>
return retrofitConfigBean;
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(com.alibaba.csp.sentinel.SphU.class)
public DegradeRuleRegister sentinelDegradeRuleRegister(){
return new SentinelDegradeRuleRegister(retrofitProperties.getDegrade());
}


@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi4;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -56,6 +58,9 @@ public class RetrofitStarterTest {
@Autowired
private HttpApi3 httpApi3;

@Autowired
private HttpApi4 httpApi4;

private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

Expand Down Expand Up @@ -417,4 +422,25 @@ public void testBoolean() {
System.out.println(apiBoolean);
}

@Test
public void testDegrade() {
for (int i = 0; i < 10; i++) {
try {
MockResponse response = new MockResponse()
.setResponseCode(400)
.addHeader("Content-Type", "application/text; charset=utf-8")
.addHeader("Cache-Control", "no-cache")
.setBody("false")
.setHeadersDelay(5, TimeUnit.SECONDS);
server.enqueue(response);
System.out.println(httpApi4.getPerson(2L).getCode());
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
System.out.println("当前请求轮次: "+ (i+1));
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.lianjiatech.retrofit.spring.boot.test.http;

import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
* @author 陈添明
*/
@RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = HttpApi4.HttpDegradeFallbackFactory.class)
@Degrade(count = 0.5)
public interface HttpApi4 {

/**
* .
*
* @param id .
* @return .
*/
@GET("person")
Result<Person> getPerson(@Query("id") Long id);

@Service
public class HttpDegradeFallbackFactory implements FallbackFactory<HttpApi4> {
Logger log = LoggerFactory.getLogger(HttpDegradeFallbackFactory.class);
/**
* Returns an instance of the fallback appropriate for the given cause
*
* @param cause fallback cause
* @return 实现了retrofit接口的实例。an instance that implements the retrofit interface.
*/
@Override
public HttpApi4 create(Throwable cause) {
log.error("触发熔断了! ", cause.getMessage(), cause);
return id -> {
Result<Person> fallback = new Result<>();
fallback.setCode(100)
.setMsg("fallback")
.setData(new Person());
return fallback;
};
}
}

}

0 comments on commit 7f8e34d

Please sign in to comment.