Wait for N occurrences of a signal pattern. Args: sock: Connected socket to read from signal_pattern: bytes pattern to count (e.g., b"ready") count: Number of occurrences expected timeout: Socket timeout in seconds Returns: bytes: Complete accum
(sock, signal_pattern, count, timeout=SHORT_TIMEOUT)
| 124 | |
| 125 | |
| 126 | def _wait_for_n_signals(sock, signal_pattern, count, timeout=SHORT_TIMEOUT): |
| 127 | """ |
| 128 | Wait for N occurrences of a signal pattern. |
| 129 | |
| 130 | Args: |
| 131 | sock: Connected socket to read from |
| 132 | signal_pattern: bytes pattern to count (e.g., b"ready") |
| 133 | count: Number of occurrences expected |
| 134 | timeout: Socket timeout in seconds |
| 135 | |
| 136 | Returns: |
| 137 | bytes: Complete accumulated response buffer |
| 138 | |
| 139 | Raises: |
| 140 | RuntimeError: If connection closed or timeout before receiving all signals |
| 141 | """ |
| 142 | sock.settimeout(timeout) |
| 143 | buffer = b"" |
| 144 | found_count = 0 |
| 145 | |
| 146 | while found_count < count: |
| 147 | try: |
| 148 | chunk = sock.recv(4096) |
| 149 | if not chunk: |
| 150 | raise RuntimeError( |
| 151 | f"Connection closed after {found_count}/{count} signals. " |
| 152 | f"Last 200 bytes: {buffer[-200:]!r}" |
| 153 | ) |
| 154 | buffer += chunk |
| 155 | # Count occurrences in entire buffer |
| 156 | found_count = buffer.count(signal_pattern) |
| 157 | except socket.timeout: |
| 158 | raise RuntimeError( |
| 159 | f"Timeout waiting for {count} signals (found {found_count}). " |
| 160 | f"Last 200 bytes: {buffer[-200:]!r}" |
| 161 | ) |
| 162 | |
| 163 | return buffer |
| 164 | |
| 165 | |
| 166 | @contextmanager |
no test coverage detected
searching dependent graphs…