Copilot commented on code in PR #13820: URL: https://github.com/apache/trafficserver/pull/13820#discussion_r4442129675
########## tests/uranium_tests/query/test_query_retry.py: ########## @@ -0,0 +1,128 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Verify QUERY content is not lost by retrying after an origin closes.""" + +from pathlib import Path +import re +import shlex +import sys + +import pytest + +from tools.uranium.services import ATSFactory, Curl, ServiceFactory, wait_for_metric + +from ..h2.origin_test_helpers import configure_h2_origin, finish_origin + +BODY = 'SELECT * FROM t WHERE a' + + [email protected]('method,buffering', [('GET', False), ('QUERY', True), ('QUERY', False)]) [email protected]('failure', ['close', 'tls-read-error', 'tls-handshake-error']) +def test_query_retry( + ats_factory: ATSFactory, services: ServiceFactory, curl: Curl, method: str, buffering: bool, failure: str) -> None: + """Retry GET after an origin closes, but never replay QUERY without content. + + :param ats_factory: Proxy factory. + :param services: Custom origin factory. + :param curl: Transport-aware HTTP client. + :param method: Request method; GET is the safe retry control. + :param buffering: Whether ATS buffers the request content. + :param failure: Origin close, TLS read error, or TLS failure before sending. Review Comment: The docstring and parameter descriptions are misleading now that this test covers both GET and QUERY with buffering-dependent behavior. Please update the docstring/`:param` text to describe QUERY’s retry rules (e.g., retry allowed only before sending, and after sending only when the body can be replayed via the not-processed retry path) to avoid future confusion when extending failures/methods. ########## tests/uranium_tests/query/query_retry_origin.py: ########## @@ -0,0 +1,125 @@ +''' +An origin that closes the first connection after reading the request, without +responding, and answers every later request with a 200. +''' +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +from pathlib import Path +import socket +import ssl +import sys +import time + + +def read_request(conn: socket.socket) -> tuple[str, bytes]: + """Read one request and its declared content. + + :param conn: Connected origin socket. + """ + data = bytearray() + while b'\r\n\r\n' not in data: + chunk = conn.recv(4096) + if not chunk: + return '', b'' + data += chunk + head, _, rest = data.partition(b'\r\n\r\n') + body = bytearray(rest) + lines = head.decode('latin-1').split('\r\n') + method = lines[0].split(' ')[0] + print(f'request headers method={method}', flush=True) + length = 0 + for line in lines[1:]: + name, _, value = line.partition(':') + if name.strip().lower() == 'content-length': + length = int(value.strip()) + while len(body) < length: + chunk = conn.recv(4096) + if not chunk: + break + body += chunk + return method, bytes(body[:length]) + + +def main() -> int: + """Close or corrupt the first origin response, then serve retries.""" + parser = argparse.ArgumentParser() + parser.add_argument('port', type=int) + parser.add_argument('--tls', nargs=2, metavar=('CERTIFICATE', 'KEY')) + parser.add_argument('--upload-log', type=Path) + parser.add_argument('--handshake-error', action='store_true') + args = parser.parse_args() + context = None + if args.tls: + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + context.load_cert_chain(*args.tls) + context.set_alpn_protocols(['http/1.1']) + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: + listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + listener.bind(('127.0.0.1', args.port)) + listener.listen() + attempt = 0 + handshake_failed = False + while True: + conn, _ = listener.accept() + conn.settimeout(10) + if context: + if args.handshake_error and not handshake_failed: + with conn: + if not conn.recv(1, socket.MSG_PEEK): + continue + conn.sendall(b'not a TLS record\r\n') + handshake_failed = True + print('TLS handshake failed before request', flush=True) + continue + try: + conn = context.wrap_socket(conn, server_side=True) + except ssl.SSLError: + conn.close() + continue + with conn: + try: + method, body = read_request(conn) + except OSError as e: + print(f'request read failed: {e}', flush=True) + continue + if not method: + continue + attempt += 1 + text = body.decode('utf-8', errors='replace') + print(f'request attempt={attempt} method={method} body={text}', flush=True) + if attempt == 1 and not args.handshake_error: + if context: + if args.upload_log and body: + deadline = time.monotonic() + 5 + while '[tunnel_handler_post_server, VC_EVENT_WRITE_COMPLETE' not in args.upload_log.read_text(): Review Comment: This loop can raise an exception if `args.upload_log` does not exist yet (or is temporarily unreadable) when the origin reaches this code path (e.g., timing differences between starting ATS and writing the log). Make the polling robust by checking `args.upload_log.exists()` before `read_text()` or catching `FileNotFoundError`/`OSError` and continuing until the deadline. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
