|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import hashlib |
| 4 | +import socket |
| 5 | +import threading |
4 | 6 | import time |
5 | 7 | from pathlib import Path |
6 | 8 |
|
|
11 | 13 | from pace_controller.leak import LeakMonitor |
12 | 14 | from pace_controller.models import LeakThresholds |
13 | 15 | from pace_controller.service import scpi_float, scpi_number, scpi_numbers, scpi_payload |
14 | | -from pace_controller.transports import SimulatorTransport |
| 16 | +from pace_controller.transports import SimulatorTransport, TcpTransport |
15 | 17 |
|
16 | 18 |
|
17 | 19 | ROOT = Path(__file__).resolve().parents[2] |
@@ -61,6 +63,57 @@ def test_simulator_accepts_same_scpi_as_real_transport() -> None: |
61 | 63 | device.close() |
62 | 64 |
|
63 | 65 |
|
| 66 | +def test_tcp_transport_matches_validated_pace_line_endings() -> None: |
| 67 | + listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 68 | + listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 69 | + listener.bind(("127.0.0.1", 0)) |
| 70 | + listener.listen(1) |
| 71 | + host, port = listener.getsockname() |
| 72 | + received: list[bytes] = [] |
| 73 | + server_errors: list[BaseException] = [] |
| 74 | + |
| 75 | + def receive_command(connection: socket.socket) -> bytes: |
| 76 | + payload = bytearray() |
| 77 | + while not payload.endswith(b"\n"): |
| 78 | + chunk = connection.recv(1024) |
| 79 | + if not chunk: |
| 80 | + break |
| 81 | + payload.extend(chunk) |
| 82 | + return bytes(payload) |
| 83 | + |
| 84 | + def serve() -> None: |
| 85 | + try: |
| 86 | + connection, _ = listener.accept() |
| 87 | + with connection: |
| 88 | + connection.settimeout(2.0) |
| 89 | + received.append(receive_command(connection)) |
| 90 | + connection.sendall(b"DRUCK,PACE5000,TEST,1.0\r") |
| 91 | + time.sleep(0.05) |
| 92 | + connection.sendall(b"\n") |
| 93 | + received.append(receive_command(connection)) |
| 94 | + time.sleep(0.05) |
| 95 | + connection.sendall(b"BAR\r\n") |
| 96 | + except BaseException as exc: # surfaced in the test thread |
| 97 | + server_errors.append(exc) |
| 98 | + finally: |
| 99 | + listener.close() |
| 100 | + |
| 101 | + server = threading.Thread(target=serve, daemon=True) |
| 102 | + server.start() |
| 103 | + transport = TcpTransport(host, port, timeout=1.0) |
| 104 | + try: |
| 105 | + transport.connect() |
| 106 | + assert transport.query("*IDN?") == "DRUCK,PACE5000,TEST,1.0" |
| 107 | + assert transport.query(":UNIT1:PRES?") == "BAR" |
| 108 | + finally: |
| 109 | + transport.close() |
| 110 | + server.join(2.0) |
| 111 | + |
| 112 | + assert not server.is_alive() |
| 113 | + assert not server_errors |
| 114 | + assert received == [b"*IDN?\r\n", b":UNIT1:PRES?\r\n"] |
| 115 | + |
| 116 | + |
64 | 117 | @pytest.mark.parametrize( |
65 | 118 | ("elapsed", "drop", "expected"), |
66 | 119 | [ |
|
0 commit comments