mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 00:16:23 +02:00
Fix test suite Prometheus registry pollution and remove default (#806)
Fix test suite Prometheus registry pollution and remove default coverage
The test_metrics.py fixture (added in 4e63dbda) deleted
class-level metric singleton attributes without restoring them. This
stripped the hasattr() guards that prevent duplicate Prometheus
registration, so every subsequent Processor construction in the test
run raised ValueError: Duplicated timeseries. Fix by saving and
restoring the original attributes around each test.
Remove --cov=trustgraph from pytest.ini defaults — the coverage
import hooks interact badly with the trustgraph namespace package,
causing duplicate class loading. Coverage can still be requested
explicitly via the command line.
This commit is contained in:
parent
39dcd1d386
commit
f976f1b6fe
3 changed files with 16 additions and 5 deletions
|
|
@ -4,14 +4,11 @@ python_paths = .
|
||||||
python_files = test_*.py
|
python_files = test_*.py
|
||||||
python_classes = Test*
|
python_classes = Test*
|
||||||
python_functions = test_*
|
python_functions = test_*
|
||||||
addopts =
|
addopts =
|
||||||
-v
|
-v
|
||||||
--tb=short
|
--tb=short
|
||||||
--strict-markers
|
--strict-markers
|
||||||
--disable-warnings
|
--disable-warnings
|
||||||
--cov=trustgraph
|
|
||||||
--cov-report=html
|
|
||||||
--cov-report=term-missing
|
|
||||||
# --cov-fail-under=80
|
# --cov-fail-under=80
|
||||||
asyncio_mode = auto
|
asyncio_mode = auto
|
||||||
markers =
|
markers =
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,14 @@ from trustgraph.base import metrics
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def reset_metric_singletons():
|
def reset_metric_singletons():
|
||||||
|
"""Temporarily remove metric singletons so each test can inject mocks.
|
||||||
|
|
||||||
|
Saves any existing class-level metrics and restores them after the test
|
||||||
|
so that later tests in the same process still find the hasattr() guard
|
||||||
|
intact — deleting without restoring causes every subsequent Processor()
|
||||||
|
construction to re-register the same Prometheus metric name, which raises
|
||||||
|
ValueError: Duplicated timeseries.
|
||||||
|
"""
|
||||||
classes_and_attrs = {
|
classes_and_attrs = {
|
||||||
metrics.ConsumerMetrics: [
|
metrics.ConsumerMetrics: [
|
||||||
"state_metric",
|
"state_metric",
|
||||||
|
|
@ -23,18 +31,24 @@ def reset_metric_singletons():
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
saved = {}
|
||||||
for cls, attrs in classes_and_attrs.items():
|
for cls, attrs in classes_and_attrs.items():
|
||||||
for attr in attrs:
|
for attr in attrs:
|
||||||
if hasattr(cls, attr):
|
if hasattr(cls, attr):
|
||||||
|
saved[(cls, attr)] = getattr(cls, attr)
|
||||||
delattr(cls, attr)
|
delattr(cls, attr)
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
# Remove anything the test may have set, then restore originals
|
||||||
for cls, attrs in classes_and_attrs.items():
|
for cls, attrs in classes_and_attrs.items():
|
||||||
for attr in attrs:
|
for attr in attrs:
|
||||||
if hasattr(cls, attr):
|
if hasattr(cls, attr):
|
||||||
delattr(cls, attr)
|
delattr(cls, attr)
|
||||||
|
|
||||||
|
for (cls, attr), value in saved.items():
|
||||||
|
setattr(cls, attr, value)
|
||||||
|
|
||||||
|
|
||||||
def test_consumer_metrics_reuses_singletons_and_records_events(monkeypatch):
|
def test_consumer_metrics_reuses_singletons_and_records_events(monkeypatch):
|
||||||
enum_factory = MagicMock()
|
enum_factory = MagicMock()
|
||||||
|
|
|
||||||
|
|
@ -412,4 +412,4 @@ async def test_subscriber_multiple_subscribers():
|
||||||
msg1 = await queue1.get()
|
msg1 = await queue1.get()
|
||||||
msg_all = await queue_all.get()
|
msg_all = await queue_all.get()
|
||||||
assert msg1 == {"data": "broadcast"}
|
assert msg1 == {"data": "broadcast"}
|
||||||
assert msg_all == {"data": "broadcast"}
|
assert msg_all == {"data": "broadcast"}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue