From f961c6eb4b6eee465f16960388ddabedf29929d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=B0=8F=E4=BA=91?= Date: Thu, 18 Dec 2025 20:42:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tuoheng/machine/BasicScenarioTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/test/java/com/tuoheng/machine/BasicScenarioTest.java b/src/test/java/com/tuoheng/machine/BasicScenarioTest.java index 1b7e3ff..ccde512 100644 --- a/src/test/java/com/tuoheng/machine/BasicScenarioTest.java +++ b/src/test/java/com/tuoheng/machine/BasicScenarioTest.java @@ -182,6 +182,88 @@ public class BasicScenarioTest { log.info(">>> 测试通过:状态回调超时,错误消息: {}", result.getErrorMessage()); } + + + /** + * 测试1: 简单成功场景 + */ + @Test + @Order(5) + @DisplayName("测试1: 简单成功场景 状态第一次错误 第二次成功") + public void testSimpleSuccessTwo() throws ExecutionException, InterruptedException { + log.info(">>> 场景:指令被通过,远程命令成功,方法回调和状态回调都成功"); + + CompletableFuture future = + machineCommandManager.executeCommand(TEST_SN, CommandType.TAKE_OFF, new HashMap<>()); + + scheduler.schedule(() -> { + try { + Thread.sleep(100); + String response = "{\"result\":\"success\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response); + log.info(">>> 模拟发送方法回调: {}", response); + + Thread.sleep(100); + response = "{\"status\":\"error\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/state", response); + log.info(">>> 模拟发送状态回调: {}", response); + + Thread.sleep(100); + response = "{\"status\":\"completed\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/state", response); + log.info(">>> 模拟发送状态回调: {}", response); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + }, 200, TimeUnit.MILLISECONDS); + + CommandResult result = future.get(); + assertTrue(result.isSuccess(), "指令应该执行成功"); + log.info(">>> 测试通过:指令执行成功"); + } + + + /** + * 测试1: 简单成功场景 + */ + @Test + @Order(6) + @DisplayName("测试6: 简单失败场景 方法第一次失败 第二次成功") + public void testSimpleSuccessThree() throws ExecutionException, InterruptedException { + log.info(">>> 场景:指令被通过,远程命令成功,方法回调和状态回调都成功"); + + CompletableFuture future = + machineCommandManager.executeCommand(TEST_SN, CommandType.TAKE_OFF, new HashMap<>()); + + scheduler.schedule(() -> { + try { + + Thread.sleep(100); + String response = "{\"result\":\"error\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response); + log.info(">>> 模拟发送方法回调: {}", response); + + Thread.sleep(100); + response = "{\"result\":\"success\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response); + log.info(">>> 模拟发送方法回调: {}", response); + + Thread.sleep(100); + response = "{\"status\":\"completed\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/state", response); + log.info(">>> 模拟发送状态回调: {}", response); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + }, 200, TimeUnit.MILLISECONDS); + + CommandResult result = future.get(); + assertFalse(result.isSuccess(), "指令应该失败"); + log.info(">>> 测试通过:指令执行失败"); + } + @AfterAll public static void cleanupAll() { log.info("=== 基础场景测试完成 ===");