Skip to content

Commit 177fbd8

Browse files
Python(chore): use inprocess to improve test performance (#590)
1 parent 01c3694 commit 177fbd8

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

python/lib/sift_client/_tests/pytest_plugin/conftest.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
block inside ``pytest_configure``, useful for inspecting internal state
1010
without running tests against a real backend
1111
12-
Every test in this suite invokes the inner session via
13-
``pytester.runpytest_subprocess(...)`` rather than ``pytester.runpytest(...)``.
14-
``runpytest`` runs the inner pytest in-process, which re-imports the Sift
15-
plugin on each test; the plugin transitively imports numpy, whose C
16-
extensions refuse to initialize twice in one process and raise
17-
``cannot load module more than once per process``. Spawning a subprocess
18-
gives each inner session a fresh interpreter and sidesteps that guard.
12+
The offline-log tests (``test_hierarchy.py``, ``test_pass_fail.py``) drive the
13+
inner session in-process via ``pytester.runpytest_inprocess(...)``. This is
14+
fast because the outer session already preloads the plugin (``pyproject.toml``
15+
sets ``addopts = "... -p sift_client.pytest_plugin ..."``), so the numpy C
16+
extensions the plugin pulls in are imported once for the whole outer process
17+
and reused by every inner run — no per-test interpreter spawn, and no
18+
``cannot load module more than once per process`` re-init guard to trip.
19+
20+
Tests that need true process isolation (fresh env vars, credential and
21+
connection resolution, ini parsing) still use ``pytester.runpytest_subprocess(...)``
22+
so the inner session starts from a clean interpreter.
1923
"""
2024

2125
from __future__ import annotations

python/lib/sift_client/_tests/pytest_plugin/test_hierarchy.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_b(self):
7676
"""
7777
)
7878
)
79-
result = pytester.runpytest_subprocess("-v")
79+
result = pytester.runpytest_inprocess("-v")
8080
result.assert_outcomes(passed=2)
8181
steps = capture.load_steps(log_file)
8282
by_name = _by_name(steps)
@@ -97,7 +97,7 @@ def test_a(self):
9797
"""
9898
)
9999
)
100-
result = pytester.runpytest_subprocess("-v")
100+
result = pytester.runpytest_inprocess("-v")
101101
result.assert_outcomes(passed=1)
102102
steps = capture.load_steps(log_file)
103103
by_name = _by_name(steps)
@@ -125,7 +125,7 @@ def test_a(self, v):
125125
"""
126126
)
127127
)
128-
result = pytester.runpytest_subprocess("-v")
128+
result = pytester.runpytest_inprocess("-v")
129129
result.assert_outcomes(passed=2)
130130
steps = capture.load_steps(log_file)
131131
by_name = _by_name(steps)
@@ -150,7 +150,7 @@ def test_y(self):
150150
"""
151151
)
152152
)
153-
result = pytester.runpytest_subprocess("-v")
153+
result = pytester.runpytest_inprocess("-v")
154154
result.assert_outcomes(passed=2)
155155
steps = capture.load_steps(log_file)
156156
by_name = _by_name(steps)
@@ -175,7 +175,7 @@ def test_free():
175175
"""
176176
)
177177
)
178-
result = pytester.runpytest_subprocess("-v")
178+
result = pytester.runpytest_inprocess("-v")
179179
result.assert_outcomes(passed=2)
180180
steps = capture.load_steps(log_file)
181181
by_name = _by_name(steps)
@@ -205,7 +205,7 @@ def test_b(self):
205205
"""
206206
)
207207
)
208-
result = pytester.runpytest_subprocess("-v")
208+
result = pytester.runpytest_inprocess("-v")
209209
result.assert_outcomes(passed=2)
210210
steps = capture.load_steps(log_file)
211211
by_name = _by_name(steps)
@@ -230,7 +230,7 @@ def test_b(self):
230230
"""
231231
)
232232
)
233-
result = pytester.runpytest_subprocess("-v")
233+
result = pytester.runpytest_inprocess("-v")
234234
result.assert_outcomes(passed=2)
235235
steps = capture.load_steps(log_file)
236236
by_name = _by_name(steps)
@@ -252,7 +252,7 @@ def test_a(self):
252252
'''
253253
)
254254
)
255-
result = pytester.runpytest_subprocess("-v")
255+
result = pytester.runpytest_inprocess("-v")
256256
result.assert_outcomes(passed=1)
257257
steps = capture.load_steps(log_file)
258258
by_name = _by_name(steps)
@@ -284,7 +284,7 @@ def test_y(self, w):
284284
"""
285285
)
286286
)
287-
result = pytester.runpytest_subprocess("-v")
287+
result = pytester.runpytest_inprocess("-v")
288288
result.assert_outcomes(passed=2)
289289
steps = capture.load_steps(log_file)
290290
by_name = _by_name(steps)
@@ -396,7 +396,7 @@ def test_c(self):
396396
"""
397397
)
398398
)
399-
result = pytester.runpytest_subprocess("-v")
399+
result = pytester.runpytest_inprocess("-v")
400400
result.assert_outcomes(passed=2, failed=1)
401401
steps = capture.load_steps(log_file)
402402
by_name = _by_name(steps)
@@ -434,7 +434,7 @@ def test_b(self):
434434
"""
435435
)
436436
)
437-
result = pytester.runpytest_subprocess("-v")
437+
result = pytester.runpytest_inprocess("-v")
438438
result.assert_outcomes(passed=2, failed=1)
439439
steps = capture.load_steps(log_file)
440440
by_name = _by_name(steps)
@@ -476,7 +476,7 @@ def test_b(self):
476476
"""
477477
)
478478
)
479-
result = pytester.runpytest_subprocess("-v")
479+
result = pytester.runpytest_inprocess("-v")
480480
result.assert_outcomes(passed=2)
481481
steps = capture.load_steps(log_file)
482482
by_name = _by_name(steps)
@@ -499,7 +499,7 @@ def test_a(self):
499499
"""
500500
)
501501
)
502-
result = pytester.runpytest_subprocess("-v")
502+
result = pytester.runpytest_inprocess("-v")
503503
result.assert_outcomes(passed=1)
504504
steps = capture.load_steps(log_file)
505505
by_name = _by_name(steps)
@@ -524,7 +524,7 @@ def test_a(v):
524524
"""
525525
)
526526
)
527-
result = pytester.runpytest_subprocess("-v")
527+
result = pytester.runpytest_inprocess("-v")
528528
result.assert_outcomes(passed=2)
529529
steps = capture.load_steps(log_file)
530530
by_name = _by_name(steps)
@@ -565,7 +565,7 @@ def test_y(self):
565565
"""
566566
),
567567
)
568-
result = pytester.runpytest_subprocess("-v")
568+
result = pytester.runpytest_inprocess("-v")
569569
result.assert_outcomes(passed=2)
570570
steps = capture.load_steps(log_file)
571571
by_name = _by_name(steps)
@@ -593,7 +593,7 @@ def test_one():
593593
"""
594594
)
595595
)
596-
result = pytester.runpytest_subprocess("-v")
596+
result = pytester.runpytest_inprocess("-v")
597597
result.assert_outcomes(passed=1)
598598
steps = capture.load_steps(log_file)
599599
by_name = _by_name(steps)
@@ -637,7 +637,7 @@ def test_two():
637637
)
638638
# ``importlib`` import mode is required so two packages with the same
639639
# name on disk don't collide during sys.path-based import.
640-
result = pytester.runpytest_subprocess("-v", "--import-mode=importlib")
640+
result = pytester.runpytest_inprocess("-v", "--import-mode=importlib")
641641
result.assert_outcomes(passed=2)
642642
steps = capture.load_steps(log_file)
643643
by_name = _by_name(steps)
@@ -666,7 +666,7 @@ def test_one():
666666
"""
667667
)
668668
)
669-
result = pytester.runpytest_subprocess("-v")
669+
result = pytester.runpytest_inprocess("-v")
670670
result.assert_outcomes(passed=1)
671671
steps = capture.load_steps(log_file)
672672
by_name = _by_name(steps)
@@ -697,7 +697,7 @@ def test_a(self, v):
697697
"""
698698
)
699699
)
700-
result = pytester.runpytest_subprocess("-v")
700+
result = pytester.runpytest_inprocess("-v")
701701
result.assert_outcomes(passed=2)
702702
steps = capture.load_steps(log_file)
703703
by_name = _by_name(steps)
@@ -730,7 +730,7 @@ def test_rail(v):
730730
"""
731731
)
732732
)
733-
result = pytester.runpytest_subprocess("-v")
733+
result = pytester.runpytest_inprocess("-v")
734734
result.assert_outcomes(passed=2)
735735
steps = capture.load_steps(log_file)
736736
by_name = _by_name(steps)
@@ -759,7 +759,7 @@ def test_iso(voltage, component):
759759
"""
760760
)
761761
)
762-
result = pytester.runpytest_subprocess("-v")
762+
result = pytester.runpytest_inprocess("-v")
763763
result.assert_outcomes(passed=4)
764764
steps = capture.load_steps(log_file)
765765
by_name = _by_name(steps)
@@ -798,7 +798,7 @@ def test_widget(widget):
798798
"""
799799
)
800800
)
801-
result = pytester.runpytest_subprocess("-v")
801+
result = pytester.runpytest_inprocess("-v")
802802
result.assert_outcomes(passed=2)
803803
steps = capture.load_steps(log_file)
804804
by_name = _by_name(steps)
@@ -831,7 +831,7 @@ def test_two(w):
831831
"""
832832
),
833833
)
834-
result = pytester.runpytest_subprocess("-v")
834+
result = pytester.runpytest_inprocess("-v")
835835
result.assert_outcomes(passed=4)
836836
steps = capture.load_steps(log_file)
837837
by_name = _by_name(steps)
@@ -855,7 +855,7 @@ def test_chain(a, b):
855855
"""
856856
)
857857
)
858-
result = pytester.runpytest_subprocess("-v")
858+
result = pytester.runpytest_inprocess("-v")
859859
result.assert_outcomes(passed=1)
860860
steps = capture.load_steps(log_file)
861861
leaf = next(s for s in steps if s["name"].startswith("b="))

0 commit comments

Comments
 (0)