fix(tests): Fix test case for Prometheus metrics

Disable prometheus multiprocess mode in tests to avoid global state:
Depending on the workers which are processing a testfile,
we might run into race issues where tests might influence each other.

We also need to make sure to clear any previously collected values
in case the same worker/process is executing different tests which
evaluate prometheus values.

Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-11-27 13:21:37 +01:00
parent 027dfbd970
commit 4637b2edba
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
4 changed files with 26 additions and 8 deletions

View file

@ -43,6 +43,7 @@ from multiprocessing import Lock
import py
import pytest
from prometheus_client import values
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
from sqlalchemy.engine.base import Engine
@ -54,12 +55,16 @@ import aurweb.db
from aurweb import aur_logging, initdb, testing
from aurweb.testing.email import Email
from aurweb.testing.git import GitRepository
from aurweb.testing.prometheus import clear_metrics
logger = aur_logging.get_logger(__name__)
# Synchronization lock for database setup.
setup_lock = Lock()
# Disable prometheus multiprocess mode for tests
values.ValueClass = values.MutexValue
def test_engine() -> Engine:
"""
@ -206,3 +211,13 @@ def email_test() -> None:
that we set them up to be used via aurweb.testing.email.Email.
"""
setup_email()
@pytest.fixture
def prometheus_test():
"""
Prometheus test fixture
Removes any existing values from our metrics
"""
clear_metrics()