| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #!/usr/bin/env bash
- # Python 3.11/3.12/3.13 wheel matrix against a live Mock UDS daemon.
- # Missing interpreters are reported, not silently counted as pass.
- set -euo pipefail
- ROOT="$(cd "$(dirname "$0")/.." && pwd)"
- SDK="$ROOT/sdk/python"
- WORKDIR="${TMPDIR:-/tmp}/hth-sdk-matrix"
- mkdir -p "$WORKDIR/dist" "$WORKDIR/run"
- SDK_BUILD="$WORKDIR/sdk-src"
- rm -rf "$SDK_BUILD"
- mkdir -p "$SDK_BUILD"
- cp -a "$SDK/." "$SDK_BUILD/"
- rm -rf "$SDK_BUILD/build" "$SDK_BUILD/hth_sdk.egg-info" "$SDK_BUILD/.pytest_cache"
- export PATH="/opt/python-3.11/bin:/opt/python-3.12/bin:/usr/bin:/data/prod2u/opt/go1.25.13/bin:/root/go/bin:${PATH}"
- export GOTOOLCHAIN="${GOTOOLCHAIN:-local}"
- if [[ -d /data/prod2u/opt/go1.25.13 ]]; then
- export GOROOT=/data/prod2u/opt/go1.25.13
- fi
- PY_BUILD="${HTH_PYTHON:-python3.13}"
- command -v "$PY_BUILD" >/dev/null || PY_BUILD=python3
- BUILD_VENV="$WORKDIR/build-venv"
- rm -rf "$BUILD_VENV"
- "$PY_BUILD" -m venv "$BUILD_VENV"
- "$BUILD_VENV/bin/pip" install -q -i https://mirrors.aliyun.com/pypi/simple build
- "$BUILD_VENV/bin/python" -m build --wheel --outdir "$WORKDIR/dist" "$SDK_BUILD"
- WHEEL="$(ls -1t "$WORKDIR/dist"/hth_sdk-*.whl | head -n1)"
- echo "wheel=$WHEEL"
- CFG="$WORKDIR/run/hthd.yaml"
- RUNTIME="$WORKDIR/run"
- mkdir -p "$RUNTIME/observe" "$RUNTIME/control"
- OBS="$RUNTIME/observe/observe.sock"
- CTL="$RUNTIME/control/control.sock"
- cat > "$CFG" <<EOF
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: $ROOT/profiles/prod2u-door-temp/profile.json
- runtime_root: $RUNTIME
- sockets:
- observe: $OBS
- control: $CTL
- poll:
- temperature_ms: 80
- motor_status_ms: 50
- watchdog:
- heartbeat_period_ms: 200
- lease:
- min_ttl_ms: 50
- max_ttl_ms: 5000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 32
- subscriber_buffer_size: 4
- grpc:
- reflection: false
- mock:
- seed: 1
- startup_complete: true
- EOF
- HTHD="$WORKDIR/run/hthd"
- (cd "$ROOT" && CGO_ENABLED=0 go build -o "$HTHD" ./cmd/hthd)
- start_hthd() {
- rm -f "$OBS" "$CTL"
- "$HTHD" --config "$CFG" &
- HTHD_PID=$!
- for i in $(seq 1 50); do
- [[ -S "$OBS" && -S "$CTL" ]] && return 0
- sleep 0.1
- done
- echo "hthd sockets not ready"
- return 1
- }
- stop_hthd() {
- if [[ -n "${HTHD_PID:-}" ]]; then
- kill "$HTHD_PID" 2>/dev/null || true
- wait "$HTHD_PID" 2>/dev/null || true
- HTHD_PID=
- fi
- }
- cleanup() { stop_hthd; }
- trap cleanup EXIT
- start_hthd
- fail=0
- ran=0
- seen=" "
- for py in python3.11 python3.12 python3.13 python3; do
- if ! command -v "$py" >/dev/null 2>&1; then
- echo "MISSING $py"
- continue
- fi
- ver="$("$py" -c 'import sys; print("%d.%d"%sys.version_info[:2])')"
- case "$ver" in
- 3.11|3.12|3.13) ;;
- *) echo "SKIP $py ($ver not in 3.11/3.12/3.13)"; continue ;;
- esac
- if [[ "$seen" == *" $ver "* ]]; then
- echo "SKIP $py (already ran $ver)"
- continue
- fi
- seen="$seen$ver "
- venv="$WORKDIR/venv-$ver"
- rm -rf "$venv"
- if ! "$py" -m venv "$venv" >/tmp/hth-venv-"$ver".log 2>&1; then
- echo "SKIP $py (venv failed)"
- cat /tmp/hth-venv-"$ver".log | tail -5
- continue
- fi
- ran=$((ran+1))
- stop_hthd
- start_hthd
- # shellcheck disable=SC1091
- source "$venv/bin/activate"
- pip install -q -i https://mirrors.aliyun.com/pypi/simple "$WHEEL[live]"
- HTH_OBSERVE_SOCK="$OBS" HTH_CONTROL_SOCK="$CTL" python - <<'PY' || fail=1
- import asyncio, os
- from datetime import timedelta
- import hth_sdk
- from hth_sdk.generated.hth.api.v1 import hth_pb2, hth_pb2_grpc
- from hth_sdk.client import connect
- from hth_sdk.thermal import ThermalHTH, connect_thermal
- from hth_sdk.types import ActionBatch
- from hth_sdk.profiles import prod2u
- assert hth_pb2 is not None and hth_pb2_grpc is not None
- assert ThermalHTH is not None and connect_thermal is not None
- async def main():
- async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
- caps = await client.get_capabilities("prod2u-0")
- assert caps.api_version == "hth.api.v1"
- assert caps.timing is not None and caps.timing.recommended_rpc_deadline_ms > 2000
- assert caps.timing.recommended_rpc_deadline_ms >= caps.timing.temperature_transaction_worst_case_ms
- snap = await client.get_snapshot("prod2u-0")
- n = 0
- async for ev in client.subscribe_telemetry("prod2u-0"):
- n += 1
- if n >= 2:
- break
- async with await client.acquire_lease("prod2u-0", "matrix", timedelta(seconds=8), auto_renew=False) as lease:
- b = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision)
- for act in caps.actuators:
- tgt = snap.target(act.id)
- if tgt is not None:
- b.set(act.id, tgt.commanded_target)
- else:
- b.set(act.id, act.safety_default)
- batch = b.build()
- val = await lease.validate(batch)
- assert val is not None
- res = await lease.apply(batch)
- assert res.accepted, res
- assert res.confirmation != "CONFIRMED"
- last_eid = 0
- n_ev = 0
- async for ev in client.subscribe_events("prod2u-0"):
- eid = int(ev.event_id)
- assert eid > 0
- if last_eid:
- assert eid > last_eid, (last_eid, eid)
- last_eid = eid
- n_ev += 1
- if n_ev >= 3:
- break
- assert n_ev >= 1
- safe = await lease.enter_safe_mode("wheel-matrix")
- assert safe is not None
- async with await client.acquire_lease("prod2u-0", "matrix-resume", timedelta(seconds=8), auto_renew=False) as lease2:
- snap2 = await client.get_snapshot("prod2u-0")
- rb = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap2.snapshot_seq, snap2.control_revision)
- for act in caps.actuators:
- tgt = snap2.target(act.id)
- if tgt is not None:
- rb.set(act.id, tgt.commanded_target)
- else:
- rb.set(act.id, act.safety_default)
- resumed = await lease2.resume_from_safe(rb.build())
- assert resumed.accepted, resumed
- asyncio.run(main())
- print("matrix-ok", getattr(hth_sdk, "__version__", "0.3.0"))
- PY
- deactivate
- done
- # Production timing: 5s GET_TEMPERATURE + wheel + control lease + periodic Apply.
- TIMING_CFG="$WORKDIR/run/hthd-timing.yaml"
- cat > "$TIMING_CFG" <<EOF
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: $ROOT/profiles/prod2u-door-temp/profile.json
- runtime_root: $RUNTIME
- sockets:
- observe: $OBS
- control: $CTL
- poll:
- temperature_ms: 80
- motor_status_ms: 200
- watchdog:
- heartbeat_period_ms: 8000
- lease:
- min_ttl_ms: 50
- max_ttl_ms: 20000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 32
- subscriber_buffer_size: 4
- grpc:
- reflection: false
- mock:
- seed: 1
- startup_complete: true
- slow_get_ms: 5000
- EOF
- CFG="$TIMING_CFG"
- stop_hthd
- start_hthd
- PY_TIMING=python3.13
- command -v "$PY_TIMING" >/dev/null || PY_TIMING=python3
- venv_t="$WORKDIR/venv-timing"
- rm -rf "$venv_t"
- "$PY_TIMING" -m venv "$venv_t"
- # shellcheck disable=SC1091
- source "$venv_t/bin/activate"
- pip install -q -i https://mirrors.aliyun.com/pypi/simple "$WHEEL[live]"
- HTH_OBSERVE_SOCK="$OBS" HTH_CONTROL_SOCK="$CTL" python - <<'PY' || fail=1
- import asyncio, os, time
- from datetime import timedelta
- from hth_sdk.client import connect
- from hth_sdk.exceptions import ActionRejected
- from hth_sdk.profiles import prod2u
- def _fill(b, caps, snap):
- for act in caps.actuators:
- tgt = snap.target(act.id)
- b.set(act.id, tgt.commanded_target if tgt is not None else act.safety_default)
- return b
- async def main():
- async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
- caps = await client.get_capabilities("prod2u-0")
- assert caps.timing.temperature_transaction_worst_case_ms >= 5000
- rec = caps.timing.recommended_action_ttl_ms
- assert rec > 2000
- for _ in range(80):
- snap = await client.get_snapshot("prod2u-0")
- if snap.lifecycle == "READY":
- break
- await asyncio.sleep(0.05)
- await asyncio.sleep(0.25)
- async with await client.acquire_lease("prod2u-0", "timing", timedelta(seconds=20), auto_renew=True) as lease:
- rejected = False
- attempts = 0
- deadline = time.monotonic() + 12.0
- while time.monotonic() < deadline:
- attempts += 1
- snap = await client.get_snapshot("prod2u-0")
- b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
- batch = b.valid_for(timedelta(seconds=2)).build()
- t0 = time.monotonic()
- try:
- res = await lease.apply(batch)
- except ActionRejected as exc:
- dt = time.monotonic() - t0
- if dt < 0.8 and "EXPIRED_DEADLINE" in str(exc):
- rejected = True
- break
- raise
- dt = time.monotonic() - t0
- reason = str(getattr(res, "reject_reason", ""))
- if (not res.accepted) and dt < 0.8 and "EXPIRED_DEADLINE" in reason:
- rejected = True
- break
- await asyncio.sleep(0.1)
- assert rejected, (
- f"2s deadline must be rejected during 5s GET_TEMPERATURE; attempts={attempts}"
- )
- ok = 0
- for i in range(3):
- snap = await client.get_snapshot("prod2u-0")
- b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
- t0 = time.monotonic()
- res = await lease.apply(b.build())
- dt = time.monotonic() - t0
- assert res.accepted, (i, res, dt)
- assert res.confirmation != "CONFIRMED"
- ok += 1
- assert ok == 3
- # Latest-wins: two overlapping applies during the 5s GET.
- snap = await client.get_snapshot("prod2u-0")
- b1 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
- b2 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
- r1, r2 = await asyncio.gather(lease.apply(b1.build()), lease.apply(b2.build()), return_exceptions=True)
- # One may be superseded or both accepted if the GET finished; neither may be CONFIRMED.
- for r in (r1, r2):
- if isinstance(r, Exception):
- continue
- assert getattr(r, "confirmation", "") != "CONFIRMED"
- asyncio.run(main())
- print("python-sdk-5s-timing-ok")
- PY
- deactivate
- stop_hthd
- echo "interpreters_run=$ran"
- if [[ "$ran" -lt 1 ]]; then
- echo "Python wheel matrix NOT RUN (no 3.11/3.12/3.13 interpreter)"
- exit 2
- fi
- if [[ "$ran" -lt 3 ]]; then
- echo "Python 3.11/3.12/3.13 matrix PARTIAL (ran $ran); missing interpreters are NOT RUN, not PASS"
- fi
- exit "$fail"
|