-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LocalDriver differs from the HPC drivers in that it is made of three parts that are run in sequence. `init` the subprocess, `wait` for the process to complete and `kill` when the user wants to cancel. Between the three parts the driver needs to send `JobEvent`s to the `Scheduler`. This commit implements a `MockDriver`, where the user can optionally specify a simplified version of each of `init`, `wait` or `kill`, depending on what they wish to do.
- Loading branch information
Showing
7 changed files
with
199 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
from ert.scheduler.local_driver import LocalDriver | ||
|
||
|
||
class MockDriver(LocalDriver): | ||
def __init__(self, init=None, wait=None, kill=None): | ||
super().__init__() | ||
self._mock_init = init | ||
self._mock_wait = wait | ||
self._mock_kill = kill | ||
|
||
async def _init(self, *args, **kwargs): | ||
if self._mock_init is not None: | ||
await self._mock_init(*args, **kwargs) | ||
|
||
async def _wait(self, *args): | ||
if self._mock_wait is not None: | ||
result = await self._mock_wait() | ||
return True if result is None else bool(result) | ||
return True | ||
|
||
async def _kill(self, *args): | ||
if self._mock_kill is not None: | ||
await self._mock_kill() | ||
|
||
|
||
@pytest.fixture | ||
def mock_driver(): | ||
return MockDriver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
from ert.scheduler import local_driver | ||
from ert.scheduler.driver import JobEvent | ||
from ert.scheduler.local_driver import LocalDriver | ||
|
||
|
||
async def test_success(tmp_path): | ||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/usr/bin/env", "touch", "testfile", cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.STARTED) | ||
assert await driver.event_queue.get() == (42, JobEvent.COMPLETED) | ||
|
||
assert (tmp_path / "testfile").exists() | ||
|
||
|
||
async def test_failure(tmp_path): | ||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/usr/bin/env", "false", cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.STARTED) | ||
assert await driver.event_queue.get() == (42, JobEvent.FAILED) | ||
|
||
|
||
async def test_file_not_found(tmp_path): | ||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/file/not/found", cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.FAILED) | ||
|
||
|
||
async def test_kill(tmp_path): | ||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/usr/bin/env", "sleep", "10", cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.STARTED) | ||
await driver.kill(42) | ||
assert await driver.event_queue.get() == (42, JobEvent.ABORTED) | ||
|
||
|
||
@pytest.mark.timeout(5) | ||
async def test_kill_unresponsive_process(monkeypatch, tmp_path): | ||
# Reduce timeout to something more appropriate for a test | ||
monkeypatch.setattr(local_driver, "_TERMINATE_TIMEOUT", 0.1) | ||
|
||
(tmp_path / "script").write_text( | ||
"""\ | ||
trap "" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ||
sleep 60 | ||
""" | ||
) | ||
|
||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/bin/sh", tmp_path / "script", cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.STARTED) | ||
await driver.kill(42) | ||
assert await driver.event_queue.get() == (42, JobEvent.ABORTED) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cmd,event", [("true", JobEvent.COMPLETED), ("false", JobEvent.FAILED)] | ||
) | ||
async def test_kill_when_job_completed(tmp_path, cmd, event): | ||
driver = LocalDriver() | ||
|
||
await driver.submit(42, "/usr/bin/env", cmd, cwd=tmp_path) | ||
assert await driver.event_queue.get() == (42, JobEvent.STARTED) | ||
await asyncio.sleep(0.5) | ||
await driver.kill(42) | ||
assert await driver.event_queue.get() == (42, event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters