From 769eb6ffca01d4f155a8b04336f947d828690009 Mon Sep 17 00:00:00 2001 From: Artemiy Vereshchinskiy Date: Wed, 18 Nov 2026 01:40:47 +0700 Subject: [PATCH] Release 3.4.2: direct mode keeps its engine connection alive DirectEngine opened a new connection (urllib.request.urlopen) for every protocol call. It now keeps one http.client connection per thread and reuses it (HTTPS when the engine URL says so, verified as before). - A connection the engine closed while idle is detected before reuse and replaced. - If a reused connection fails before the engine answers, a read (describe, query other than a statement) is sent once more on a new connection; a write is not and raises RushDBError, as before. - Read timeouts still raise TimeoutError; DirectEngine takes `timeout` (default 120 s, unchanged). - Connections of ended threads are closed; close() closes all; a forked child does not touch the parent's sockets. - When HTTP_PROXY/HTTPS_PROXY applies to the engine URL, calls go through urllib and the proxy, as before. - No new dependency. count(*) p50 through db.engine.execute() against a local rushdb 3.0.12 container (20K records, 3 x 3000 calls per run, ABBA): via the published port: urlopen 0.155-0.164 ms, keep-alive 0.077-0.079 ms via the container IP: urlopen 0.121-0.123 ms, keep-alive 0.066 ms raw http.client keep-alive floor: 0.070 / 0.061 ms --- README.md | 6 + pyproject.toml | 2 +- src/rushdb/direct/engine.py | 218 +++++++++++++++-- tests/test_direct_engine_transport.py | 329 ++++++++++++++++++++++++++ tests/test_direct_mode.py | 38 ++- tests/test_direct_mode_live.py | 17 ++ tests/test_error_bodies.py | 18 +- uv.lock | 2 +- 8 files changed, 575 insertions(+), 55 deletions(-) create mode 100644 tests/test_direct_engine_transport.py diff --git a/README.md b/README.md index 15e3b21..504b6b2 100755 --- a/README.md +++ b/README.md @@ -104,6 +104,12 @@ from rushdb import CLOUD_ONLY_METHODS, CloudOnlyError, is_cloud_only_method `db.engine.execute()` runs any Protocol v1 operation directly. +Direct mode keeps its connection to the engine alive: each thread reuses one connection, so a call +does not pay for a new TCP (or TLS) handshake. A connection the engine closed is replaced; a read +that lost its connection is sent once more, a write is not (it raises `RushDBError`). +`db.engine.close()` closes the open connections. When `HTTP_PROXY`/`HTTPS_PROXY` applies to the +engine URL, calls go through the proxy without keep-alive. + --- ## Agent memory in 3 lines diff --git a/pyproject.toml b/pyproject.toml index d7769dc..f5e87c8 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "rushdb" -version = "3.4.1" +version = "3.4.2" description = "RushDB SDK for Python: the client for RushDB, the database for AI (RushDB Cloud or self-hosted)" authors = [ {name = "RushDB Team", email = "hi@rushdb.com"} diff --git a/src/rushdb/direct/engine.py b/src/rushdb/direct/engine.py index 189fd92..c491ca2 100755 --- a/src/rushdb/direct/engine.py +++ b/src/rushdb/direct/engine.py @@ -5,19 +5,80 @@ no RushDB Cloud. The tenant scope (multitenant engines) is the ``X-RushDB-Tenant`` header and auth is the engine token. The REST calls of the SDK are answered from these operations by :mod:`rushdb.direct.router`. + +Connections are kept alive: each thread reuses one ``http.client`` connection +to the engine, so a call does not pay for a new TCP (and TLS) handshake. """ +import http.client import json +import os +import select +import threading import urllib.error +import urllib.parse import urllib.request -from typing import Any, Dict, Optional +from typing import Any, Dict, Optional, Tuple from ..common import RushDBError, parse_error_body +DEFAULT_TIMEOUT = 120.0 +"""Seconds to wait for the engine to connect or answer.""" + +# The engine may close an idle keep-alive connection at any time. A request sent +# on such a connection fails with one of these before any answer arrives. +_STALE_CONNECTION_ERRORS = ( + http.client.RemoteDisconnected, + http.client.BadStatusLine, + ConnectionResetError, + ConnectionAbortedError, + BrokenPipeError, +) + + +def _is_read(operation: Dict[str, Any]) -> bool: + """Whether ``operation`` only reads, so sending it twice is harmless. + + A Cypher or SQL statement can write, so it does not count as a read. + """ + kind = operation.get("kind") + if kind == "describe": + return True + if kind == "query": + query = operation.get("query") + return isinstance(query, dict) and query.get("kind") != "statement" + return False + + +def _is_dropped(conn: http.client.HTTPConnection) -> bool: + """Whether the engine closed ``conn`` while it sat idle. + + An idle keep-alive socket has nothing to read; once it turns readable the + engine has closed it (or sent something unexpected), so it is not reused. + """ + sock = conn.sock + if sock is None: + return True + try: + if hasattr(select, "poll"): + poller = select.poll() + poller.register(sock, select.POLLIN) + return bool(poller.poll(0)) + return bool(select.select([sock], [], [], 0)[0]) + except (OSError, ValueError): + return True + class DirectEngine: """Client for a standalone ``rushdb`` protocol endpoint. + Each thread keeps one connection to the engine and reuses it across calls. + A connection the engine closed while idle is replaced before the next + request. If a reused connection fails before the engine answers, a read is + sent once more on a new connection; a write is not, and the error is + raised. When a proxy applies to ``engine_url`` (``HTTP_PROXY`` / + ``HTTPS_PROXY``), every call goes through ``urllib`` and the proxy instead. + Args: engine_url (str): Base URL of the standalone engine, e.g. ``"http://127.0.0.1:3000"``. @@ -25,6 +86,8 @@ class DirectEngine: scope (Optional[str], optional): Tenant scope. When provided it is sent both in the envelope context and the ``X-RushDB-Tenant`` header (multitenant engines). + timeout (float, optional): Seconds to wait for the engine to connect or + answer. Default 120. """ def __init__( @@ -32,10 +95,25 @@ def __init__( engine_url: str, token: str, scope: Optional[str] = None, + timeout: float = DEFAULT_TIMEOUT, ): self.base_url = engine_url.rstrip("/") self.token = token self.scope = scope + self.timeout = timeout + url = urllib.parse.urlsplit(self.base_url) + self._https = url.scheme == "https" + self._host = url.hostname or "" + self._port = url.port + self._path = f"{url.path}/protocol/v1/execute" + self._proxied = bool( + urllib.request.getproxies().get(url.scheme) + ) and not urllib.request.proxy_bypass(url.netloc) + # One connection per thread, with the process it was opened in. + self._connections: Dict[ + threading.Thread, Tuple[http.client.HTTPConnection, int] + ] = {} + self._lock = threading.Lock() # ── transport ─────────────────────────────────────────────────────────── @@ -43,8 +121,9 @@ def execute(self, operation: Dict[str, Any]) -> Dict[str, Any]: """Run one protocol operation and return its ``response``. Raises: - RushDBError: When the engine refuses the operation; ``status`` is the - engine's HTTP status. + RushDBError: When the engine refuses the operation (``status`` is the + engine's HTTP status) or cannot be reached. + TimeoutError: When the engine does not answer within ``timeout``. """ envelope: Dict[str, Any] = {"protocolVersion": 1, "operation": operation} if self.scope: @@ -55,30 +134,135 @@ def execute(self, operation: Dict[str, Any]) -> Dict[str, Any]: } if self.scope: headers["X-RushDB-Tenant"] = self.scope + data = json.dumps(envelope).encode("utf-8") + status, raw = self._send(data, headers, _is_read(operation)) + if status >= 400: + message, details, code = parse_error_body(raw) + raise RushDBError( + message or f"engine request failed (HTTP {status})", + details, + status=status, + code=code, + ) + body = json.loads(raw.decode("utf-8") or "{}") + if body.get("error"): + message, details, code = parse_error_body(json.dumps(body)) + raise RushDBError(message or "engine request failed", details, code=code) + return body.get("response", {}) + + def close(self) -> None: + """Close the connections this client keeps open, in every thread. + + The next call opens a new one. + """ + with self._lock: + connections = list(self._connections.values()) + self._connections.clear() + for conn, pid in connections: + if pid == os.getpid(): + conn.close() + + def __del__(self) -> None: + try: + self.close() + except Exception: # noqa: BLE001 - nothing to report to at teardown + pass + + def _send( + self, data: bytes, headers: Dict[str, str], is_read: bool + ) -> Tuple[int, bytes]: + """POST ``data`` to the engine; return the HTTP status and the raw body.""" + if self._proxied: + return self._send_via_urllib(data, headers) + conn, reused = self._connection() + try: + return self._exchange(conn, data, headers) + except _STALE_CONNECTION_ERRORS as exc: + conn.close() + if not (reused and is_read): + raise RushDBError(f"Connection error: {exc}") from exc + conn, _ = self._connection() + try: + return self._exchange(conn, data, headers) + except _STALE_CONNECTION_ERRORS as exc: + conn.close() + raise RushDBError(f"Connection error: {exc}") from exc + + def _exchange( + self, conn: http.client.HTTPConnection, data: bytes, headers: Dict[str, str] + ) -> Tuple[int, bytes]: + """One request and its answer on ``conn``. Lost-connection errors are + left to the caller; on any other failure ``conn`` is closed.""" + try: + conn.request("POST", self._path, body=data, headers=headers) + resp = conn.getresponse() + return resp.status, resp.read() + except _STALE_CONNECTION_ERRORS: + raise + except TimeoutError: + conn.close() + raise + except (OSError, http.client.HTTPException) as exc: + conn.close() + raise RushDBError(f"Connection error: {exc}") from exc + + def _connection(self) -> Tuple[http.client.HTTPConnection, bool]: + """This thread's open connection, and whether it served a call before.""" + thread = threading.current_thread() + pid = os.getpid() + entry = self._connections.get(thread) + # After a fork the inherited socket belongs to the parent: leave it be. + if entry is not None and entry[1] == pid: + if not _is_dropped(entry[0]): + return entry[0], True + entry[0].close() + conn: http.client.HTTPConnection + if self._https: + conn = http.client.HTTPSConnection( + self._host, self._port, timeout=self.timeout + ) + else: + conn = http.client.HTTPConnection( + self._host, self._port, timeout=self.timeout + ) + try: + conn.connect() + except TimeoutError: + conn.close() + raise + except OSError as exc: + conn.close() + raise RushDBError(f"Connection error: {exc}") from exc + with self._lock: + # Close what threads that have ended left open. + for other, (other_conn, other_pid) in list(self._connections.items()): + if not other.is_alive(): + del self._connections[other] + if other_pid == pid: + other_conn.close() + self._connections[thread] = (conn, pid) + return conn, False + + def _send_via_urllib( + self, data: bytes, headers: Dict[str, str] + ) -> Tuple[int, bytes]: + """One request through ``urllib``, which applies the proxy settings.""" req = urllib.request.Request( f"{self.base_url}/protocol/v1/execute", - data=json.dumps(envelope).encode("utf-8"), + data=data, headers=headers, method="POST", ) try: - with urllib.request.urlopen(req, timeout=120) as resp: # noqa: S310 - body = json.loads(resp.read().decode("utf-8") or "{}") + with urllib.request.urlopen( + req, timeout=self.timeout + ) as resp: # noqa: S310 + return resp.status, resp.read() except urllib.error.HTTPError as exc: try: raw = exc.read() except OSError: raw = b"" - message, details, code = parse_error_body(raw) - raise RushDBError( - message or f"engine request failed (HTTP {exc.code})", - details, - status=exc.code, - code=code, - ) from exc + return exc.code, raw except urllib.error.URLError as exc: raise RushDBError(f"Connection error: {exc}") from exc - if body.get("error"): - message, details, code = parse_error_body(json.dumps(body)) - raise RushDBError(message or "engine request failed", details, code=code) - return body.get("response", {}) diff --git a/tests/test_direct_engine_transport.py b/tests/test_direct_engine_transport.py new file mode 100644 index 0000000..b70f0fd --- /dev/null +++ b/tests/test_direct_engine_transport.py @@ -0,0 +1,329 @@ +"""The direct engine transport keeps its connection alive: one connection per +thread serves every call, a connection the engine closed is replaced, a read +that lost its connection is sent once more and a write is not.""" + +import json +import shutil +import socket +import ssl +import subprocess +import threading +import time +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from unittest import mock + +import pytest + +from rushdb import RushDBError +from rushdb.direct import DirectEngine +from rushdb.direct.engine import _is_read + +READ = {"kind": "describe", "describe": {"kind": "buildInfo"}} +WRITE = {"kind": "mutate", "mutation": {"kind": "createRecord", "label": "Book"}} + + +class _Handler(BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def setup(self): + super().setup() + self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + with self.server.lock: + self.server.connections += 1 + + def log_message(self, *args): + pass + + def do_POST(self): + server = self.server + body = json.loads(self.rfile.read(int(self.headers["Content-Length"]))) + with server.lock: + server.requests.append((self.path, dict(self.headers), body)) + mode = server.modes.pop(0) if server.modes else "ok" + if mode == "drop": + # Read the request, then close without answering. + self.close_connection = True + return + if mode == "slow": + time.sleep(0.5) + status, answer = ( + (401, {"error": "unauthorized"}) + if mode == "refuse" + else (200, {"response": {"ok": True}}) + ) + payload = json.dumps(answer).encode("utf-8") + self.send_response(status) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(payload))) + if mode == "close": + self.send_header("Connection", "close") + self.end_headers() + self.wfile.write(payload) + if mode == "idle-close": + # Close after answering, without announcing it. + self.close_connection = True + + +def _serving(context=None): + httpd = ThreadingHTTPServer(("127.0.0.1", 0), _Handler) + httpd.daemon_threads = True + httpd.lock = threading.Lock() + httpd.connections = 0 + httpd.requests = [] + httpd.modes = [] + httpd.engines = [] + if context is not None: + httpd.socket = context.wrap_socket(httpd.socket, server_side=True) + threading.Thread(target=httpd.serve_forever, args=(0.02,), daemon=True).start() + return httpd + + +def _stop(httpd): + for engine in httpd.engines: + engine.close() + httpd.shutdown() + httpd.server_close() + + +@pytest.fixture +def server(): + httpd = _serving() + yield httpd + _stop(httpd) + + +def engine_for(server, scheme="http", **extra): + host, port = server.server_address + engine = DirectEngine(f"{scheme}://{host}:{port}", "TOKEN", **extra) + server.engines.append(engine) + return engine + + +def test_one_connection_serves_every_call(server): + engine = engine_for(server, scope="tenant-1") + for _ in range(20): + assert engine.execute(READ) == {"ok": True} + engine.execute(WRITE) + assert server.connections == 1 + assert len(server.requests) == 21 + path, headers, body = server.requests[0] + assert path == "/protocol/v1/execute" + assert headers["Authorization"] == "Bearer TOKEN" + assert headers["X-RushDB-Tenant"] == "tenant-1" + assert body == { + "protocolVersion": 1, + "operation": READ, + "context": {"scope": "tenant-1"}, + } + + +def test_url_path_prefix_is_kept(server): + host, port = server.server_address + engine = DirectEngine(f"http://{host}:{port}/engine/", "TOKEN") + server.engines.append(engine) + engine.execute(READ) + assert server.requests[0][0] == "/engine/protocol/v1/execute" + + +def test_a_refusal_keeps_the_connection(server): + engine = engine_for(server) + server.modes = ["refuse"] + with pytest.raises(RushDBError) as failure: + engine.execute(READ) + assert failure.value.status == 401 + assert failure.value.code == "unauthorized" + engine.execute(READ) + assert server.connections == 1 + + +@pytest.mark.parametrize("operation", [READ, WRITE], ids=["read", "write"]) +def test_connection_closed_while_idle_is_replaced(server, operation): + engine = engine_for(server) + server.modes = ["idle-close"] + engine.execute(READ) + time.sleep(0.05) # let the close reach the client + assert engine.execute(operation) == {"ok": True} + assert server.connections == 2 + assert len(server.requests) == 2 + + +def test_announced_close_opens_a_new_connection(server): + engine = engine_for(server) + server.modes = ["close"] + engine.execute(WRITE) + engine.execute(WRITE) + assert server.connections == 2 + + +def test_read_that_lost_its_connection_is_sent_once_more(server): + engine = engine_for(server) + engine.execute(READ) + server.modes = ["drop"] + assert engine.execute(READ) == {"ok": True} + assert len(server.requests) == 3 + assert server.connections == 2 + + +def test_write_that_lost_its_connection_is_not_sent_again(server): + engine = engine_for(server) + engine.execute(READ) + server.modes = ["drop"] + with pytest.raises(RushDBError, match="Connection error"): + engine.execute(WRITE) + assert len(server.requests) == 2 + # The next call works on a new connection. + engine.execute(WRITE) + assert server.connections == 2 + + +def test_read_is_sent_at_most_twice(server): + engine = engine_for(server) + engine.execute(READ) + server.modes = ["drop", "drop"] + with pytest.raises(RushDBError, match="Connection error"): + engine.execute(READ) + assert len(server.requests) == 3 + + +def test_lost_first_connection_is_not_retried(server): + engine = engine_for(server) + server.modes = ["drop"] + with pytest.raises(RushDBError, match="Connection error"): + engine.execute(READ) + assert len(server.requests) == 1 + + +def test_timeout_is_honoured(server): + engine = engine_for(server, timeout=0.2) + server.modes = ["slow"] + started = time.monotonic() + with pytest.raises(TimeoutError): + engine.execute(READ) + assert time.monotonic() - started < 0.45 + assert engine.execute(READ) == {"ok": True} + + +def test_unreachable_engine_raises_rushdb_error(): + engine = DirectEngine("http://127.0.0.1:1", "TOKEN") + with pytest.raises(RushDBError, match="Connection error"): + engine.execute(READ) + + +def test_one_connection_per_thread(server): + engine = engine_for(server) + errors = [] + + def calls(): + try: + for _ in range(10): + engine.execute(READ) + except Exception as exc: # pragma: no cover - reported below + errors.append(exc) + + threads = [threading.Thread(target=calls) for _ in range(4)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + assert errors == [] + assert len(server.requests) == 40 + assert server.connections == 4 + # The next new connection closes those of the threads that have ended. + engine.execute(READ) + assert list(engine._connections) == [threading.current_thread()] + + +def test_close_drops_the_connection(server): + engine = engine_for(server) + engine.execute(READ) + engine.close() + engine.execute(READ) + assert server.connections == 2 + + +def test_proxy_settings_send_through_urllib(monkeypatch): + monkeypatch.setenv("HTTP_PROXY", "http://proxy.example:8080") + monkeypatch.delenv("NO_PROXY", raising=False) + monkeypatch.delenv("no_proxy", raising=False) + engine = DirectEngine("http://engine.example:3000", "TOKEN") + + class Response: + status = 200 + + def read(self): + return b'{"response": {"ok": true}}' + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + with mock.patch("urllib.request.urlopen", return_value=Response()) as urlopen: + assert engine.execute(READ) == {"ok": True} + request = urlopen.call_args.args[0] + assert request.full_url == "http://engine.example:3000/protocol/v1/execute" + + +def test_reads_are_told_apart_from_writes(): + assert _is_read(READ) + assert _is_read({"kind": "query", "query": {"kind": "records", "query": {}}}) + assert not _is_read( + {"kind": "query", "query": {"kind": "statement", "language": "cypher"}} + ) + assert not _is_read(WRITE) + assert not _is_read({"kind": "transaction", "transaction": {"kind": "begin"}}) + assert not _is_read({"kind": "ingest", "ingest": {}}) + assert not _is_read({"kind": "index", "index": {"kind": "drop"}}) + + +@pytest.fixture +def tls_server(tmp_path, monkeypatch): + if shutil.which("openssl") is None: + pytest.skip("openssl is not installed") + cert, key = tmp_path / "cert.pem", tmp_path / "key.pem" + subprocess.run( + [ + "openssl", + "req", + "-x509", + "-newkey", + "rsa:2048", + "-nodes", + "-days", + "1", + "-subj", + "/CN=127.0.0.1", + "-addext", + "subjectAltName=IP:127.0.0.1", + "-addext", + "basicConstraints=critical,CA:TRUE", + "-keyout", + str(key), + "-out", + str(cert), + ], + check=True, + capture_output=True, + ) + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + context.load_cert_chain(cert, key) + httpd = _serving(context) + yield httpd, cert + _stop(httpd) + + +def test_tls_connection_is_verified_and_reused(tls_server, monkeypatch): + httpd, cert = tls_server + monkeypatch.setenv("SSL_CERT_FILE", str(cert)) + engine = engine_for(httpd, scheme="https") + for _ in range(5): + assert engine.execute(READ) == {"ok": True} + assert httpd.connections == 1 + + +def test_tls_certificate_is_not_trusted_blindly(tls_server, monkeypatch): + httpd, _ = tls_server + monkeypatch.delenv("SSL_CERT_FILE", raising=False) + with pytest.raises(RushDBError, match="CERTIFICATE_VERIFY_FAILED"): + engine_for(httpd, scheme="https").execute(READ) diff --git a/tests/test_direct_mode.py b/tests/test_direct_mode.py index 7cef883..fdb47ac 100755 --- a/tests/test_direct_mode.py +++ b/tests/test_direct_mode.py @@ -16,6 +16,7 @@ RushDB, RushDBError, ) +from rushdb.direct import DirectEngine RECORD = { "id": "0190a7e2-0000-7000-8000-000000000001", @@ -158,38 +159,23 @@ def engine_answer(operation): return answers.get(key, {"kind": "mutation", "affected": 1}) -class _Resp: - def __init__(self, body): - self._body = json.dumps(body).encode("utf-8") - - def read(self): - return self._body - - def __enter__(self): - return self - - def __exit__(self, *args): - return False - - @pytest.fixture def engine_calls(): calls = [] - def fake_urlopen(request, timeout=None): - body = json.loads(request.data.decode("utf-8")) if request.data else None + def fake_send(engine, data, headers, is_read): + body = json.loads(data.decode("utf-8")) calls.append( { - "url": request.full_url, - "headers": dict(request.header_items()), + "url": f"{engine.base_url}/protocol/v1/execute", + "headers": headers, "body": body, } ) - return _Resp( - {"protocolVersion": 1, "response": engine_answer(body["operation"])} - ) + answer = {"protocolVersion": 1, "response": engine_answer(body["operation"])} + return 200, json.dumps(answer).encode("utf-8") - with mock.patch("urllib.request.urlopen", side_effect=fake_urlopen): + with mock.patch.object(DirectEngine, "_send", autospec=True, side_effect=fake_send): yield calls @@ -413,7 +399,10 @@ def test_imports_and_connections_are_cloud_only_in_direct_mode(): for name, method in EVERY_METHOD.items(): if name.startswith(("imports.", "connections.")): assert name in CLOUD_ONLY_METHODS, name - with mock.patch("urllib.request.urlopen") as urlopen: + with ( + mock.patch("urllib.request.urlopen") as urlopen, + mock.patch.object(DirectEngine, "_send") as send, + ): with pytest.raises(CloudOnlyError) as raised: direct_db().imports.import_files([{"data": b"x", "name": "a.csv"}]) assert raised.value.method == "imports.import_files" @@ -421,6 +410,7 @@ def test_imports_and_connections_are_cloud_only_in_direct_mode(): direct_db().connections.sync("conn-1") assert raised.value.method == "connections.sync" urlopen.assert_not_called() + send.assert_not_called() def test_direct_mode_needs_an_engine_url(): @@ -431,7 +421,7 @@ def test_direct_mode_needs_an_engine_url(): def test_engine_token_and_tenant_scope_are_sent(engine_calls): direct_db(scope="tenant-1").records.find({"labels": ["Book"]}) assert engine_calls[0]["headers"]["Authorization"] == "Bearer ENGINE-SECRET" - assert engine_calls[0]["headers"]["X-rushdb-tenant"] == "tenant-1" + assert engine_calls[0]["headers"]["X-RushDB-Tenant"] == "tenant-1" assert engine_calls[0]["body"]["context"] == {"scope": "tenant-1"} diff --git a/tests/test_direct_mode_live.py b/tests/test_direct_mode_live.py index 10566d8..93b2505 100644 --- a/tests/test_direct_mode_live.py +++ b/tests/test_direct_mode_live.py @@ -15,6 +15,7 @@ import hmac import json import os +import threading import time import uuid @@ -222,6 +223,22 @@ def test_cloud_features_are_refused(db): db.settings.get() +def test_calls_share_one_connection(db): + db.engine.close() + db.records.find({"labels": [BOOK]}) + conn = db.engine._connections[threading.current_thread()][0] + sock = conn.sock + for _ in range(20): + db.records.find({"labels": [BOOK]}) + db.records.create(BOOK, {"title": "Kept alive"}) + assert db.engine._connections[threading.current_thread()][0] is conn + assert conn.sock is sock + db.engine.close() + assert ( + db.records.find({"labels": [BOOK], "where": {"title": "Kept alive"}}).total == 1 + ) + + def test_bad_token_raises_rushdb_error(): db = RushDB("not-the-token", direct=True, engine_url=ENGINE_URL) with pytest.raises(RushDBError) as failure: diff --git a/tests/test_error_bodies.py b/tests/test_error_bodies.py index c76b0ce..950873b 100644 --- a/tests/test_error_bodies.py +++ b/tests/test_error_bodies.py @@ -11,6 +11,7 @@ from rushdb import RushDB, RushDBError from rushdb.common import parse_error_body +from rushdb.direct import DirectEngine ENVELOPE = { "protocolVersion": 1, @@ -76,7 +77,9 @@ def refuse(request, timeout=None): @pytest.mark.parametrize("body,message,code", SHAPES.values(), ids=SHAPES.keys()) def test_direct_mode_raises_rushdb_error(body, message, code): db = RushDB("BAD", direct=True, engine_url="http://engine:3000") - with mock.patch("urllib.request.urlopen", side_effect=refusing(401, body)): + with mock.patch.object( + DirectEngine, "_send", return_value=(401, body.encode("utf-8")) + ): with pytest.raises(RushDBError) as failure: db.records.create("Book", {"title": "Dune"}) assert str(failure.value) == (message or "engine request failed (HTTP 401)") @@ -110,18 +113,9 @@ def test_raw_upload_raises_rushdb_error(body, message, code): "error", [ENVELOPE["error"], "unauthorized"], ids=["object", "string"] ) def test_direct_mode_error_in_a_200_body(error): - class Response(io.BytesIO): - def __enter__(self): - return self - - def __exit__(self, *exc): - return False - - def answer(request, timeout=None): - return Response(json.dumps({"protocolVersion": 1, "error": error}).encode()) - + answer = json.dumps({"protocolVersion": 1, "error": error}).encode() db = RushDB("BAD", direct=True, engine_url="http://engine:3000") - with mock.patch("urllib.request.urlopen", side_effect=answer): + with mock.patch.object(DirectEngine, "_send", return_value=(200, answer)): with pytest.raises(RushDBError) as failure: db.records.create("Book", {"title": "Dune"}) assert failure.value.code == "unauthorized" diff --git a/uv.lock b/uv.lock index 0e22752..f4b0911 100755 --- a/uv.lock +++ b/uv.lock @@ -710,7 +710,7 @@ wheels = [ [[package]] name = "rushdb" -version = "3.4.1" +version = "3.4.2" source = { editable = "." } [package.dev-dependencies]