modify: 微服务演进
This commit is contained in:
@@ -33,7 +33,8 @@ class FakeWM:
|
||||
self.calls = []
|
||||
self.result = {"result": "ok"}
|
||||
|
||||
async def call(self, worker_type, subject, payload, timeout=30, cacheable=False):
|
||||
async def call(self, worker_type, subject, payload, timeout=30, cacheable=False,
|
||||
instance_id=None):
|
||||
self.calls.append(
|
||||
{
|
||||
"worker_type": worker_type,
|
||||
@@ -41,6 +42,7 @@ class FakeWM:
|
||||
"payload": payload,
|
||||
"timeout": timeout,
|
||||
"cacheable": cacheable,
|
||||
"instance_id": instance_id,
|
||||
}
|
||||
)
|
||||
return self.result
|
||||
@@ -153,6 +155,41 @@ async def test_flat_payload_routed():
|
||||
print("✅ 非信封(扁平)载荷按原始报文路由通过")
|
||||
|
||||
|
||||
async def test_target_instance():
|
||||
"""实例定向:载荷顶层 target_instance 作为控制字段摘除并透传给 worker_manager.call"""
|
||||
wm = FakeWM()
|
||||
bridge, published = make_bridge(
|
||||
wm, [{"topic": "devices/+/telemetry", "worker": "device"}]
|
||||
)
|
||||
# 1. 带 target_instance:透传实例名,且控制字段不进业务 payload
|
||||
await bridge._handle(make_msg(
|
||||
"devices/d1/telemetry",
|
||||
{"meta": {}, "data": {"v": 1}, "target_instance": "edge-01"},
|
||||
))
|
||||
assert wm.calls[-1]["instance_id"] == "edge-01", wm.calls[-1]
|
||||
assert "target_instance" not in wm.calls[-1]["payload"], wm.calls[-1]["payload"]
|
||||
assert wm.calls[-1]["payload"]["data"] == {"v": 1}
|
||||
|
||||
# 2. 不带该字段:instance_id 为 None(跨实例随机均衡,与改动前一致)
|
||||
await bridge._handle(make_msg("devices/d1/telemetry", {"meta": {}, "data": {"v": 1}}))
|
||||
assert wm.calls[-1]["instance_id"] is None, wm.calls[-1]
|
||||
|
||||
# 3. 空白值视为未指定
|
||||
await bridge._handle(make_msg(
|
||||
"devices/d1/telemetry",
|
||||
{"meta": {}, "data": {"v": 1}, "target_instance": " "},
|
||||
))
|
||||
assert wm.calls[-1]["instance_id"] is None, wm.calls[-1]
|
||||
|
||||
# 4. 内置 chongming/... 主题同样支持
|
||||
await bridge._handle(make_msg(
|
||||
f"{MQTT_TOPIC_PREFIX}/example/add",
|
||||
{"meta": {}, "data": {"a": 1}, "target_instance": "edge-02"},
|
||||
))
|
||||
assert wm.calls[-1]["instance_id"] == "edge-02", wm.calls[-1]
|
||||
print("✅ 实例定向(target_instance 控制字段)透传通过")
|
||||
|
||||
|
||||
async def test_non_json_dropped():
|
||||
wm = FakeWM()
|
||||
bridge, published = make_bridge(
|
||||
@@ -219,6 +256,7 @@ async def main():
|
||||
await test_custom_routing()
|
||||
await test_envelope_meta_fields()
|
||||
await test_flat_payload_routed()
|
||||
await test_target_instance()
|
||||
await test_non_json_dropped()
|
||||
await test_board_ping_topic()
|
||||
test_plugin_config_driven_collection()
|
||||
|
||||
Reference in New Issue
Block a user