"""C ABI 同步回调:编译器 manifest、生命周期、业务错误和非法缓冲区。""" import json import pathlib import struct import subprocess import sys manifest = json.loads(pathlib.Path(sys.argv[1]).read_text()) binary = sys.argv[2] hello = {name: manifest[name] for name in ("port", "schema", "adapter", "operations", "capabilities")} hello.update(kind="hello", version="dever-component-1", setting={"marker": "unused"}) def frame(value): body = json.dumps(value, ensure_ascii=False).encode() return struct.pack(">I", len(body)) + body def run(messages, suffix=b""): result = subprocess.run([binary], input=b"".join(map(frame, messages)) + suffix, capture_output=True, timeout=3) body = result.stdout replies = [] while body: size = struct.unpack(">I", body[:4])[0] assert size <= len(body) - 4 replies.append(json.loads(body[4:4+size])) body = body[4+size:] return result, replies call = dict(kind="call", id=1, operation="echo", payload=dict(value=2**63-1, label="中文", items=[])) result, replies = run([hello, dict(kind="health", id=0), call, dict(kind="cancel", id=1), dict(kind="call", id=2, operation="fail", payload={}), dict(kind="shutdown")]) assert result.returncode == 0, result.stderr assert replies[2]["payload"] == dict(out=2**63-1, name="中文") assert replies[3]["error"] == "demo.service.port.Error.Rejected" assert replies[3]["payload"]["message"] == "business rejection" assert replies[4] == dict(kind="shutdown") assert result.stderr == b"closed\n" for changed in [dict(schema="wrong"), dict(operations=["other"])]: result, replies = run([hello | changed]) assert result.returncode != 0 and not replies assert b"closed" not in result.stderr for changed in [dict(id=2), dict(operation="missing"), dict(operation="invalid"), dict(id=-1)]: result, replies = run([hello, call | changed]) assert result.returncode != 0 and len(replies) == 1 assert b"closed" in result.stderr body = b'{"kind":"health","id":0,"id":0}' result, replies = run([hello], struct.pack(">I", len(body)) + body) assert result.returncode != 0 and len(replies) == 1 print("C ABI: 8 protocol cases passed")