-
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. To achieve this, we decople the sending of `JobEvent`s from the `LocalDriver` into a new driver called `TaskDriver`. `LocalDriver` then becomes a very bare-bones implementation of just what is needed for `asyncio.subprocess.Process`. Thus, it is now possible to use `MockDriver` and specify the following functions: - `init` is called with the program arguments and awaited - `JobEvent.STARTED` is sent - `wait` is awaited - Depending on the result of `wait` (`True` or `False`), `JobEvent.COMPLETED` or `JobEvent.FAILED` is sent and we are done - If at any point the user wants to cancel, `kill` is awaited - `JobEvent.ABORTED` is then sent
- Loading branch information
Showing
4 changed files
with
224 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
from ert.scheduler.driver import JobEvent | ||
from ert.scheduler.local_driver import LocalDriver | ||
|
||
|
||
async def test_success(tmp_path): | ||
driver = LocalDriver() | ||
|
||
# LocalDriver doesn't have a polling task | ||
assert driver.create_poll_task() is None | ||
|
||
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) | ||
|
||
|
||
async def test_kill_unresponsive_process(tmp_path): | ||
(tmp_path / "script").write_text( | ||
"""\ | ||
trap "" 1 2 3 4 5 6 7 8 10 11 12 13 14 15 | ||
sleep 10 | ||
""" | ||
) | ||
|
||
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