修改测试用例

This commit is contained in:
孙小云 2025-12-18 19:10:24 +08:00
parent 03dd8ad7d9
commit af7800bdbe
3 changed files with 117 additions and 28 deletions

View File

@ -77,30 +77,27 @@ public class TidBidMatchingTest {
} }
/** /**
* 测试13: tid/bid 功能演示 - 不配置 tid/bid 时正常工作 * 测试13: tid/bid 匹配成功场景
* 使用简单的成功指令来演示当不配置 tid/bid 回调正常工作 * 使用固定的tid/bid值发送匹配的消息应该成功回调
*/ */
@Test @Test
@Order(13) @Order(13)
@DisplayName("测试13: 不配置tid/bid时正常工作") @DisplayName("测试13: tid/bid匹配成功")
public void testWithoutTidBid() throws ExecutionException, InterruptedException { public void testTidBidMatch() throws ExecutionException, InterruptedException {
log.info(">>> 场景:不配置 tid/bid 时,回调正常工作"); log.info(">>> 场景:tid/bid 匹配成功,回调正常工作");
// 使用 TAKE_OFF 命令它使用 TestSimpleSuccessInstruction不配置 tid/bid // 使用 PAUSE_MISSION 命令它使用 TestHardcodedTidBidInstruction配置了固定的 tid/bid
CompletableFuture<CommandResult> future = CompletableFuture<CommandResult> future =
machineCommandManager.executeCommand(TEST_SN, CommandType.TAKE_OFF, new HashMap<>()); machineCommandManager.executeCommand(TEST_SN, CommandType.PAUSE_MISSION, new HashMap<>());
scheduler.schedule(() -> { scheduler.schedule(() -> {
try { try {
Thread.sleep(100); 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); mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/response", response);
log.info(">>> 模拟发送方法回调不含tid/bid: {}", response); log.info(">>> 模拟发送方法回调tid/bid匹配: {}", response);
Thread.sleep(100);
response = "{\"status\":\"completed\"}";
mqttCallbackRegistry.handleMessage("test/" + TEST_SN + "/state", response);
log.info(">>> 模拟发送状态回调不含tid/bid: {}", response);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
@ -108,31 +105,50 @@ public class TidBidMatchingTest {
}, 200, TimeUnit.MILLISECONDS); }, 200, TimeUnit.MILLISECONDS);
CommandResult result = future.get(); CommandResult result = future.get();
assertTrue(result.isSuccess(), "指令应该执行成功"); assertTrue(result.isSuccess(), "tid/bid匹配时指令应该执行成功");
log.info(">>> 测试通过:不配置 tid/bid 时,消息正常匹配"); log.info(">>> 测试通过:tid/bid 匹配成功,消息正常回调");
} }
/** /**
* 测试14: tid/bid 不匹配场景 * 测试14: tid/bid 不匹配场景
* 注意这个测试演示当配置了 tid/bid 但消息中的值不匹配时的情况 * 发送不匹配的tid/bid消息应该被过滤掉导致超时
*/ */
@Test @Test
@Order(14) @Order(14)
@DisplayName("测试14: tid/bid不匹配导致超时") @DisplayName("测试14: tid/bid不匹配导致超时")
public void testTidBidMismatch() throws ExecutionException, InterruptedException { public void testTidBidMismatch() {
log.info(">>> 场景:演示 tid/bid 过滤机制"); log.info(">>> 场景tid/bid 不匹配,消息被过滤,导致超时");
log.info(">>> 注意:由于测试指令使用 TestTidBidMatchInstruction它配置了 tid/bid");
log.info(">>> 但我们发送的消息不包含正确的 tid/bid所以会被过滤掉");
// 这个测试实际上会超时因为 TestTidBidMatchInstruction 配置了 tid/bid // 使用 PAUSE_MISSION 命令它使用 TestHardcodedTidBidInstruction配置了固定的 tid/bid
// 但我们无法在测试中获取到自动生成的 tid/bid CompletableFuture<CommandResult> future =
// 所以这个测试主要是演示 tid/bid 过滤的存在 machineCommandManager.executeCommand(TEST_SN, CommandType.PAUSE_MISSION, new HashMap<>());
log.info(">>> 跳过此测试,因为需要实际的 tid/bid 值"); scheduler.schedule(() -> {
log.info(">>> tid/bid 过滤功能已在 MqttCallbackRegistry 中实现"); try {
log.info(">>> 可以通过日志观察到 'tid/bid 不匹配,跳过回调' 的消息"); 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 @AfterAll

View File

@ -156,6 +156,12 @@ public class TestVendorConfig implements VendorConfig {
.setTimeout(10000); .setTimeout(10000);
transactionMap.put(CommandType.EXIT_DRC_MODE, tidBidMatchTransaction); 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()); log.info("测试厂家配置初始化完成,共配置{}个命令", transactionMap.size());
} }
} }

View File

@ -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;
}
}