Skip to content

Commit f37e988

Browse files
committed
feat(metrics): safely paralelize all defined tests per module
1 parent 4413844 commit f37e988

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

tests/e2e/metrics/conftest.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import json
12
from pathlib import Path
3+
from typing import Dict
24

35
import pytest
46
from _pytest import fixtures
7+
from filelock import FileLock
58

69
from tests.e2e.metrics.infrastructure import MetricsStack
710

811

9-
@pytest.fixture(autouse=True)
10-
def infrastructure(request: fixtures.SubRequest) -> MetricsStack:
12+
@pytest.fixture(autouse=True, scope="module")
13+
def infrastructure(request: fixtures.SubRequest, tmp_path_factory: pytest.TempPathFactory, worker_id) -> MetricsStack:
1114
"""Setup and teardown logic for E2E test infrastructure
1215
1316
Parameters
@@ -25,9 +28,25 @@ def infrastructure(request: fixtures.SubRequest) -> MetricsStack:
2528
Iterator[MetricsStack]
2629
Deployed Infrastructure
2730
"""
31+
stack = MetricsStack(handlers_dir=Path(f"{request.fspath.dirname}/handlers"))
2832
try:
29-
stack = MetricsStack(handlers_dir=Path(f"{request.fspath.dirname}/handlers"))
30-
stack.deploy()
31-
yield stack
33+
if worker_id == "master":
34+
# no parallelization, deploy stack and let fixture be cached
35+
yield stack.deploy()
36+
37+
# tmp dir shared by all workers
38+
root_tmp_dir = tmp_path_factory.getbasetemp().parent
39+
40+
cache = root_tmp_dir / "cache.json"
41+
with FileLock(f"{cache}.lock"):
42+
# If cache exists, return stack outputs back
43+
# otherwise it's the first run by the main worker
44+
# deploy and return stack outputs so subsequent workers can reuse
45+
if cache.is_file():
46+
stack_outputs = json.loads(cache.read_text())
47+
else:
48+
stack_outputs: Dict = stack.deploy()
49+
cache.write_text(json.dumps(stack_outputs))
50+
yield stack_outputs
3251
finally:
3352
stack.delete()

tests/e2e/metrics/test_metrics.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@
33

44
import pytest
55

6-
from tests.e2e.metrics.infrastructure import MetricsStack
76
from tests.e2e.utils import helpers
87

98

109
@pytest.fixture
11-
def infra_outputs(infrastructure: MetricsStack):
12-
return infrastructure.get_stack_outputs()
10+
def basic_handler_fn_arn(infrastructure: dict) -> str:
11+
return infrastructure.get("BasicHandlerArn", "")
1312

1413

1514
@pytest.fixture
16-
def basic_handler_fn(infra_outputs: dict) -> str:
17-
return infra_outputs.get("BasicHandlerArn", "")
15+
def cold_start_fn(infrastructure: dict) -> str:
16+
return infrastructure.get("ColdStart", "")
1817

1918

2019
@pytest.fixture
21-
def cold_start_fn(infra_outputs: dict) -> str:
22-
return infra_outputs.get("ColdStart", "")
23-
24-
25-
@pytest.fixture
26-
def cold_start_fn_arn(infra_outputs: dict) -> str:
27-
return infra_outputs.get("ColdStartArn", "")
20+
def cold_start_fn_arn(infrastructure: dict) -> str:
21+
return infrastructure.get("ColdStartArn", "")
2822

2923

3024
METRIC_NAMESPACE = "powertools-e2e-metric"

0 commit comments

Comments
 (0)