Skip to content

Commit

Permalink
#31 Made parallel task propagating exceptions (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb authored Oct 31, 2024
1 parent 360fb15 commit a341378
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 81 deletions.
2 changes: 2 additions & 0 deletions pytest-backend/doc/changes/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [0.2.0](changes_0.2.0.md)
* [0.2.1](changes_0.2.1.md)
* [0.3.0](changes_0.3.0.md)
* [0.3.1](changes_0.3.1.md)

<!--- This MyST Parser Sphinx directive is necessary to keep Sphinx happy. We need list here all release letters again, because release droid and other scripts assume Markdown --->
```{toctree}
Expand All @@ -16,5 +17,6 @@ changes_0.1.0
changes_0.2.0
changes_0.2.1
changes_0.3.0
changes_0.3.1
```
14 changes: 14 additions & 0 deletions pytest-backend/doc/changes/changes_0.3.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 0.3.1 - 2024-10-31

## Summary

Relocked the dependencies and fixed the deficiencies of the 0.3.0

## Documentation

* #69: Fixed names of the CLI options in the user guide of pytest-exasol-backend

## Refactorings

* Relock dependencies
* #71 Made the ParallelTask propagating exceptions to the caller.
8 changes: 0 additions & 8 deletions pytest-backend/doc/changes/unreleased.md
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
# Unreleased

## Documentation

* #69: Fixed names of CLI options in user guide of pytest-exasol-backend

## Refactorings

* Relock dependencies
23 changes: 18 additions & 5 deletions pytest-backend/exasol/pytest_backend/parallel_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ def __init__(self, func, args, kwargs):
self._proc: mp.Process | None = None

def _run(self) -> None:
with self._ctx_func(*self._args, **self._kwargs) as output:
# Now the task has completed. Make the results available for
# caller and indicate the completion.
self._queue.put(output)
try:
with self._ctx_func(*self._args, **self._kwargs) as output:
# Now the task has completed. Make the results available for
# caller and indicate the completion.
# Return None as the error object followed by the function output.
self._queue.put(None)
self._queue.put(output)
self._ready.set()
# Wait until the caller is done and proceed with the cleanup.
self._done.wait()
except Exception as ex:
# Pass the exception back to the caller and wait until it's done.
self._queue.put(ex)
self._ready.set()
# Wait until the caller is done and proceed with the cleanup.
self._done.wait()

def __enter__(self) -> "_ParallelGenCtxManager":
Expand All @@ -43,6 +51,11 @@ def wait(self, timeout: float | None = None) -> None:
self._proc.kill()
raise TimeoutError(f'{self._func_name} failed to complete within {timeout} seconds')

# Check if an exception is returned and if so re-raise it
error = self._queue.get()
if error is not None:
raise RuntimeError(f'{self._func_name} failed') from error

def get_output(self, timeout: float | None = None) -> Any:
self.wait(timeout)
return self._queue.get()
Expand Down
2 changes: 1 addition & 1 deletion pytest-backend/exasol/pytest_backend/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`.
MAJOR = 0
MINOR = 3
PATCH = 0
PATCH = 1
VERSION = f"{MAJOR}.{MINOR}.{PATCH}"
136 changes: 70 additions & 66 deletions pytest-backend/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pytest-backend/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-exasol-backend"
version = "0.3.0"
version = "0.3.1"
description = ""
authors = ["Mikhail Beck <[email protected]>"]
readme = "README.md"
Expand Down
13 changes: 13 additions & 0 deletions pytest-backend/test/unit/test_parallel_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
import json
import pytest

from exasol.pytest_backend.parallel_task import paralleltask

Expand Down Expand Up @@ -32,3 +33,15 @@ def test_my_brewery():
assert brewery_info['logos'] == ['Cambridge Beaver', 'Cam Droplets']
# Now the flyer should be deleted
assert not os.path.exists(flyer_file)


@paralleltask
def my_div(num: int, den: int):
yield num // den


def test_div_by_zero():
with pytest.raises(RuntimeError) as ex_info:
with my_div(10, 0) as my_div_async:
my_div_async.get_output()
assert isinstance(ex_info.value.__cause__, ZeroDivisionError)

0 comments on commit a341378

Please sign in to comment.