-
Notifications
You must be signed in to change notification settings - Fork 10
resource delay in db : Async wait for [Cancelled] #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ab9c043
async wait for
nishika26 e780e7b
along with test cases and installment
nishika26 b548260
test cases
nishika26 e318a9d
Merge branch 'main' into bug/delay_response
nishika26 a5ad26a
logging
nishika26 06b36a2
Merge branch 'main' into bug/delay_response
nishika26 e8ca356
ci checks fix
nishika26 5d8aee4
calculating file size
nishika26 486b400
storage
nishika26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
backend/app/tests/api/routes/collections/test_create_collections.py
This file contains hidden or 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,112 @@ | ||
| import pytest | ||
| import asyncio | ||
| import io | ||
| from openai import OpenAIError | ||
| import openai_responses | ||
| from uuid import UUID | ||
| from httpx import AsyncClient | ||
| from sqlmodel import Session | ||
| from app.core.config import settings | ||
| from app.tests.utils.document import DocumentStore | ||
| from app.tests.utils.utils import openai_credentials | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_s3(monkeypatch): | ||
| class FakeStorage: | ||
| def __init__(self, *args, **kwargs): | ||
| pass | ||
|
|
||
| def upload(self, file_obj, path: str, **kwargs): | ||
| return f"s3://fake-bucket/{path or 'mock-file.txt'}" | ||
|
|
||
| def stream(self, file_obj): | ||
| fake_file = io.BytesIO(b"dummy content") | ||
| fake_file.name = "fake.txt" | ||
| return fake_file | ||
|
|
||
| def get_file_size_kb(self, url: str) -> float: | ||
| return 1.0 # Simulate 1KB files | ||
|
|
||
| class FakeS3Client: | ||
| def head_object(self, Bucket, Key): | ||
| return {"ContentLength": 1024} | ||
|
|
||
| monkeypatch.setattr("app.api.routes.collections.AmazonCloudStorage", FakeStorage) | ||
| monkeypatch.setattr("boto3.client", lambda service: FakeS3Client()) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("openai_credentials") | ||
| class TestCollectionRouteCreate: | ||
| _n_documents = 5 | ||
|
|
||
| @pytest.mark.asyncio | ||
| @openai_responses.mock() | ||
| async def test_create_collection_success( | ||
| self, | ||
| async_client: AsyncClient, | ||
| db: Session, | ||
| superuser_token_headers: dict[str, str], | ||
| ): | ||
| store = DocumentStore(db) | ||
| documents = store.fill(self._n_documents) | ||
| doc_ids = [str(doc.id) for doc in documents] | ||
|
|
||
| body = { | ||
| "documents": doc_ids, | ||
| "batch_size": 2, | ||
| "model": "gpt-4o", | ||
| "instructions": "Test collection assistant.", | ||
| "temperature": 0.1, | ||
| } | ||
|
|
||
| response = await async_client.post( | ||
| f"{settings.API_V1_STR}/collections/create", | ||
| json=body, | ||
| headers=superuser_token_headers, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| json = response.json() | ||
| assert json["success"] is True | ||
| metadata = json.get("metadata", {}) | ||
| assert metadata["status"] == "processing" | ||
| assert UUID(metadata["key"]) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_collection_timeout( | ||
| self, | ||
| async_client: AsyncClient, | ||
| db: Session, | ||
| superuser_token_headers: dict[str, str], | ||
| monkeypatch, | ||
| ): | ||
| async def long_task(*args, **kwargs): | ||
| await asyncio.sleep(30) # exceed timeout | ||
| return None | ||
|
|
||
| monkeypatch.setattr( | ||
| "app.api.routes.collections.do_create_collection", # adjust if necessary | ||
| long_task, | ||
| ) | ||
|
|
||
| body = { | ||
| "documents": [], | ||
| "batch_size": 1, | ||
| "model": "gpt-4o", | ||
| "instructions": "Slow task", | ||
| "temperature": 0.2, | ||
| } | ||
|
|
||
| response = await async_client.post( | ||
| f"{settings.API_V1_STR}/collections/create", | ||
| json=body, | ||
| headers=superuser_token_headers, | ||
| ) | ||
|
|
||
| assert response.status_code == 408 | ||
| json = response.json() | ||
| assert json["success"] is False | ||
| assert json["data"] is None | ||
| assert json["error"] == "The task timed out." | ||
| assert json["metadata"] is None | ||
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason it is here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving it for any future reference, although it is self explanatory but still just in case