continuo-engine-contract 0.8.0__tar.gz → 0.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/PKG-INFO +4 -4
  2. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/README.md +1 -1
  3. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/keys.py +3 -3
  4. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/port.py +9 -1
  5. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/testing/conformance.py +130 -5
  6. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/pyproject.toml +3 -3
  7. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_contract_packaging.py +1 -1
  8. continuo_engine_contract-0.9.0/tests/test_startup_conformance.py +167 -0
  9. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/.gitignore +0 -0
  10. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/LICENSE +0 -0
  11. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/NOTICE +0 -0
  12. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/__init__.py +0 -0
  13. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/config.py +0 -0
  14. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/result.py +0 -0
  15. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/sql.py +0 -0
  16. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/testing/__init__.py +0 -0
  17. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/continuo_engine_contract/types.py +0 -0
  18. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/__init__.py +0 -0
  19. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_config.py +0 -0
  20. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_conformance_suite.py +0 -0
  21. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_contract_keys.py +0 -0
  22. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_contract_types.py +0 -0
  23. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_port.py +0 -0
  24. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_result.py +0 -0
  25. {continuo_engine_contract-0.8.0 → continuo_engine_contract-0.9.0}/tests/test_sql.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: continuo-engine-contract
3
- Version: 0.8.0
3
+ Version: 0.9.0
4
4
  Summary: The contract a Continuo warehouse engine must satisfy: the WarehouseAdapter port and the result-block wire format.
5
5
  Author: Simone Carolini
6
6
  License-Expression: Apache-2.0
@@ -9,9 +9,9 @@ License-File: NOTICE
9
9
  Classifier: Development Status :: 4 - Beta
10
10
  Classifier: Programming Language :: Python :: 3.14
11
11
  Requires-Python: >=3.14
12
- Requires-Dist: sqlglot==30.17.0
12
+ Requires-Dist: sqlglot==30.23.0
13
13
  Provides-Extra: testing
14
- Requires-Dist: pyarrow==25.0.1; extra == 'testing'
14
+ Requires-Dist: pyarrow==26.0.0; extra == 'testing'
15
15
  Requires-Dist: pytest==9.1.1; extra == 'testing'
16
16
  Description-Content-Type: text/markdown
17
17
 
@@ -28,7 +28,7 @@ validation runner alike — and every engine adapter library. Seven modules:
28
28
  | `continuo_engine_contract.sql` | `ensure_single_read`, the parse-backed gate `check_binds` runs before it lets a declared read near an engine |
29
29
  | `continuo_engine_contract.config` | `ensure_known_keys`, the fail-closed gate `build_empty_from_columns` runs before it lets a node's physical-layout `config` block reach DDL |
30
30
  | `continuo_engine_contract.result` | The sentinel-framed result-block wire format the runner emits and the k8s-controller parses |
31
- | `continuo_engine_contract.testing.conformance` | The pytest conformance suite for the write capabilities — `IntrospectionConformance`, `AppendConformance`, `UpsertConformance` — that an adapter's integration tests subclass, providing `adapter` and `schema` fixtures. Needs the `testing` extra |
31
+ | `continuo_engine_contract.testing.conformance` | The pytest conformance suite that an adapter's integration tests subclass: `StartupConformance`, which every adapter runs (`from_env` waits out a warehouse that refuses connections at first; fixtures `warehouse_address` and `adapter_from_env`), and the write capabilities' `IntrospectionConformance`, `AppendConformance`, `UpsertConformance` (fixtures `adapter` and `schema`). Needs the `testing` extra |
32
32
 
33
33
  Each `continuo-python-runtime-<engine>` library depends on this package and
34
34
  implements the port.
@@ -11,7 +11,7 @@ validation runner alike — and every engine adapter library. Seven modules:
11
11
  | `continuo_engine_contract.sql` | `ensure_single_read`, the parse-backed gate `check_binds` runs before it lets a declared read near an engine |
12
12
  | `continuo_engine_contract.config` | `ensure_known_keys`, the fail-closed gate `build_empty_from_columns` runs before it lets a node's physical-layout `config` block reach DDL |
13
13
  | `continuo_engine_contract.result` | The sentinel-framed result-block wire format the runner emits and the k8s-controller parses |
14
- | `continuo_engine_contract.testing.conformance` | The pytest conformance suite for the write capabilities — `IntrospectionConformance`, `AppendConformance`, `UpsertConformance` — that an adapter's integration tests subclass, providing `adapter` and `schema` fixtures. Needs the `testing` extra |
14
+ | `continuo_engine_contract.testing.conformance` | The pytest conformance suite that an adapter's integration tests subclass: `StartupConformance`, which every adapter runs (`from_env` waits out a warehouse that refuses connections at first; fixtures `warehouse_address` and `adapter_from_env`), and the write capabilities' `IntrospectionConformance`, `AppendConformance`, `UpsertConformance` (fixtures `adapter` and `schema`). Needs the `testing` extra |
15
15
 
16
16
  Each `continuo-python-runtime-<engine>` library depends on this package and
17
17
  implements the port.
@@ -29,9 +29,9 @@ def ensure_unique_key(data: "pyarrow.Table", primary_key: Sequence[str]) -> None
29
29
  Groups the rows by the key columns. NULL counts as a value here; the
30
30
  contract requires every key column to be ``nullable: false`` and the
31
31
  harness's ``conform()`` rejects NULLs in such columns before this runs.
32
- The message names the first repeated keys, in order of first appearance,
33
- with their row counts. Only methods of *data* are called, so this module
34
- needs no pyarrow import.
32
+ The message names up to a fixed number of the repeated keys with their row
33
+ counts; their order follows pyarrow's grouping and is not contractual. Only
34
+ methods of *data* are called, so this module needs no pyarrow import.
35
35
 
36
36
  Raises:
37
37
  ValueError: If *primary_key* is empty, names a column *data* lacks, or
@@ -58,7 +58,15 @@ class WarehouseAdapter(ABC):
58
58
  @classmethod
59
59
  @abstractmethod
60
60
  def from_env(cls) -> "WarehouseAdapter":
61
- """Construct a connected adapter from environment variables."""
61
+ """Construct a connected adapter from environment variables.
62
+
63
+ A warehouse that refuses connections for the first few seconds does not
64
+ fail it: a Job pod's first connections can be refused until the
65
+ cluster's NetworkPolicy engine has admitted the pod. A connection that
66
+ timed out is not waited out again. ``StartupConformance`` in
67
+ ``continuo_engine_contract.testing.conformance`` checks this against the
68
+ real engine.
69
+ """
62
70
 
63
71
  @abstractmethod
64
72
  def ensure_schema(self, schema: str) -> None:
@@ -1,15 +1,17 @@
1
- """Conformance tests for the engine contract's write capabilities.
1
+ """Conformance tests for the engine contract.
2
2
 
3
- One test class per capability. An adapter's integration suite subclasses the
4
- classes for exactly the capabilities it declares and runs them against its
5
- real engine; a bring-your-own adapter can do the same::
3
+ One test class per capability, plus :class:`StartupConformance`, which every
4
+ adapter runs. An adapter's integration suite subclasses ``StartupConformance``
5
+ and the classes for exactly the capabilities it declares, and runs them against
6
+ its real engine; a bring-your-own adapter can do the same::
6
7
 
7
8
  from continuo_engine_contract.testing.conformance import AppendConformance
8
9
 
9
10
  class TestMyEngineAppend(AppendConformance):
10
11
  pass
11
12
 
12
- The subclass's module (or a conftest.py above it) provides two fixtures:
13
+ The subclass's module (or a conftest.py above it) provides two fixtures for the
14
+ capability classes:
13
15
 
14
16
  ``adapter``
15
17
  A connected adapter instance that declares the capability under test.
@@ -17,6 +19,8 @@ The subclass's module (or a conftest.py above it) provides two fixtures:
17
19
  A schema name the tests may create tables in (``ensure_table`` creates
18
20
  it). The fixture drops it after the test.
19
21
 
22
+ ``StartupConformance`` takes two others, described on the class.
23
+
20
24
  Rows are read back through ``adapter.fetch`` with ANSI double-quoted
21
25
  identifiers; an engine that quotes differently overrides
22
26
  :meth:`ConformanceBase.read_rows`. Comparisons raise ``AssertionError`` with
@@ -26,7 +30,10 @@ Needs the ``testing`` extra: ``pip install "continuo-engine-contract[testing]"``
26
30
  """
27
31
  from __future__ import annotations
28
32
 
33
+ import contextlib
29
34
  import datetime
35
+ import socket
36
+ import threading
30
37
  import uuid
31
38
  from collections.abc import Iterable, Sequence
32
39
  from decimal import Decimal
@@ -100,6 +107,124 @@ def canonical(rows: Iterable[Sequence[Any]]) -> list[tuple[Any, ...]]:
100
107
  return sorted((tuple(row) for row in rows), key=repr)
101
108
 
102
109
 
110
+ # A Job pod's first connections can be refused for a few seconds after the pod
111
+ # starts, until the cluster's NetworkPolicy engine has admitted the pod: an
112
+ # engine that rejects (k3s's kube-router) refuses at once instead of dropping
113
+ # the packets. ``from_env`` must wait out a refusal this long.
114
+ REFUSAL_SECONDS = 3.0
115
+
116
+
117
+ class LateOpeningPort:
118
+ """A loopback port that refuses connections for a while, then forwards them.
119
+
120
+ For *refuse_for* seconds no socket holds the port, so the kernel refuses
121
+ every connection to it, as a NetworkPolicy engine that rejects does; then
122
+ it listens and forwards each connection to *upstream*. A port that Docker
123
+ publishes cannot stand in for the refusal: Docker accepts the connection
124
+ and then closes it, which a client reports as a dropped connection, not a
125
+ refused one.
126
+ """
127
+
128
+ def __init__(self, upstream: tuple[str, int], refuse_for: float) -> None:
129
+ self._upstream = upstream
130
+ with socket.socket() as probe:
131
+ probe.bind(("127.0.0.1", 0))
132
+ self.port: int = probe.getsockname()[1]
133
+ self._lock = threading.Lock()
134
+ self._closed = False
135
+ self._listener: socket.socket | None = None
136
+ self._sockets: list[socket.socket] = []
137
+ self._timer = threading.Timer(refuse_for, self._open)
138
+ self._timer.daemon = True
139
+ self._timer.start()
140
+
141
+ def __enter__(self) -> LateOpeningPort:
142
+ return self
143
+
144
+ def __exit__(self, *exc_info: object) -> None:
145
+ self.close()
146
+
147
+ def _open(self) -> None:
148
+ listener = socket.socket()
149
+ listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
150
+ listener.bind(("127.0.0.1", self.port))
151
+ listener.listen(16)
152
+ with self._lock:
153
+ if self._closed:
154
+ listener.close()
155
+ return
156
+ self._listener = listener
157
+ threading.Thread(target=self._accept, args=(listener,), daemon=True).start()
158
+
159
+ def _accept(self, listener: socket.socket) -> None:
160
+ while True:
161
+ try:
162
+ client, _ = listener.accept()
163
+ except OSError:
164
+ return
165
+ try:
166
+ upstream = socket.create_connection(self._upstream)
167
+ except OSError:
168
+ client.close()
169
+ continue
170
+ with self._lock:
171
+ self._sockets.extend([client, upstream])
172
+ threading.Thread(target=_pipe, args=(client, upstream), daemon=True).start()
173
+ threading.Thread(target=_pipe, args=(upstream, client), daemon=True).start()
174
+
175
+ def close(self) -> None:
176
+ """Stop listening and drop every forwarded connection."""
177
+ self._timer.cancel()
178
+ with self._lock:
179
+ self._closed = True
180
+ listener, sockets = self._listener, list(self._sockets)
181
+ # shutdown() before close(): on Linux, close() alone does not wake a
182
+ # thread blocked in accept() on the listener.
183
+ for sock in ([listener] if listener else []) + sockets:
184
+ with contextlib.suppress(OSError):
185
+ sock.shutdown(socket.SHUT_RDWR)
186
+ sock.close()
187
+
188
+
189
+ def _pipe(source: socket.socket, sink: socket.socket) -> None:
190
+ try:
191
+ while data := source.recv(65536):
192
+ sink.sendall(data)
193
+ except OSError:
194
+ pass
195
+ finally:
196
+ for sock in (source, sink):
197
+ with contextlib.suppress(OSError):
198
+ sock.close()
199
+
200
+
201
+ class StartupConformance:
202
+ """``from_env`` returns a working adapter when the warehouse refuses connections at first.
203
+
204
+ Every adapter runs this class. Its fixtures:
205
+
206
+ ``warehouse_address``
207
+ The ``(host, port)`` of the real engine endpoint the adapter connects
208
+ to first (for an engine whose catalog lives elsewhere, the endpoint
209
+ ``from_env`` opens a connection to).
210
+ ``adapter_from_env``
211
+ A callable ``(host, port) -> adapter`` that points the engine's
212
+ connection env at *host*:*port* and returns ``from_env()``.
213
+ """
214
+
215
+ def test_from_env_waits_out_a_warehouse_that_refuses_at_first(
216
+ self, warehouse_address, adapter_from_env
217
+ ):
218
+ with LateOpeningPort(warehouse_address, refuse_for=REFUSAL_SECONDS) as port:
219
+ adapter = adapter_from_env("127.0.0.1", port.port)
220
+ try:
221
+ rows = adapter.fetch("SELECT 1 AS one").to_pylist()
222
+ finally:
223
+ adapter.close()
224
+ if rows != [{"one": 1}]:
225
+ raise AssertionError(f"SELECT 1 AS one returned {rows}, expected [{{'one': 1}}]")
226
+
227
+
103
228
  class ConformanceBase:
104
229
  """Helpers shared by the capability classes (fixtures: see the module docstring)."""
105
230
 
@@ -1,5 +1,5 @@
1
1
  [project]
2
- version = "0.8.0"
2
+ version = "0.9.0"
3
3
  name = "continuo-engine-contract"
4
4
  description = "The contract a Continuo warehouse engine must satisfy: the WarehouseAdapter port and the result-block wire format."
5
5
  authors = [{ name = "Simone Carolini" }]
@@ -7,7 +7,7 @@ readme = "README.md"
7
7
  requires-python = ">= 3.14"
8
8
  license = "Apache-2.0"
9
9
  license-files = ["LICENSE", "NOTICE"]
10
- dependencies = ["sqlglot==30.17.0"]
10
+ dependencies = ["sqlglot==30.23.0"]
11
11
  classifiers = [
12
12
  "Development Status :: 4 - Beta",
13
13
  "Programming Language :: Python :: 3.14",
@@ -17,7 +17,7 @@ classifiers = [
17
17
  # The conformance suite (continuo_engine_contract.testing.conformance) for adapter test
18
18
  # suites, this repo's and bring-your-own: pytest, plus pyarrow to build the rows.
19
19
  # Pinned exactly, like every in-repo dependency.
20
- testing = ["pytest==9.1.1", "pyarrow==25.0.1"]
20
+ testing = ["pytest==9.1.1", "pyarrow==26.0.0"]
21
21
 
22
22
  [build-system]
23
23
  requires = ["hatchling"]
@@ -23,4 +23,4 @@ def test_wheel_ships_the_conformance_suite_and_the_testing_extra(tmp_path):
23
23
  assert "continuo_engine_contract/keys.py" in names
24
24
  assert "Provides-Extra: testing" in metadata
25
25
  assert re.search(r"Requires-Dist: pytest==9\.1\.1; extra == ['\"]testing['\"]", metadata)
26
- assert re.search(r"Requires-Dist: pyarrow==25\.0\.1; extra == ['\"]testing['\"]", metadata)
26
+ assert re.search(r"Requires-Dist: pyarrow==26\.0\.0; extra == ['\"]testing['\"]", metadata)
@@ -0,0 +1,167 @@
1
+ """StartupConformance and its LateOpeningPort, run against a reference adapter.
2
+
3
+ The reference adapter connects a TCP socket to a ping/pong server: it shows a
4
+ bring-your-own adapter how to provide the two fixtures, and two broken
5
+ adapters check that the suite catches an adapter that connects once and one
6
+ that connects lazily, without retrying, on its first request.
7
+ """
8
+ import contextlib
9
+ import os
10
+ import socket
11
+ import threading
12
+ import time
13
+
14
+ import pyarrow as pa
15
+ import pytest
16
+
17
+ from continuo_engine_contract.testing.conformance import LateOpeningPort, StartupConformance
18
+
19
+
20
+ @pytest.fixture
21
+ def warehouse_address():
22
+ """A ping/pong server standing in for a warehouse."""
23
+ server = socket.socket()
24
+ server.bind(("127.0.0.1", 0))
25
+ server.listen(16)
26
+
27
+ def serve():
28
+ while True:
29
+ try:
30
+ conn, _ = server.accept()
31
+ except OSError:
32
+ return
33
+ with conn:
34
+ while conn.recv(4) == b"ping":
35
+ conn.sendall(b"pong")
36
+
37
+ threading.Thread(target=serve, daemon=True).start()
38
+ yield server.getsockname()
39
+ with server, contextlib.suppress(OSError):
40
+ # Wakes the accept() on Linux; macOS refuses shutdown() on a listener.
41
+ server.shutdown(socket.SHUT_RDWR)
42
+
43
+
44
+ def _address():
45
+ return os.environ["REFERENCE_HOST"], int(os.environ["REFERENCE_PORT"])
46
+
47
+
48
+ class _RetryingWarehouse:
49
+ """Connects in from_env and retries a refused connection: what the suite requires."""
50
+
51
+ def __init__(self, sock):
52
+ self._sock = sock
53
+
54
+ @classmethod
55
+ def from_env(cls):
56
+ for attempt in range(1, 6):
57
+ try:
58
+ return cls(socket.create_connection(_address(), timeout=5))
59
+ except ConnectionRefusedError:
60
+ if attempt == 5:
61
+ raise
62
+ time.sleep(1.0)
63
+
64
+ def fetch(self, sql):
65
+ if self._sock is None:
66
+ self._sock = socket.create_connection(_address(), timeout=5)
67
+ self._sock.sendall(b"ping")
68
+ if self._sock.recv(4) != b"pong":
69
+ raise RuntimeError("the warehouse did not answer")
70
+ return pa.table({"one": [1]})
71
+
72
+ def close(self):
73
+ if self._sock is not None:
74
+ self._sock.close()
75
+
76
+
77
+ class _ConnectOnceWarehouse(_RetryingWarehouse):
78
+ @classmethod
79
+ def from_env(cls):
80
+ return cls(socket.create_connection(_address(), timeout=5))
81
+
82
+
83
+ class _LazyWarehouse(_RetryingWarehouse):
84
+ """Opens no connection until the first request, and does not retry it."""
85
+
86
+ @classmethod
87
+ def from_env(cls):
88
+ return cls(None)
89
+
90
+
91
+ def _from_env_of(adapter_cls, monkeypatch):
92
+ def build(host, port):
93
+ monkeypatch.setenv("REFERENCE_HOST", host)
94
+ monkeypatch.setenv("REFERENCE_PORT", str(port))
95
+ return adapter_cls.from_env()
96
+
97
+ return build
98
+
99
+
100
+ @pytest.fixture
101
+ def adapter_from_env(monkeypatch):
102
+ return _from_env_of(_RetryingWarehouse, monkeypatch)
103
+
104
+
105
+ class TestReferenceStartup(StartupConformance):
106
+ pass
107
+
108
+
109
+ def test_the_suite_catches_an_adapter_that_connects_once(warehouse_address, monkeypatch):
110
+ with pytest.raises(ConnectionRefusedError):
111
+ StartupConformance().test_from_env_waits_out_a_warehouse_that_refuses_at_first(
112
+ warehouse_address, _from_env_of(_ConnectOnceWarehouse, monkeypatch)
113
+ )
114
+
115
+
116
+ def test_the_suite_catches_an_adapter_that_connects_lazily_without_retrying(
117
+ warehouse_address, monkeypatch
118
+ ):
119
+ with pytest.raises(ConnectionRefusedError):
120
+ StartupConformance().test_from_env_waits_out_a_warehouse_that_refuses_at_first(
121
+ warehouse_address, _from_env_of(_LazyWarehouse, monkeypatch)
122
+ )
123
+
124
+
125
+ # --- the LateOpeningPort double itself ---------------------------------------
126
+
127
+
128
+ def _ping(port):
129
+ with socket.create_connection(("127.0.0.1", port), timeout=2) as conn:
130
+ conn.sendall(b"ping")
131
+ return conn.recv(4)
132
+
133
+
134
+ def test_the_port_refuses_connections_before_it_opens(warehouse_address):
135
+ with LateOpeningPort(warehouse_address, refuse_for=60) as port:
136
+ with pytest.raises(ConnectionRefusedError):
137
+ socket.create_connection(("127.0.0.1", port.port), timeout=2)
138
+
139
+
140
+ def test_the_port_forwards_once_it_opens(warehouse_address):
141
+ with LateOpeningPort(warehouse_address, refuse_for=0.2) as port:
142
+ deadline = time.monotonic() + 5
143
+ while True:
144
+ try:
145
+ assert _ping(port.port) == b"pong"
146
+ break
147
+ except ConnectionRefusedError:
148
+ if time.monotonic() > deadline:
149
+ raise
150
+ time.sleep(0.05)
151
+
152
+
153
+ def test_the_port_refuses_connections_after_it_closes(warehouse_address):
154
+ port = LateOpeningPort(warehouse_address, refuse_for=0)
155
+ deadline = time.monotonic() + 5
156
+ while True:
157
+ try:
158
+ _ping(port.port)
159
+ break
160
+ except ConnectionRefusedError:
161
+ if time.monotonic() > deadline:
162
+ raise
163
+ time.sleep(0.05)
164
+ time.sleep(0.2) # let the accept thread block in accept(): the Linux case needs it
165
+ port.close()
166
+ with pytest.raises(ConnectionRefusedError):
167
+ socket.create_connection(("127.0.0.1", port.port), timeout=2)