This commit is contained in:
parent
68b65da5c2
commit
c7e636345f
|
|
@ -151,7 +151,7 @@ public class TransactionExecutor {
|
||||||
|
|
||||||
// a. 判断是否可以执行
|
// a. 判断是否可以执行
|
||||||
if (!instruction.canExecute(context)) {
|
if (!instruction.canExecute(context)) {
|
||||||
String error = "指令不满足执行条件";
|
String error = "指令被拒绝";
|
||||||
log.warn("指令不满足执行条件: instruction={}, sn={}", instruction.getName(), context.getSn());
|
log.warn("指令不满足执行条件: instruction={}, sn={}", instruction.getName(), context.getSn());
|
||||||
InstructionResult result = InstructionResult.failure(error);
|
InstructionResult result = InstructionResult.failure(error);
|
||||||
instruction.onComplete(context, result);
|
instruction.onComplete(context, result);
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试1: 简单成功场景
|
* 测试1: 简单成功场景
|
||||||
* 指令被通过,远程命令成功,方法回调和状态回调都成功
|
* 指令被通过,远程命令成功,方法回调和状态回调都成功
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
@DisplayName("测试1: 简单成功场景 - 指令通过,远程命令成功,回调都成功")
|
@DisplayName("测试1: 简单成功场景 - 指令通过,远程命令成功,回调都成功")
|
||||||
public void testSimpleSuccess() throws ExecutionException, InterruptedException {
|
public void testSimpleSuccess() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -136,7 +136,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试2: 远程命令失败场景
|
* 测试2: 远程命令失败场景
|
||||||
* 指令被通过,但远程命令执行失败(抛出异常)
|
* 指令被通过,但远程命令执行失败(抛出异常)
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(2)
|
@Order(2)
|
||||||
@DisplayName("测试2: 远程命令失败场景 - 指令通过,但远程命令执行失败")
|
@DisplayName("测试2: 远程命令失败场景 - 指令通过,但远程命令执行失败")
|
||||||
public void testRemoteCommandFail() throws ExecutionException, InterruptedException {
|
public void testRemoteCommandFail() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -155,7 +155,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试3: 方法回调超时场景
|
* 测试3: 方法回调超时场景
|
||||||
* 远程命令发送成功,但方法回调超时(不发送回调消息)
|
* 远程命令发送成功,但方法回调超时(不发送回调消息)
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(3)
|
@Order(3)
|
||||||
@DisplayName("测试3: 方法回调超时场景 - 远程命令成功,但方法回调超时")
|
@DisplayName("测试3: 方法回调超时场景 - 远程命令成功,但方法回调超时")
|
||||||
public void testMethodCallbackTimeout() throws ExecutionException, InterruptedException {
|
public void testMethodCallbackTimeout() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -178,7 +178,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试4: 状态回调超时场景
|
* 测试4: 状态回调超时场景
|
||||||
* 方法回调成功,但状态回调超时
|
* 方法回调成功,但状态回调超时
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(4)
|
@Order(4)
|
||||||
@DisplayName("测试4: 状态回调超时场景 - 方法回调成功,但状态回调超时")
|
@DisplayName("测试4: 状态回调超时场景 - 方法回调成功,但状态回调超时")
|
||||||
public void testStateCallbackTimeout() throws ExecutionException, InterruptedException {
|
public void testStateCallbackTimeout() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -212,7 +212,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试5: 成功子命令场景
|
* 测试5: 成功子命令场景
|
||||||
* 主指令成功后执行成功分支的子指令
|
* 主指令成功后执行成功分支的子指令
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(5)
|
@Order(5)
|
||||||
@DisplayName("测试5: 成功子命令场景 - 主指令成功后执行成功分支子指令")
|
@DisplayName("测试5: 成功子命令场景 - 主指令成功后执行成功分支子指令")
|
||||||
public void testSuccessSubCommand() throws ExecutionException, InterruptedException {
|
public void testSuccessSubCommand() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -253,7 +253,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试6: 失败子命令场景
|
* 测试6: 失败子命令场景
|
||||||
* 主指令失败后执行失败分支的补救子指令
|
* 主指令失败后执行失败分支的补救子指令
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(6)
|
@Order(6)
|
||||||
@DisplayName("测试6: 失败子命令场景 - 主指令失败后执行失败分支补救子指令")
|
@DisplayName("测试6: 失败子命令场景 - 主指令失败后执行失败分支补救子指令")
|
||||||
public void testFailureSubCommand() throws ExecutionException, InterruptedException {
|
public void testFailureSubCommand() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -300,7 +300,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试7: Always子命令场景(主指令成功)
|
* 测试7: Always子命令场景(主指令成功)
|
||||||
* 主指令成功,无论如何都执行清理指令
|
* 主指令成功,无论如何都执行清理指令
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(7)
|
@Order(7)
|
||||||
@DisplayName("测试7: Always子命令场景(主指令成功)- 无论成功失败都执行清理")
|
@DisplayName("测试7: Always子命令场景(主指令成功)- 无论成功失败都执行清理")
|
||||||
public void testAlwaysSubCommandWithSuccess() throws ExecutionException, InterruptedException {
|
public void testAlwaysSubCommandWithSuccess() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -341,7 +341,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试8: Always子命令场景(主指令失败)
|
* 测试8: Always子命令场景(主指令失败)
|
||||||
* 主指令失败,但仍然执行清理指令
|
* 主指令失败,但仍然执行清理指令
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(8)
|
@Order(8)
|
||||||
@DisplayName("测试8: Always子命令场景(主指令失败)- 主指令失败仍执行清理")
|
@DisplayName("测试8: Always子命令场景(主指令失败)- 主指令失败仍执行清理")
|
||||||
public void testAlwaysSubCommandWithFailure() throws ExecutionException, InterruptedException {
|
public void testAlwaysSubCommandWithFailure() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -382,7 +382,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试9: 复杂指令树场景(成功路径)
|
* 测试9: 复杂指令树场景(成功路径)
|
||||||
* 测试复杂的多层嵌套指令树,走成功分支
|
* 测试复杂的多层嵌套指令树,走成功分支
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(9)
|
@Order(9)
|
||||||
@DisplayName("测试9: 复杂指令树场景(成功路径)- 多层嵌套,走成功分支")
|
@DisplayName("测试9: 复杂指令树场景(成功路径)- 多层嵌套,走成功分支")
|
||||||
public void testComplexInstructionTreeSuccess() throws ExecutionException, InterruptedException {
|
public void testComplexInstructionTreeSuccess() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -500,7 +500,7 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
* 测试12: 指令被通过场景
|
* 测试12: 指令被通过场景
|
||||||
* canExecute返回true,指令可以执行
|
* canExecute返回true,指令可以执行
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(12)
|
@Order(12)
|
||||||
@DisplayName("测试12: 指令被通过场景 - canExecute返回true")
|
@DisplayName("测试12: 指令被通过场景 - canExecute返回true")
|
||||||
public void testCommandAccepted() throws ExecutionException, InterruptedException {
|
public void testCommandAccepted() throws ExecutionException, InterruptedException {
|
||||||
|
|
@ -521,60 +521,13 @@ public class ComprehensiveDrcStateMachineTest {
|
||||||
log.info(">>> 测试通过:指令被接受并执行");
|
log.info(">>> 测试通过:指令被接受并执行");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试13: tid/bid 匹配成功场景
|
|
||||||
* 回调消息中的 tid 和 bid 与指令执行时生成的值匹配
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
@Order(13)
|
|
||||||
@DisplayName("测试13: tid/bid匹配成功场景 - 回调消息tid/bid匹配")
|
|
||||||
public void testTidBidMatchSuccess() throws ExecutionException, InterruptedException {
|
|
||||||
log.info(">>> 场景:回调消息中的 tid 和 bid 与指令执行时生成的值匹配");
|
|
||||||
|
|
||||||
CompletableFuture<CommandResult> future =
|
|
||||||
machineCommandManager.executeCommand(currentTestSn, CommandType.EXIT_DRC_MODE, new HashMap<>());
|
|
||||||
|
|
||||||
// 需要获取生成的 tid 和 bid
|
|
||||||
// 注意:这里我们需要从 future 或其他方式获取 context 中的 tid/bid
|
|
||||||
// 为了测试,我们延迟一下,让指令开始执行,然后模拟正确的 tid/bid 响应
|
|
||||||
|
|
||||||
scheduler.schedule(() -> {
|
|
||||||
try {
|
|
||||||
// 在实际场景中,tid 和 bid 应该从指令执行上下文中获取
|
|
||||||
// 这里为了演示,我们假设可以通过某种方式获取到
|
|
||||||
// 实际使用时,设备会在收到命令后,将 tid/bid 原样返回
|
|
||||||
|
|
||||||
Thread.sleep(100);
|
|
||||||
|
|
||||||
// 模拟方法回调 - 包含正确的 tid 和 bid
|
|
||||||
// 注意:在真实场景中,这些值应该从命令中获取并原样返回
|
|
||||||
String response = "{\"tid\":\"test-tid-123\",\"bid\":\"test-bid-456\",\"data\":{\"result\":\"success\"}}";
|
|
||||||
mqttCallbackRegistry.handleMessage("test/" + currentTestSn + "/response", response);
|
|
||||||
log.info(">>> 模拟发送方法回调(包含tid/bid): {}", response);
|
|
||||||
|
|
||||||
Thread.sleep(100);
|
|
||||||
|
|
||||||
// 模拟状态回调 - 包含正确的 tid 和 bid
|
|
||||||
response = "{\"tid\":\"test-tid-123\",\"bid\":\"test-bid-456\",\"status\":\"completed\"}";
|
|
||||||
mqttCallbackRegistry.handleMessage("test/" + currentTestSn + "/state", response);
|
|
||||||
log.info(">>> 模拟发送状态回调(包含tid/bid): {}", response);
|
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}, 200, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
CommandResult result = future.get();
|
|
||||||
assertTrue(result.isSuccess(), "指令应该执行成功(tid/bid匹配)");
|
|
||||||
log.info(">>> 测试通过:tid/bid 匹配成功");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试14: tid/bid 不匹配场景
|
* 测试14: tid/bid 不匹配场景
|
||||||
* 回调消息中的 tid 或 bid 与指令执行时生成的值不匹配,应该超时
|
* 回调消息中的 tid 或 bid 与指令执行时生成的值不匹配,应该超时
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
@Order(14)
|
@Order(13)
|
||||||
@DisplayName("测试14: tid/bid不匹配场景 - 回调消息tid/bid不匹配导致超时")
|
@DisplayName("测试14: tid/bid不匹配场景 - 回调消息tid/bid不匹配导致超时")
|
||||||
public void testTidBidMismatch() throws ExecutionException, InterruptedException {
|
public void testTidBidMismatch() throws ExecutionException, InterruptedException {
|
||||||
log.info(">>> 场景:回调消息中的 tid 或 bid 与指令执行时生成的值不匹配");
|
log.info(">>> 场景:回调消息中的 tid 或 bid 与指令执行时生成的值不匹配");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue