Skip to content

Commit 4b4042a

Browse files
committed
feat(tck): port the table, metadata and ordering steps from Java
Four step definitions the Java binding has and Python did not: - `a {} provider with {string} configuration`, one step rather than two so the backend is serving the named configuration before the provider connects. Backed by an optional `serve_configuration` control operation, read off the control rather than declared on the protocol: Python has no optional protocol member. `prepare_scenario` takes the next scenario back to the suite's configuration, because `/reset` restores flag state and not which configuration is served; `reconnect` keeps the scenario's. - `a context with the following keys:`, the table form of the context step, with `Long` and `StringList` added to the table vocabulary. - `the {} event handler should have been executed with no {} event before it`, which needs an order across event kinds. The per-kind queues held none, so the recorders now share one ordered log and an await claims the one record it matched. - `the resolved metadata should contain` and `the resolved metadata is empty`, whose wording and columns are the specification's own. Signed-off-by: Simon Schrottner <simon.schrottner@flagsmith.com>
1 parent a098392 commit 4b4042a

16 files changed

Lines changed: 1476 additions & 27 deletions

‎tools/openfeature-tck/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,23 @@ provider withheld, and a report saying so would describe a question the suite ne
322322
filter cannot express this: it narrows the whole run, canonical scenarios included, and `not @slow`
323323
leaves an untagged scenario running where `include_tags` must skip it.
324324

325+
### Naming a backend configuration
326+
327+
An extension scenario that needs other flags than the canonical set can ask the backend to serve one
328+
of its own named configurations:
329+
330+
```gherkin
331+
Given a stable provider with "metadata" configuration
332+
```
333+
334+
One step rather than two, because the ordering is the whole difficulty: the backend has to be serving
335+
that configuration **before** the provider connects, and a separate `Given` could be written after
336+
`a stable provider` and watch the provider connect to the wrong flags. The name is the backend's own —
337+
the control API takes `config` as a free string defaulting to `default` — so only a control that can
338+
serve more than one answers it; `InProcessControl` has a single in-process flag set and reports the
339+
missing operation instead. The scenario after yours is taken back to the suite's own configuration,
340+
because `POST /reset` restores flag state and says nothing about what is being served.
341+
325342
**Your scenarios cannot stand in for ours.** Canonical features are identified by their path under
326343
`gherkin/` and yours under `extensions/`, and `extensions.py` refuses a file of yours under the
327344
reserved prefix, or two that would share one uri. Both are reachable here, because pytest-bdd names a

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def tck_config():
9898
ConnectionControl,
9999
UnsupportedControlError,
100100
backend_type_of,
101+
serve_configuration,
101102
)
102103
from .extensions import (
103104
EXTENSIONS_DIRECTORY,
@@ -152,6 +153,7 @@ def tck_config():
152153
"control_api_spec",
153154
"feature_paths",
154155
"run_compose_backend",
156+
"serve_configuration",
155157
]
156158

157159
# The files under gherkin/ and flag_data/, and control-api.yaml, are copies of

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/control.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"ConnectionControl",
1212
"UnsupportedControlError",
1313
"backend_type_of",
14+
"serve_configuration",
1415
"unsupported_control",
1516
]
1617

@@ -142,6 +143,44 @@ def backend_type_of(control: BackendControl) -> BackendType:
142143
return typing.cast("BackendType", stated)
143144

144145

146+
def serve_configuration(control: BackendControl, configuration: str) -> None:
147+
"""Restart the backend on one of its own named flag configurations.
148+
149+
The names belong to the backend rather than to the TCK: the control API takes
150+
``config`` as a free string defaulting to ``default``, so a feature file
151+
naming one is naming something only that backend's control defines. The
152+
canonical scenarios never ask -- the baseline every control already serves is
153+
what they are written against -- which makes this an extension-only
154+
operation.
155+
156+
Optional, and read off the control rather than declared on
157+
:class:`BackendControl`, for the reason :func:`backend_type_of` is: Python has
158+
no optional protocol member, and a ``runtime_checkable`` protocol member stays
159+
required of every implementation even with a default body. A protocol of its
160+
own, as :class:`ConnectionControl` is, would buy an ``isinstance`` narrowing
161+
over a single method that nothing branches on -- a scenario either gets the
162+
configuration it named or the suite is misconfigured -- so the attribute is
163+
asked for directly. A control over one in-process flag set has no second
164+
configuration to offer and simply does not define it.
165+
166+
:raises UnsupportedControlError: if this backend serves one configuration only.
167+
"""
168+
serve = getattr(control, "serve_configuration", None)
169+
if serve is None:
170+
msg = (
171+
f"{control.description} does not support 'serve_configuration', so it "
172+
f"cannot serve the {configuration!r} configuration. This is a "
173+
f"test-configuration bug rather than a provider defect: an extension "
174+
f"scenario named a backend configuration, and only a control over a "
175+
f"backend with more than one can answer that. Either the scenario "
176+
f"belongs to an adoption driving the HTTP control API, or it should "
177+
f"name no configuration and run against the baseline every control "
178+
f"serves."
179+
)
180+
raise UnsupportedControlError(msg)
181+
typing.cast("typing.Callable[[str], None]", serve)(configuration)
182+
183+
145184
def unsupported_control(
146185
control: BackendControl, operation: str
147186
) -> UnsupportedControlError:

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/httpcontrol.py‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class HttpControl:
9393
a disconnect is recorded and the scenario that follows one is prepared with
9494
``/start``.
9595
96+
**And after a served configuration, ``/start`` on this suite's own.** For the
97+
same half of the same reason: ``/reset`` restores flag state and says nothing
98+
about *which* configuration is being served, so a scenario that asked for
99+
another one has to be taken off it explicitly -- see
100+
:meth:`serve_configuration`.
101+
96102
Safe to share between suites, and it should be shared whenever they drive the
97103
same backend: the disconnect bookkeeping is only correct if every operation
98104
against one backend goes through one instance of this class.
@@ -137,6 +143,9 @@ def __init__(
137143
# Set by any operation that may have left the backend down, so the next
138144
# prepare_scenario starts it rather than merely resetting flag state.
139145
self._backend_maybe_down = False
146+
# A configuration one scenario asked for, which the next scenario has to
147+
# be taken off again. None while this suite's own is what is served.
148+
self._scenario_configuration: str | None = None
140149

141150
@property
142151
def backend_type(self) -> BackendType:
@@ -219,11 +228,24 @@ def prepare_scenario(self) -> None:
219228
"""Bring the backend to the state every scenario starts from.
220229
221230
Prefers ``/reset`` and falls back to ``/start`` -- see the class
222-
documentation for why, and for why a disconnect forces ``/start``.
231+
documentation for why, and for why a disconnect or a served configuration
232+
forces ``/start``.
223233
"""
224234
with self._lock:
235+
served = self._scenario_configuration
225236
must_start = self._backend_maybe_down or self._reset_supported is False
226237

238+
if served is not None:
239+
# This suite's configuration, not the last scenario's: that is what
240+
# preparing means. `/reset` would leave the served one in place.
241+
self._start_configuration(self._backend_configuration)
242+
with self._lock:
243+
# Cleared only now the start has succeeded, so a failure is
244+
# retried by the next scenario rather than downgraded to
245+
# `/reset`, which does not change what is being served.
246+
self._scenario_configuration = None
247+
return
248+
227249
if must_start:
228250
self._start()
229251
return
@@ -273,18 +295,43 @@ def reconnect(self) -> None:
273295
274296
Starting with the configuration already in effect restores the same
275297
baseline, so the provider observes a change in availability and never a
276-
change in flag values.
298+
change in flag values. "In effect" includes a configuration this scenario
299+
served: losing the connection is not how a scenario changes what the
300+
backend serves.
277301
"""
278302
self._start()
279303

304+
def serve_configuration(self, configuration: str) -> None:
305+
"""Restart the backend on one of its own named flag configurations.
306+
307+
Optional on a backend control and absent from the protocol -- see
308+
:func:`~.control.serve_configuration`, which is how a step reaches it.
309+
310+
``POST /start?config=...`` with the name given, which also seeds that
311+
configuration's flag state. It takes effect before the provider connects,
312+
because the step that names a configuration is the step that creates the
313+
provider; :meth:`reconnect` brings this configuration back rather than the
314+
suite's, and :meth:`prepare_scenario` takes the next scenario off it.
315+
"""
316+
self._require("/start", {"config": configuration})
317+
with self._lock:
318+
self._scenario_configuration = configuration
319+
self._backend_maybe_down = False
320+
280321
# No binding for `POST /restart`: no shipped scenario reaches it, and a
281322
# binding nothing can call misreports the contract by implying every backend
282323
# under test owes the endpoint. A `@caching` scenario asserting what a stale
283324
# provider serves *during* an outage would bring it back, needing the
284325
# flag-state preservation `/restart` has and `/stop` + `/start` does not.
285326

286327
def _start(self) -> None:
287-
self._require("/start", {"config": self._backend_configuration})
328+
"""Start the backend on the configuration in effect: the scenario's, else this suite's."""
329+
with self._lock:
330+
configuration = self._scenario_configuration or self._backend_configuration
331+
self._start_configuration(configuration)
332+
333+
def _start_configuration(self, configuration: str) -> None:
334+
self._require("/start", {"config": configuration})
288335
with self._lock:
289336
self._backend_maybe_down = False
290337

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/inprocess.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class InProcessControl:
3636
using it leaves :attr:`Capability.STALE` and
3737
:attr:`Capability.UNAVAILABLE_INIT` undeclared.
3838
39+
**Named configurations.** Deliberately absent too, for the same reason: this
40+
control serves one in-process flag set, so there is no second configuration
41+
for a scenario to name. An extension scenario that names one reaches
42+
:func:`~.control.serve_configuration`, which reports the missing operation
43+
rather than quietly serving the baseline and asserting against the wrong
44+
flags.
45+
3946
**Ownership of the provider.** This type both seeds the flags and creates
4047
the provider serving them, because in-process they are the same object:
4148
:meth:`change_flag` has to reach the live instance to emit an event from it.

0 commit comments

Comments
 (0)