Repository navigation
refactor!: take request lock durations in seconds #1199
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
| from werkzeug import Request, Response | ||
|
|
||
| from apify_client import ApifyClient, ApifyClientAsync | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| from pytest_httpserver import HTTPServer | ||
|
|
||
| pytestmark = pytest.mark.usefixtures('http_client_classes') | ||
|
|
||
| _QUEUE_ID = 'test-queue-id' | ||
| _REQUEST_ID = 'test-request-id' | ||
| _HEAD_LOCK_PATH = f'/v2/request-queues/{_QUEUE_ID}/head/lock' | ||
| _REQUEST_LOCK_PATH = f'/v2/request-queues/{_QUEUE_ID}/requests/{_REQUEST_ID}/lock' | ||
| _HEAD_LOCK_RESPONSE = { | ||
| 'data': { | ||
| 'limit': 1, | ||
| 'queueModifiedAt': '2024-01-01T00:00:00.000Z', | ||
| 'hadMultipleClients': False, | ||
| 'lockSecs': 60, | ||
| 'queueHasLockedRequests': True, | ||
| 'clientKey': 'test-client-key', | ||
| 'items': [], | ||
| } | ||
| } | ||
| _REQUEST_LOCK_RESPONSE = {'data': {'lockExpiresAt': '2024-01-01T00:01:00.000Z'}} | ||
|
|
||
|
|
||
| def _capture_into(captured_requests: list[Request], body: dict) -> Callable[[Request], Response]: | ||
| def handler(request: Request) -> Response: | ||
| captured_requests.append(request) | ||
| return Response(status=200, response=json.dumps(body), mimetype='application/json') | ||
|
|
||
| return handler | ||
|
|
||
|
|
||
| def test_list_and_lock_head_sends_lock_secs_sync(httpserver: HTTPServer) -> None: | ||
| """`list_and_lock_head()` sends the lock length unchanged as the `lockSecs` query parameter.""" | ||
| captured_requests: list[Request] = [] | ||
| httpserver.expect_request(_HEAD_LOCK_PATH, method='POST').respond_with_handler( | ||
| _capture_into(captured_requests, _HEAD_LOCK_RESPONSE) | ||
| ) | ||
| client = ApifyClient(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) | ||
|
|
||
| head = client.request_queue(_QUEUE_ID).list_and_lock_head(lock_secs=60, limit=1) | ||
|
|
||
| assert head.lock_secs == 60 | ||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].args['lockSecs'] == '60' | ||
| assert captured_requests[0].args['limit'] == '1' | ||
|
|
||
|
|
||
| async def test_list_and_lock_head_sends_lock_secs_async(httpserver: HTTPServer) -> None: | ||
| """Async variant of `test_list_and_lock_head_sends_lock_secs_sync`.""" | ||
| captured_requests: list[Request] = [] | ||
| httpserver.expect_request(_HEAD_LOCK_PATH, method='POST').respond_with_handler( | ||
| _capture_into(captured_requests, _HEAD_LOCK_RESPONSE) | ||
| ) | ||
| client = ApifyClientAsync(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) | ||
|
|
||
| head = await client.request_queue(_QUEUE_ID).list_and_lock_head(lock_secs=60, limit=1) | ||
|
|
||
| assert head.lock_secs == 60 | ||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].args['lockSecs'] == '60' | ||
| assert captured_requests[0].args['limit'] == '1' | ||
|
|
||
|
|
||
| def test_prolong_request_lock_sends_lock_secs_sync(httpserver: HTTPServer) -> None: | ||
| """`prolong_request_lock()` sends the lock length unchanged as the `lockSecs` query parameter.""" | ||
| captured_requests: list[Request] = [] | ||
| httpserver.expect_request(_REQUEST_LOCK_PATH, method='PUT').respond_with_handler( | ||
| _capture_into(captured_requests, _REQUEST_LOCK_RESPONSE) | ||
| ) | ||
| client = ApifyClient(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) | ||
|
|
||
| lock_info = client.request_queue(_QUEUE_ID).prolong_request_lock(_REQUEST_ID, lock_secs=120, forefront=True) | ||
|
|
||
| assert lock_info is not None | ||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].args['lockSecs'] == '120' | ||
| assert captured_requests[0].args['forefront'] == 'true' | ||
|
|
||
|
|
||
| async def test_prolong_request_lock_sends_lock_secs_async(httpserver: HTTPServer) -> None: | ||
| """Async variant of `test_prolong_request_lock_sends_lock_secs_sync`.""" | ||
| captured_requests: list[Request] = [] | ||
| httpserver.expect_request(_REQUEST_LOCK_PATH, method='PUT').respond_with_handler( | ||
| _capture_into(captured_requests, _REQUEST_LOCK_RESPONSE) | ||
| ) | ||
| client = ApifyClientAsync(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) | ||
|
|
||
| lock_info = await client.request_queue(_QUEUE_ID).prolong_request_lock(_REQUEST_ID, lock_secs=120, forefront=True) | ||
|
|
||
| assert lock_info is not None | ||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].args['lockSecs'] == '120' | ||
| assert captured_requests[0].args['forefront'] == 'true' |
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.
Note: the Python SDK still calls
list_and_lock_head(lock_duration=...)andprolong_request_lock(..., lock_duration=...)with atimedelta, so on this client version every shared request-queue fetch fails withTypeError: unexpected keyword argument. The SDK has to switch tolock_secs=int(...total_seconds())in step with this release. (apify-sdk-python src/apify/storage_clients/_apify/_request_queue_shared_client.py:659)