Merge pull request #2686 from Textualize/worker-coverage

Increase worker coverage and fix bug.
This commit is contained in:
Rodrigo Girão Serrão
2023-05-30 16:13:20 +01:00
committed by GitHub
3 changed files with 64 additions and 1 deletions

View File

@@ -12,8 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed zero division error https://github.com/Textualize/textual/issues/2673
- Fix `scroll_to_center` when there were nested layers out of view (Compositor full_map not populated fully) https://github.com/Textualize/textual/pull/2684
- Issue with computing progress in workers https://github.com/Textualize/textual/pull/2686
- Issues with `switch_screen` not updating the results callback appropriately https://github.com/Textualize/textual/issues/2650
### Added
- `work` decorator accepts `description` parameter to add debug string https://github.com/Textualize/textual/issues/2597

View File

@@ -256,7 +256,7 @@ class Worker(Generic[ResultType]):
if completed_steps is not None:
self._completed_steps += completed_steps
if total_steps != -1:
self._total_steps = None if total_steps is None else min(0, total_steps)
self._total_steps = None if total_steps is None else max(0, total_steps)
def advance(self, steps: int = 1) -> None:
"""Advance the number of completed steps.

View File

@@ -4,8 +4,11 @@ import pytest
from textual.app import App
from textual.worker import (
DeadlockError,
NoActiveWorker,
Worker,
WorkerCancelled,
WorkerError,
WorkerFailed,
WorkerState,
get_current_worker,
@@ -153,3 +156,61 @@ async def test_get_worker() -> None:
worker._start(app)
assert await worker.wait() is worker
def test_no_active_worker() -> None:
"""No active worker raises a specific exception."""
with pytest.raises(NoActiveWorker):
get_current_worker()
async def test_progress_update():
async def long_work():
pass
app = App()
async with app.run_test():
worker = Worker(app, long_work)
worker._start(app)
worker.update(total_steps=100)
assert worker.progress == 0
worker.advance(50)
assert worker.progress == 50
worker.update(completed_steps=23)
assert worker.progress == 73
async def test_double_start():
async def long_work():
return 0
app = App()
async with app.run_test():
worker = Worker(app, long_work)
worker._start(app)
worker._start(app)
assert await worker.wait() == 0
async def test_self_referential_deadlock():
async def self_referential_work():
await get_current_worker().wait()
app = App()
async with app.run_test():
worker = Worker(app, self_referential_work)
worker._start(app)
with pytest.raises(WorkerFailed) as exc:
await worker.wait()
assert exc.type is DeadlockError
async def test_wait_without_start():
async def work():
return
app = App()
async with app.run_test():
worker = Worker(app, work)
with pytest.raises(WorkerError):
await worker.wait()