This commit is contained in:
孙小云 2025-12-18 20:38:06 +08:00
parent 68b65da5c2
commit c7e636345f
2 changed files with 13 additions and 60 deletions

View File

@ -151,7 +151,7 @@ public class TransactionExecutor {
// a. 判断是否可以执行
if (!instruction.canExecute(context)) {
String error = "指令不满足执行条件";
String error = "指令被拒绝";
log.warn("指令不满足执行条件: instruction={}, sn={}", instruction.getName(), context.getSn());
InstructionResult result = InstructionResult.failure(error);
instruction.onComplete(context, result);

View File

@ -98,7 +98,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试1: 简单成功场景
* 指令被通过远程命令成功方法回调和状态回调都成功
*/
// @Test
@Test
@Order(1)
@DisplayName("测试1: 简单成功场景 - 指令通过,远程命令成功,回调都成功")
public void testSimpleSuccess() throws ExecutionException, InterruptedException {
@ -136,7 +136,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试2: 远程命令失败场景
* 指令被通过但远程命令执行失败抛出异常
*/
// @Test
@Test
@Order(2)
@DisplayName("测试2: 远程命令失败场景 - 指令通过,但远程命令执行失败")
public void testRemoteCommandFail() throws ExecutionException, InterruptedException {
@ -155,7 +155,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试3: 方法回调超时场景
* 远程命令发送成功但方法回调超时不发送回调消息
*/
// @Test
@Test
@Order(3)
@DisplayName("测试3: 方法回调超时场景 - 远程命令成功,但方法回调超时")
public void testMethodCallbackTimeout() throws ExecutionException, InterruptedException {
@ -178,7 +178,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试4: 状态回调超时场景
* 方法回调成功但状态回调超时
*/
// @Test
@Test
@Order(4)
@DisplayName("测试4: 状态回调超时场景 - 方法回调成功,但状态回调超时")
public void testStateCallbackTimeout() throws ExecutionException, InterruptedException {
@ -212,7 +212,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试5: 成功子命令场景
* 主指令成功后执行成功分支的子指令
*/
// @Test
@Test
@Order(5)
@DisplayName("测试5: 成功子命令场景 - 主指令成功后执行成功分支子指令")
public void testSuccessSubCommand() throws ExecutionException, InterruptedException {
@ -253,7 +253,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试6: 失败子命令场景
* 主指令失败后执行失败分支的补救子指令
*/
// @Test
@Test
@Order(6)
@DisplayName("测试6: 失败子命令场景 - 主指令失败后执行失败分支补救子指令")
public void testFailureSubCommand() throws ExecutionException, InterruptedException {
@ -300,7 +300,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试7: Always子命令场景主指令成功
* 主指令成功无论如何都执行清理指令
*/
// @Test
@Test
@Order(7)
@DisplayName("测试7: Always子命令场景主指令成功- 无论成功失败都执行清理")
public void testAlwaysSubCommandWithSuccess() throws ExecutionException, InterruptedException {
@ -341,7 +341,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试8: Always子命令场景主指令失败
* 主指令失败但仍然执行清理指令
*/
// @Test
@Test
@Order(8)
@DisplayName("测试8: Always子命令场景主指令失败- 主指令失败仍执行清理")
public void testAlwaysSubCommandWithFailure() throws ExecutionException, InterruptedException {
@ -382,7 +382,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试9: 复杂指令树场景成功路径
* 测试复杂的多层嵌套指令树走成功分支
*/
// @Test
@Test
@Order(9)
@DisplayName("测试9: 复杂指令树场景(成功路径)- 多层嵌套,走成功分支")
public void testComplexInstructionTreeSuccess() throws ExecutionException, InterruptedException {
@ -500,7 +500,7 @@ public class ComprehensiveDrcStateMachineTest {
* 测试12: 指令被通过场景
* canExecute返回true指令可以执行
*/
// @Test
@Test
@Order(12)
@DisplayName("测试12: 指令被通过场景 - canExecute返回true")
public void testCommandAccepted() throws ExecutionException, InterruptedException {
@ -521,60 +521,13 @@ public class ComprehensiveDrcStateMachineTest {
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 不匹配场景
* 回调消息中的 tid bid 与指令执行时生成的值不匹配应该超时
*/
// @Test
@Order(14)
@Test
@Order(13)
@DisplayName("测试14: tid/bid不匹配场景 - 回调消息tid/bid不匹配导致超时")
public void testTidBidMismatch() throws ExecutionException, InterruptedException {
log.info(">>> 场景:回调消息中的 tid 或 bid 与指令执行时生成的值不匹配");