Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/pytest_plugin.py
# Copyright (C) 2019-2022 The Software Heritage developers | # Copyright (C) 2019-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | |||||
import multiprocessing | import multiprocessing | ||||
from pathlib import Path | from pathlib import Path | ||||
import subprocess | import subprocess | ||||
from aiohttp.test_utils import TestClient, TestServer, loop_context | from aiohttp.test_utils import TestClient, TestServer, loop_context | ||||
import grpc | import grpc | ||||
import pytest | import pytest | ||||
from swh.graph.grpc.swhgraph_pb2_grpc import TraversalServiceStub | |||||
from swh.graph.http_client import RemoteGraphClient | from swh.graph.http_client import RemoteGraphClient | ||||
from swh.graph.http_naive_client import NaiveClient | from swh.graph.http_naive_client import NaiveClient | ||||
from swh.graph.rpc.swhgraph_pb2_grpc import TraversalServiceStub | |||||
SWH_GRAPH_TESTS_ROOT = Path(__file__).parents[0] / "tests" | SWH_GRAPH_TESTS_ROOT = Path(__file__).parents[0] / "tests" | ||||
TEST_GRAPH_PATH = SWH_GRAPH_TESTS_ROOT / "dataset/compressed/example" | TEST_GRAPH_PATH = SWH_GRAPH_TESTS_ROOT / "dataset/compressed/example" | ||||
logger = logging.getLogger(__name__) | |||||
class GraphServerProcess(multiprocessing.Process): | class GraphServerProcess(multiprocessing.Process): | ||||
def __init__(self, *args, **kwargs): | def __init__(self, *args, **kwargs): | ||||
self.q = multiprocessing.Queue() | self.q = multiprocessing.Queue() | ||||
super().__init__(*args, **kwargs) | super().__init__(*args, **kwargs) | ||||
def run(self): | def run(self): | ||||
# Lazy import to allow debian packaging | # Lazy import to allow debian packaging | ||||
from swh.graph.http_server import make_app | from swh.graph.http_server import make_app | ||||
try: | try: | ||||
config = {"graph": {"path": TEST_GRAPH_PATH}} | config = { | ||||
"graph": { | |||||
"cls": "local", | |||||
"grpc_server": {"path": TEST_GRAPH_PATH}, | |||||
"rpc_server": {"debug": True}, | |||||
} | |||||
} | |||||
with loop_context() as loop: | with loop_context() as loop: | ||||
app = make_app(config=config, debug=True, spawn_rpc_port=None) | app = make_app(config=config) | ||||
client = TestClient(TestServer(app), loop=loop) | client = TestClient(TestServer(app), loop=loop) | ||||
loop.run_until_complete(client.start_server()) | loop.run_until_complete(client.start_server()) | ||||
url = client.make_url("/graph/") | url = client.make_url("/graph/") | ||||
self.q.put( | self.q.put( | ||||
{ | { | ||||
"server_url": url, | "server_url": url, | ||||
"rpc_url": app["rpc_url"], | "rpc_url": app["rpc_url"], | ||||
"pid": app["local_server"].pid, | "pid": app["local_server"].pid, | ||||
} | } | ||||
) | ) | ||||
loop.run_forever() | loop.run_forever() | ||||
except Exception as e: | except Exception as e: | ||||
logger.exception(e) | |||||
vlorentz: `sentry_sdk.capture_exception(e)` | |||||
self.q.put(e) | self.q.put(e) | ||||
def start(self, *args, **kwargs): | def start(self, *args, **kwargs): | ||||
super().start() | super().start() | ||||
self.result = self.q.get() | self.result = self.q.get() | ||||
@pytest.fixture(scope="module") | @pytest.fixture(scope="module") | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |
sentry_sdk.capture_exception(e)