新增重试测试用例

This commit is contained in:
byteblogs168 2024-03-03 12:42:04 +08:00
parent 3af97e6a7d
commit f29dae98c4
10 changed files with 396 additions and 11 deletions

View File

@ -39,4 +39,79 @@ public class LocalAndRemoteRetryController {
@RequestParam("params") String params) {
localRemoteService.remoteRetryWithLocalRemote(params);
}
@GetMapping("/localRetryWithTwoRetryMethod")
/**
*
* 方法内部存在两个串行的方法retryMethod1retryMethod1 如下所属
* public boolean localRetryWithTwoRetryMethod(final String params) {
* retryHandler.retryMethod1(params);
* retryHandler.retryMethod1(params);
* return true;
* }
* params: 1 => 则retryMethod1触发重试
* params: 2 => 则retryMethod2触发重试
* params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试
*
*/
@Operation(
summary = "N个串行执行的方法触发重试的场景",
description = "方法内部存在两个串行的方法retryMethod1、retryMethod1\n" +
"params: 1 => 则retryMethod1触发重试\n" +
"params: 2 => 则retryMethod2触发重试\n" +
"params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试"
)
public boolean localRetryWithTwoRetryMethod(@RequestParam("params") String params) {
return localRemoteService.localRetryWithTwoRetryMethod(params);
}
/**
* 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED,
* 只执行入口方法重试
*/
@GetMapping("/localRetryWithPropagationRequired")
@Operation(
description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试",
summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试"
)
public boolean localRetryWithPropagationRequired(@RequestParam("params") String params) {
return localRemoteService.localRetryWithPropagationRequired(params);
}
/**
* 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,
* 外部和内部方法都触发重试
*/
@GetMapping("/localRetryWithPropagationRequiredNew")
@Operation(
description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试",
summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试"
)
public boolean localRetryWithPropagationRequiredNew(@RequestParam("params") String params) {
return localRemoteService.localRetryWithPropagationRequiredNew(params);
}
/**
* 内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试
*/
@GetMapping("/localRetryWithTryCatch1")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试"
)
public boolean localRetryWithTryCatch1(@RequestParam("params") String params) {
return localRemoteService.localRetryWithTryCatch1(params);
}
/**
* 内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试
*/
@GetMapping("/localRetryWithTryCatch2")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试"
)
public boolean localRetryWithTryCatch2(@RequestParam("params") String params) {
return localRemoteService.localRetryWithTryCatch2(params);
}
}

View File

@ -168,5 +168,78 @@ public class RemoteRetryController {
remoteRetryService.remoteRetryWithBizNo(orderVo);
}
@GetMapping("/localRetryWithTwoRetryMethod")
/**
*
* 方法内部存在两个串行的方法retryMethod1retryMethod1 如下所属
* public boolean localRetryWithTwoRetryMethod(final String params) {
* retryHandler.retryMethod1(params);
* retryHandler.retryMethod1(params);
* return true;
* }
* params: 1 => 则retryMethod1触发重试
* params: 2 => 则retryMethod2触发重试
* params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试
*
*/
@Operation(
summary = "N个串行执行的方法触发重试的场景",
description = "方法内部存在两个串行的方法retryMethod1、retryMethod1\n" +
"params: 1 => 则retryMethod1触发重试\n" +
"params: 2 => 则retryMethod2触发重试\n" +
"params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试"
)
public boolean localRetryWithTwoRetryMethod(@RequestParam("params") String params) {
return remoteRetryService.localRetryWithTwoRetryMethod(params);
}
/**
* 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED,
* 只执行入口方法重试
*/
@GetMapping("/localRetryWithPropagationRequired")
@Operation(
description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试",
summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试"
)
public boolean localRetryWithPropagationRequired(@RequestParam("params") String params) {
return remoteRetryService.localRetryWithPropagationRequired(params);
}
/**
* 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,
* 外部和内部方法都触发重试
*/
@GetMapping("/localRetryWithPropagationRequiredNew")
@Operation(
description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试",
summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试"
)
public boolean localRetryWithPropagationRequiredNew(@RequestParam("params") String params) {
return remoteRetryService.localRetryWithPropagationRequiredNew(params);
}
/**
* 内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试
*/
@GetMapping("/localRetryWithTryCatch1")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试"
)
public boolean localRetryWithTryCatch1(@RequestParam("params") String params) {
return remoteRetryService.localRetryWithTryCatch1(params);
}
/**
* 内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试
*/
@GetMapping("/localRetryWithTryCatch2")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试"
)
public boolean localRetryWithTryCatch2(@RequestParam("params") String params) {
return remoteRetryService.localRetryWithTryCatch2(params);
}
}

View File

@ -0,0 +1,59 @@
package com.example.easy.retry.handler;
import com.aizuda.easy.retry.client.core.annotation.Propagation;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author: xiaowoniu
* @date : 2024-02-05
* @since : 3.1.0
*/
@Component
public class LocalAndRemoteRetryHandler {
@Retryable(scene = "localRetryWithTwoRetryMethod1", retryStrategy = RetryType.LOCAL_REMOTE)
public void retryMethod1(String params) {
System.out.println("localRetryWithTwoRetryMethod1");
if (params.equals("1")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
int random = new Random().nextInt(10);
if (random % 3 == 0) {
System.out.println("localRetryWithTwoRetryMethod1 is success");
return;
}
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetryWithTwoRetryMethod2", retryStrategy = RetryType.LOCAL_REMOTE)
public void retryMethod2(String params) {
System.out.println("localRetryWithTwoRetryMethod2");
if (params.equals("2")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetryWithRequires", retryStrategy = RetryType.LOCAL_REMOTE)
public void localRetryWithRequires(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
@Retryable(scene = "localRetryWithRequiresNew", retryStrategy = RetryType.LOCAL_REMOTE, propagation = Propagation.REQUIRES_NEW)
public void localRetryWithRequiresNew(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
}

View File

@ -3,7 +3,6 @@ package com.example.easy.retry.handler;
import com.aizuda.easy.retry.client.core.annotation.Propagation;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import com.example.easy.retry.vo.OrderVo;
import org.springframework.stereotype.Component;
import java.util.Random;
@ -14,7 +13,7 @@ import java.util.Random;
* @since : 3.1.0
*/
@Component
public class RetryHandler {
public class OnlyLocalRetryHandler {
@Retryable(scene = "localRetryWithTwoRetryMethod1", retryStrategy = RetryType.ONLY_LOCAL)
public void retryMethod1(String params) {

View File

@ -0,0 +1,59 @@
package com.example.easy.retry.handler;
import com.aizuda.easy.retry.client.core.annotation.Propagation;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author: xiaowoniu
* @date : 2024-02-05
* @since : 3.1.0
*/
@Component
public class OnlyRemoteRetryHandler {
@Retryable(scene = "localRetryWithTwoRetryMethod1", retryStrategy = RetryType.ONLY_LOCAL)
public void retryMethod1(String params) {
System.out.println("localRetryWithTwoRetryMethod1");
if (params.equals("1")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
int random = new Random().nextInt(10);
if (random % 3 == 0) {
System.out.println("localRetryWithTwoRetryMethod1 is success");
return;
}
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetryWithTwoRetryMethod2", retryStrategy = RetryType.ONLY_LOCAL)
public void retryMethod2(String params) {
System.out.println("localRetryWithTwoRetryMethod2");
if (params.equals("2")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetryWithRequires", retryStrategy = RetryType.ONLY_LOCAL)
public void localRetryWithRequires(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
@Retryable(scene = "localRetryWithRequiresNew", retryStrategy = RetryType.ONLY_LOCAL, propagation = Propagation.REQUIRES_NEW)
public void localRetryWithRequiresNew(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
}

View File

@ -9,4 +9,14 @@ public interface LocalRemoteService {
void localRemote();
String remoteRetryWithLocalRemote(String requestId);
boolean localRetryWithPropagationRequired(String params);
boolean localRetryWithPropagationRequiredNew(String params);
boolean localRetryWithTryCatch1(String params);
boolean localRetryWithTryCatch2(String params);
boolean localRetryWithTwoRetryMethod(String params);
}

View File

@ -24,4 +24,14 @@ public interface RemoteRetryService {
boolean remoteRetryWithCompleteCallback(String scene, OrderVo orderVo);
boolean remoteRetryWithBizNo(OrderVo orderVo);
boolean localRetryWithPropagationRequired(String params);
boolean localRetryWithPropagationRequiredNew(String params);
boolean localRetryWithTryCatch1(String params);
boolean localRetryWithTryCatch2(String params);
boolean localRetryWithTwoRetryMethod(String params);
}

View File

@ -2,7 +2,10 @@ package com.example.easy.retry.service.impl;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import com.example.easy.retry.handler.LocalAndRemoteRetryHandler;
import com.example.easy.retry.service.LocalRemoteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@ -15,7 +18,10 @@ import java.util.concurrent.TimeUnit;
* @since 2.1.0
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class LocalRemoteServiceImpl implements LocalRemoteService {
private final LocalAndRemoteRetryHandler localAndRemoteRetryHandler;
@Override
@Retryable(scene = "localRemote", retryStrategy = RetryType.LOCAL_REMOTE)
@ -36,4 +42,50 @@ public class LocalRemoteServiceImpl implements LocalRemoteService {
return requestId;
}
@Override
@Retryable(scene = "localRetryWithPropagationRequired", retryStrategy = RetryType.LOCAL_REMOTE)
public boolean localRetryWithPropagationRequired(String params) {
localAndRemoteRetryHandler.localRetryWithRequires(params);
return false;
}
@Override
@Retryable(scene = "localRetryWithPropagationRequiredNew", retryStrategy = RetryType.LOCAL_REMOTE)
public boolean localRetryWithPropagationRequiredNew(String params) {
localAndRemoteRetryHandler.localRetryWithRequiresNew(params);
return false;
}
@Override
@Retryable(scene = "localRetryWithTryCatch1", retryStrategy = RetryType.LOCAL_REMOTE)
public boolean localRetryWithTryCatch1(String params) {
try {
// 内部方法需要触发重试
localAndRemoteRetryHandler.localRetryWithRequiresNew(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false;
}
@Override
@Retryable(scene = "localRetryWithTryCatch2", retryStrategy = RetryType.LOCAL_REMOTE)
public boolean localRetryWithTryCatch2(String params) {
// 由于传播机制为{REQUIRED}异常被捕获,所以内部不会触发重试
try {
localAndRemoteRetryHandler.localRetryWithRequires(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false;
}
@Override
public boolean localRetryWithTwoRetryMethod(String params) {
localAndRemoteRetryHandler.retryMethod1(params);
localAndRemoteRetryHandler.retryMethod2(params);
return false;
}
}

View File

@ -2,7 +2,7 @@ package com.example.easy.retry.service.impl;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import com.example.easy.retry.customized.OrderRetryMethod;
import com.example.easy.retry.handler.RetryHandler;
import com.example.easy.retry.handler.OnlyLocalRetryHandler;
import com.example.easy.retry.service.LocalRetryService;
import com.example.easy.retry.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
@ -21,7 +21,7 @@ import com.example.easy.retry.exception.ParamException;
public class LocalRetryServiceImpl implements LocalRetryService {
@Autowired
private RetryHandler retryHandler;
private OnlyLocalRetryHandler onlyLocalRetryHandler;
/**
* 入门案例
@ -40,8 +40,8 @@ public class LocalRetryServiceImpl implements LocalRetryService {
@Override
public boolean localRetryWithTwoRetryMethod(final String params) {
retryHandler.retryMethod1(params);
retryHandler.retryMethod2(params);
onlyLocalRetryHandler.retryMethod1(params);
onlyLocalRetryHandler.retryMethod2(params);
return true;
}
@ -120,14 +120,14 @@ public class LocalRetryServiceImpl implements LocalRetryService {
@Override
@Retryable(scene = "localRetryWithPropagationRequired", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithPropagationRequired(String params) {
retryHandler.localRetry(params);
onlyLocalRetryHandler.localRetry(params);
return false;
}
@Override
@Retryable(scene = "localRetryWithPropagationRequiredNew", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithPropagationRequiredNew(final String params) {
retryHandler.localRetryWithRequiresNew(params);
onlyLocalRetryHandler.localRetryWithRequiresNew(params);
return false;
}
@ -136,7 +136,7 @@ public class LocalRetryServiceImpl implements LocalRetryService {
public boolean localRetryWithTryCatch1(String params) {
try {
// 内部方法需要触发重试
retryHandler.localRetryWithRequiresNew(params);
onlyLocalRetryHandler.localRetryWithRequiresNew(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
@ -148,7 +148,7 @@ public class LocalRetryServiceImpl implements LocalRetryService {
public boolean localRetryWithTryCatch2(String params) {
// 由于传播机制为{REQUIRED}异常被捕获,所以内部不会触发重试
try {
retryHandler.localRetry(params);
onlyLocalRetryHandler.localRetry(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}

View File

@ -6,7 +6,10 @@ import java.util.concurrent.TimeUnit;
import cn.hutool.core.lang.Assert;
import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot;
import com.aizuda.easy.retry.common.core.enums.RetryStatusEnum;
import com.example.easy.retry.handler.OnlyRemoteRetryHandler;
import com.example.easy.retry.service.RemoteRetryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
@ -22,8 +25,10 @@ import com.example.easy.retry.vo.OrderVo;
* easy-retry中的远程重试
*/
@Component
@Slf4j
@RequiredArgsConstructor
public class RemoteRetryServiceImpl implements RemoteRetryService {
private final OnlyRemoteRetryHandler onlyRemoteRetryHandler;
/**
* 定义一个最基本的远程重试方法
*/
@ -91,6 +96,49 @@ public class RemoteRetryServiceImpl implements RemoteRetryService {
throw new NullPointerException();
}
@Override
@Retryable(scene = "localRetryWithPropagationRequired", retryStrategy = RetryType.LOCAL_REMOTE)
public boolean localRetryWithPropagationRequired(String params) {
onlyRemoteRetryHandler.localRetryWithRequires(params);
return false;
}
@Override
public boolean localRetryWithPropagationRequiredNew(String params) {
onlyRemoteRetryHandler.localRetryWithRequiresNew(params);
return false;
}
@Override
public boolean localRetryWithTryCatch1(String params) {
try {
// 内部方法需要触发重试
onlyRemoteRetryHandler.localRetryWithRequiresNew(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false;
}
@Override
public boolean localRetryWithTryCatch2(String params) {
// 由于传播机制为{REQUIRED}异常被捕获,所以内部不会触发重试
try {
onlyRemoteRetryHandler.localRetryWithRequires(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false;
}
@Override
public boolean localRetryWithTwoRetryMethod(String params) {
onlyRemoteRetryHandler.retryMethod1(params);
onlyRemoteRetryHandler.retryMethod2(params);
return false;
}
/**
* 使用自定义的retryCompleteCallback类 OrderCompleteCallback
*/