-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce proper download simulation for the user fixture (#3689)
* check outbox messages (see #3686) * allow user fixture to test file downloads * introduce proper download simulation * update docs * code review * use constant for HTTP 200 --------- Co-authored-by: Falko Schindler <falko@zauberzeug.com>
- Loading branch information
1 parent
e27064e
commit 2934267
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import time | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any, List, Optional, Union | ||
|
||
import httpx | ||
|
||
from .. import background_tasks | ||
|
||
if TYPE_CHECKING: | ||
from .user import User | ||
|
||
|
||
class UserDownload: | ||
|
||
def __init__(self, user: User) -> None: | ||
self.http_responses: List[httpx.Response] = [] | ||
self.user = user | ||
|
||
def __call__(self, src: Union[str, Path, bytes], filename: Optional[str] = None, media_type: str = '') -> Any: | ||
background_tasks.create(self._get(src)) | ||
|
||
async def _get(self, src: Union[str, Path, bytes]) -> None: | ||
if isinstance(src, bytes): | ||
await asyncio.sleep(0) | ||
response = httpx.Response(httpx.codes.OK, content=src) | ||
else: | ||
response = await self.user.http_client.get(str(src)) | ||
self.http_responses.append(response) | ||
|
||
async def next(self, *, timeout: float = 1.0) -> httpx.Response: | ||
"""Wait for a new download to happen. | ||
:param timeout: the maximum time to wait (default: 1.0) | ||
:returns: the HTTP response | ||
""" | ||
assert self.user.client | ||
downloads = len(self.http_responses) | ||
deadline = time.time() + timeout | ||
while len(self.http_responses) < downloads + 1: | ||
await asyncio.sleep(0.1) | ||
if time.time() > deadline: | ||
raise TimeoutError('Download did not happen') | ||
return self.http_responses[-1] |
This file contains 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 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