Skip to content

Commit 64b7223

Browse files
committed
test: Optimise test_processSubprocessFailureNoPid to reduce pytest duration by ~138 seconds
1 parent 0a4f397 commit 64b7223

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/DIRAC/WorkloadManagementSystem/JobWrapper/JobWrapper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
from DIRAC.WorkloadManagementSystem.Client.SandboxStoreClient import SandboxStoreClient
5656
from DIRAC.WorkloadManagementSystem.JobWrapper.Watchdog import Watchdog
5757

58+
CHILD_PID_POLL_INTERVALS = list(range(5, 40, 5))
59+
5860

5961
class JobWrapper:
6062
"""The only user of the JobWrapper is the JobWrapperTemplate"""
@@ -430,14 +432,14 @@ def process(self, command: str, env: dict):
430432
)
431433
exeThread.start()
432434
payloadPID = None
433-
for seconds in range(5, 40, 5):
435+
for seconds in CHILD_PID_POLL_INTERVALS:
434436
time.sleep(seconds)
435437
payloadPID = spObject.getChildPID()
436438
if payloadPID:
437439
self.__setJobParam("PayloadPID", payloadPID)
438440
break
439441
if not payloadPID:
440-
return S_ERROR("Payload process could not start after 140 seconds")
442+
return S_ERROR(f"Payload process could not start after {sum(CHILD_PID_POLL_INTERVALS)} seconds")
441443

442444
watchdog = Watchdog(
443445
pid=self.currentPID,

src/DIRAC/WorkloadManagementSystem/JobWrapper/test/Test_JobWrapper.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,25 +343,40 @@ def test_processQuickExecutionNoWatchdog(mocker):
343343
assert not result["Value"]["watchdogStats"]
344344

345345

346-
@pytest.mark.slow
347-
def test_processSubprocessFailureNoPid(mocker):
348-
"""Test the process method of the JobWrapper class: the subprocess fails and no PID is returned."""
346+
@pytest.mark.parametrize("expect_failure", [True, False])
347+
def test_processSubprocessFailureNoPid(mocker, monkeypatch, expect_failure):
348+
"""Test the process method of the JobWrapper class: the subprocess fails and no PID is returned.
349+
350+
expect_failure is used to ensure that the JobWrapper is functioning correctly even with the other patching
351+
that is applied in the test (e.g. CHILD_PID_POLL_INTERVALS).
352+
"""
349353
# Test failure in starting the payload process
350354
jw = JobWrapper()
351355
jw.jobArgs = {}
352356

353357
mocker.patch.object(jw, "_JobWrapper__report")
354358
mocker.patch.object(jw, "_JobWrapper__setJobParam")
359+
monkeypatch.setattr(
360+
"DIRAC.WorkloadManagementSystem.JobWrapper.JobWrapper.CHILD_PID_POLL_INTERVALS", [0.1, 0.2, 0.3, 0.4, 0.5]
361+
)
362+
355363
mock_exeThread = mocker.Mock()
356364
mock_exeThread.start.side_effect = lambda: time.sleep(0.1)
357-
mocker.patch("DIRAC.WorkloadManagementSystem.JobWrapper.JobWrapper.ExecutionThread", return_value=mock_exeThread)
365+
if expect_failure:
366+
mocker.patch(
367+
"DIRAC.WorkloadManagementSystem.JobWrapper.JobWrapper.ExecutionThread", return_value=mock_exeThread
368+
)
358369

359370
with tempfile.NamedTemporaryFile(delete=True) as std_out, tempfile.NamedTemporaryFile(delete=True) as std_err:
360371
jw.outputFile = std_out.name
361372
jw.errorFile = std_err.name
362373
result = jw.process(command="mock_command", env={})
363-
assert not result["OK"]
364-
assert "Payload process could not start after 140 seconds" in result["Message"]
374+
375+
if expect_failure:
376+
assert not result["OK"]
377+
assert "Payload process could not start after 1.5 seconds" in result["Message"]
378+
else:
379+
assert result["OK"]
365380

366381

367382
# -------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)