Skip to content

Commit

Permalink
Add unit tests for async setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Aron Carroll committed Dec 12, 2024
1 parent f2724cd commit 9ae118a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/tests/server/fixtures/setup_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Predictor:
async def download(self) -> None:
print("download complete!")

async def setup(self) -> None:
print("setup starting...")
await self.download()
print("setup complete!")

async def predict(self) -> str:
print("running prediction")
return "output"
9 changes: 9 additions & 0 deletions python/tests/server/fixtures/setup_async_with_sync_predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Predictor:
async def download(self) -> None:
print("setup used asyncio.run! it's not very effective...")

async def setup(self) -> None:
await self.download()

def predict(self) -> str:
return "output"
51 changes: 51 additions & 0 deletions python/tests/server/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,57 @@ def test_async_predictor_on_python_3_10_or_older_raises_error(worker):
)


@uses_worker(
"setup_async", max_concurrency=1, min_python=(3, 11), is_async=True, setup=False
)
def test_setup_async(worker: Worker):
fut = worker.setup()
setup_result = Result()
setup_sid = worker.subscribe(setup_result.handle_event)

# with pytest.raises(FatalWorkerException):
fut.result()
worker.unsubscribe(setup_sid)

assert setup_result.stdout_lines == [
"setup starting...\n",
"download complete!\n",
"setup complete!\n",
]

predict_result = Result()
predict_sid = worker.subscribe(predict_result.handle_event, tag="p1")
worker.predict({}, tag="p1").result()

assert predict_result.done
assert predict_result.output == "output"
assert predict_result.stdout_lines == ["running prediction\n"]

worker.unsubscribe(predict_sid)


@uses_worker(
"setup_async_with_sync_predict",
max_concurrency=1,
min_python=(3, 11),
is_async=False,
setup=False,
)
def test_setup_async_with_sync_predict_raises_error(worker: Worker):
fut = worker.setup()
result = Result()
worker.subscribe(result.handle_event)

with pytest.raises(FatalWorkerException):
fut.result()
assert result.done
assert result.done.error
assert (
result.done.error_detail
== "Invalid predictor: to use an async setup method you must use an async predict method"
)


@uses_worker("simple", max_concurrency=5, setup=False)
def test_concurrency_with_sync_predictor_raises_error(worker):
fut = worker.setup()
Expand Down

0 comments on commit 9ae118a

Please sign in to comment.