From af7800bdbef26a7c03e1a1931efbaeb3c8cbcd42 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 19:10:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=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/TidBidMatchingTest.java | 72 +++++++++++-------- .../machine/vendor/test/TestVendorConfig.java | 6 ++ .../TestHardcodedTidBidInstruction.java | 67 +++++++++++++++++ 3 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/tuoheng/machine/vendor/test/instruction/TestHardcodedTidBidInstruction.java diff --git a/src/test/java/com/tuoheng/machine/TidBidMatchingTest.java b/src/test/java/com/tuoheng/machine/TidBidMatchingTest.java index 9c16c21..e275463 100644 --- a/src/test/java/com/tuoheng/machine/TidBidMatchingTest.java +++ b/src/test/java/com/tuoheng/machine/TidBidMatchingTest.java @@ -77,30 +77,27 @@ public class TidBidMatchingTest { } /** - * 测试13: tid/bid 功能演示 - 不配置 tid/bid 时正常工作 - * 使用简单的成功指令来演示当不配置 tid/bid 时,回调正常工作 + * 测试13: tid/bid 匹配成功场景 + * 使用固定的tid/bid值,发送匹配的消息,应该成功回调 */ @Test @Order(13) - @DisplayName("测试13: 不配置tid/bid时正常工作") - public void testWithoutTidBid() throws ExecutionException, InterruptedException { - log.info(">>> 场景:不配置 tid/bid 时,回调正常工作"); + @DisplayName("测试13: tid/bid匹配成功") + public void testTidBidMatch() throws ExecutionException, InterruptedException { + log.info(">>> 场景:tid/bid 匹配成功,回调正常工作"); - // 使用 TAKE_OFF 命令,它使用 TestSimpleSuccessInstruction,不配置 tid/bid + // 使用 PAUSE_MISSION 命令,它使用 TestHardcodedTidBidInstruction,配置了固定的 tid/bid CompletableFuture future = - machineCommandManager.executeCommand(TEST_SN, CommandType.TAKE_OFF, new HashMap<>()); + machineCommandManager.executeCommand(TEST_SN, CommandType.PAUSE_MISSION, new HashMap<>()); scheduler.schedule(() -> { try { Thread.sleep(100); - String response = "{\"result\":\"success\"}"; + // 发送匹配的tid/bid消息 + String response = String.format("{\"result\":\"hardcoded_success\",\"tid\":\"%s\",\"bid\":\"%s\"}", + "test-tid-12345", "test-bid-67890"); mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response); - log.info(">>> 模拟发送方法回调(不含tid/bid): {}", response); - - Thread.sleep(100); - response = "{\"status\":\"completed\"}"; - mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/state", response); - log.info(">>> 模拟发送状态回调(不含tid/bid): {}", response); + log.info(">>> 模拟发送方法回调(tid/bid匹配): {}", response); } catch (InterruptedException e) { e.printStackTrace(); @@ -108,31 +105,50 @@ public class TidBidMatchingTest { }, 200, TimeUnit.MILLISECONDS); CommandResult result = future.get(); - assertTrue(result.isSuccess(), "指令应该执行成功"); - log.info(">>> 测试通过:不配置 tid/bid 时,消息正常匹配"); + assertTrue(result.isSuccess(), "tid/bid匹配时指令应该执行成功"); + log.info(">>> 测试通过:tid/bid 匹配成功,消息正常回调"); } /** * 测试14: tid/bid 不匹配场景 - * 注意:这个测试演示当配置了 tid/bid 但消息中的值不匹配时的情况 + * 发送不匹配的tid/bid消息,应该被过滤掉,导致超时 */ @Test @Order(14) @DisplayName("测试14: tid/bid不匹配导致超时") - public void testTidBidMismatch() throws ExecutionException, InterruptedException { - log.info(">>> 场景:演示 tid/bid 过滤机制"); - log.info(">>> 注意:由于测试指令使用 TestTidBidMatchInstruction,它配置了 tid/bid"); - log.info(">>> 但我们发送的消息不包含正确的 tid/bid,所以会被过滤掉"); + public void testTidBidMismatch() { + log.info(">>> 场景:tid/bid 不匹配,消息被过滤,导致超时"); - // 这个测试实际上会超时,因为 TestTidBidMatchInstruction 配置了 tid/bid - // 但我们无法在测试中获取到自动生成的 tid/bid 值 - // 所以这个测试主要是演示 tid/bid 过滤的存在 + // 使用 PAUSE_MISSION 命令,它使用 TestHardcodedTidBidInstruction,配置了固定的 tid/bid + CompletableFuture future = + machineCommandManager.executeCommand(TEST_SN, CommandType.PAUSE_MISSION, new HashMap<>()); - log.info(">>> 跳过此测试,因为需要实际的 tid/bid 值"); - log.info(">>> tid/bid 过滤功能已在 MqttCallbackRegistry 中实现"); - log.info(">>> 可以通过日志观察到 'tid/bid 不匹配,跳过回调' 的消息"); + scheduler.schedule(() -> { + try { + Thread.sleep(100); + // 发送不匹配的tid/bid消息(错误的tid和bid值) + String response = "{\"result\":\"hardcoded_success\",\"tid\":\"wrong-tid\",\"bid\":\"wrong-bid\"}"; + mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response); + log.info(">>> 模拟发送方法回调(tid/bid不匹配): {}", response); + log.info(">>> 此消息应该被过滤掉,不会触发回调"); - assertTrue(true, "tid/bid 过滤功能已实现"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }, 200, TimeUnit.MILLISECONDS); + + // 等待超时 + try { + CommandResult result = future.get(6, TimeUnit.SECONDS); + assertFalse(result.isSuccess(), "tid/bid不匹配时指令应该超时失败"); + log.info(">>> 测试通过:tid/bid 不匹配,消息被过滤,指令超时"); + } catch (TimeoutException e) { + log.info(">>> 测试通过:tid/bid 不匹配,消息被过滤,指令超时"); + assertTrue(true, "tid/bid不匹配导致超时,符合预期"); + } catch (Exception e) { + log.error(">>> 测试异常", e); + fail("测试过程中发生异常: " + e.getMessage()); + } } @AfterAll diff --git a/src/test/java/com/tuoheng/machine/vendor/test/TestVendorConfig.java b/src/test/java/com/tuoheng/machine/vendor/test/TestVendorConfig.java index 4946866..ec6531a 100644 --- a/src/test/java/com/tuoheng/machine/vendor/test/TestVendorConfig.java +++ b/src/test/java/com/tuoheng/machine/vendor/test/TestVendorConfig.java @@ -156,6 +156,12 @@ public class TestVendorConfig implements VendorConfig { .setTimeout(10000); transactionMap.put(CommandType.EXIT_DRC_MODE, tidBidMatchTransaction); + // 11. 固定tid/bid测试 - 使用固定的tid/bid值进行匹配测试 + Transaction hardcodedTidBidTransaction = new Transaction("固定tid/bid测试", CommandType.PAUSE_MISSION) + .root(new TestHardcodedTidBidInstruction()) + .setTimeout(10000); + transactionMap.put(CommandType.PAUSE_MISSION, hardcodedTidBidTransaction); + log.info("测试厂家配置初始化完成,共配置{}个命令", transactionMap.size()); } } \ No newline at end of file diff --git a/src/test/java/com/tuoheng/machine/vendor/test/instruction/TestHardcodedTidBidInstruction.java b/src/test/java/com/tuoheng/machine/vendor/test/instruction/TestHardcodedTidBidInstruction.java new file mode 100644 index 0000000..967492e --- /dev/null +++ b/src/test/java/com/tuoheng/machine/vendor/test/instruction/TestHardcodedTidBidInstruction.java @@ -0,0 +1,67 @@ +package com.tuoheng.machine.vendor.test.instruction; + +import com.tuoheng.machine.instruction.AbstractInstruction; +import com.tuoheng.machine.instruction.CallbackConfig; +import com.tuoheng.machine.instruction.InstructionContext; +import lombok.extern.slf4j.Slf4j; + +/** + * 测试指令 - 使用固定的tid/bid值,用于测试tid/bid匹配功能 + */ +@Slf4j +public class TestHardcodedTidBidInstruction extends AbstractInstruction { + + // 固定的tid和bid值,用于测试 + public static final String FIXED_TID = "test-tid-12345"; + public static final String FIXED_BID = "test-bid-67890"; + + @Override + public String getName() { + return "TEST_HARDCODED_TID_BID"; + } + + @Override + public void executeRemoteCall(InstructionContext context) throws Exception { + String sn = context.getSn(); + log.info("[测试] 发送固定tid/bid指令: sn={}, tid=, bid={}", sn, FIXED_TID, FIXED_BID); + + // 覆盖context中的tid和bid为固定值 + context.setTid(FIXED_TID); + context.setBid(FIXED_BID); + + String topic = "test/" + sn + "/command"; + String payload = String.format("{\"cmd\":\"hardcoded_tid_bid_test\",\"tid\":\"%s\",\"bid\":\"%s\"}", + FIXED_TID, FIXED_BID); + + context.getMqttClient().sendMessage(topic, payload); + log.debug("[测试] MQTT发送成功: topic={}, payload={}", topic, payload); + } + + @Override + public CallbackConfig getMethodCallbackConfig(InstructionContext context) { + String sn = context.getSn(); + + // 配置方法回调 - 需要匹配tid和bid + return CallbackConfig.builder() + .topic("test/" + sn + "/response") + .fieldPath("result") + .expectedValue("hardcoded_success") + .tidFieldPath("tid") + .expectedTid(FIXED_TID) + .bidFieldPath("bid") + .expectedBid(FIXED_BID) + .timeoutMs(5000) + .build(); + } + + @Override + public CallbackConfig getStateCallbackConfig(InstructionContext context) { + // 不需要状态回调 + return null; + } + + @Override + public long getTimeoutMs() { + return 10000; + } +}