Skip to content

Commit

Permalink
Add tenacity for polling (#260)
Browse files Browse the repository at this point in the history
* Add tenacity for polling
  • Loading branch information
kharus authored Jan 22, 2025
1 parent 15d0456 commit cf9de66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion natural4-server/natural4_server/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ async def petri_post_process(target_folder):
# stem is respectively a timestamp 2025-01-06T03:00:52
timestamp = (await dot_path.readlink()).stem

flowchart_tasks: asyncio.Task[None] = cyz.pipe(get_flowchart_tasks(target_folder, timestamp), run_tasks)
flowchart_tasks: asyncio.Task[None] = cyz.pipe(
get_flowchart_tasks(target_folder, timestamp), run_tasks)

return timestamp, flowchart_tasks

Expand Down
1 change: 1 addition & 0 deletions natural4-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dev-dependencies = [
"pre-commit<4.0.0,>=3.6.2",
"coverage<8.0.0,>=7.4.3",
"pytest-asyncio>=0.24.0",
"tenacity>=9.0.0",
]

[tool.ruff]
Expand Down
39 changes: 19 additions & 20 deletions natural4-server/tests/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from time import sleep, time
from time import sleep

from sanic import Sanic
from sanic.application.constants import ServerStage
from sanic_testing.reusable import ReusableClient
from tenacity import retry, retry_if_result, stop_after_delay, wait_fixed


def is_not_200(value):
"""Return True if value is None"""
return value != 200

@retry(retry=(retry_if_result(is_not_200)), wait=wait_fixed(5), stop=stop_after_delay(30))
def poll_url(client, url):
print(f"Waiting for {url}...")
request, response = client.get(url)
return response.status


def test_post(app: Sanic, post_data):
Expand Down Expand Up @@ -32,22 +44,9 @@ def test_post(app: Sanic, post_data):
request, response_json = client.get(f"{workdir_url}/petri/LATEST-small.png")
assert response_json.status == 200

start_time = time()
while time() - start_time < 60:
print("Waiting for PDF...")
request, response_pdf = client.get(f"{workdir_url}/docx/LATEST.docx")
if response_pdf.status != 200:
sleep(5)
else:
break

start_time = time()
while time() - start_time < 60:
print("Waiting for PDF...")
request, response_pdf = client.get(f"{workdir_url}/pdf/LATEST.pdf")
if response_pdf.status != 200:
sleep(5)
else:
break

assert response_pdf.status == 200
response_docx = poll_url(client, f"{workdir_url}/docx/LATEST.docx")
assert response_docx == 200

response_pdf = poll_url(client, f"{workdir_url}/pdf/LATEST.pdf")

assert response_pdf == 200
11 changes: 11 additions & 0 deletions natural4-server/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf9de66

Please sign in to comment.