This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
feat: add preconditions and retry configuration to blob.create_resumable_upload_session #484
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import pytest | ||
| import six | ||
| from six.moves import http_client | ||
| from six.moves.urllib.parse import urlencode | ||
|
|
||
| from google.cloud.storage.retry import DEFAULT_RETRY | ||
| from google.cloud.storage.retry import DEFAULT_RETRY_IF_ETAG_IN_JSON | ||
|
|
@@ -2041,8 +2042,6 @@ def _do_multipart_success( | |
| mtls=False, | ||
| retry=None, | ||
| ): | ||
| from six.moves.urllib.parse import urlencode | ||
|
|
||
| bucket = _Bucket(name="w00t", user_project=user_project) | ||
| blob = self._make_one(u"blob-name", bucket=bucket, kms_key_name=kms_key_name) | ||
| self.assertIsNone(blob.chunk_size) | ||
|
|
@@ -2286,7 +2285,6 @@ def _initiate_resumable_helper( | |
| mtls=False, | ||
| retry=None, | ||
| ): | ||
| from six.moves.urllib.parse import urlencode | ||
| from google.resumable_media.requests import ResumableUpload | ||
| from google.cloud.storage.blob import _DEFAULT_CHUNKSIZE | ||
|
|
||
|
|
@@ -3248,7 +3246,15 @@ def test_upload_from_string_w_text_w_num_retries(self): | |
| self._upload_from_string_helper(data, num_retries=2) | ||
|
|
||
| def _create_resumable_upload_session_helper( | ||
| self, origin=None, side_effect=None, timeout=None | ||
| self, | ||
| origin=None, | ||
| side_effect=None, | ||
| timeout=None, | ||
| if_generation_match=None, | ||
| if_generation_not_match=None, | ||
| if_metageneration_match=None, | ||
| if_metageneration_not_match=None, | ||
| retry=None, | ||
| ): | ||
| bucket = _Bucket(name="alex-trebek") | ||
| blob = self._make_one("blob-name", bucket=bucket) | ||
|
|
@@ -3280,6 +3286,11 @@ def _create_resumable_upload_session_helper( | |
| size=size, | ||
| origin=origin, | ||
| client=client, | ||
| if_generation_match=if_generation_match, | ||
| if_generation_not_match=if_generation_not_match, | ||
| if_metageneration_match=if_metageneration_match, | ||
| if_metageneration_not_match=if_metageneration_not_match, | ||
| retry=retry, | ||
| **timeout_kwarg | ||
| ) | ||
|
|
||
|
|
@@ -3289,10 +3300,23 @@ def _create_resumable_upload_session_helper( | |
|
|
||
| # Check the mocks. | ||
| upload_url = ( | ||
| "https://storage.googleapis.com/upload/storage/v1" | ||
| + bucket.path | ||
| + "/o?uploadType=resumable" | ||
| "https://storage.googleapis.com/upload/storage/v1" + bucket.path + "/o" | ||
| ) | ||
|
|
||
| qs_params = [("uploadType", "resumable")] | ||
| if if_generation_match is not None: | ||
| qs_params.append(("ifGenerationMatch", if_generation_match)) | ||
|
|
||
| if if_generation_not_match is not None: | ||
| qs_params.append(("ifGenerationNotMatch", if_generation_not_match)) | ||
|
|
||
| if if_metageneration_match is not None: | ||
| qs_params.append(("ifMetagenerationMatch", if_metageneration_match)) | ||
|
|
||
| if if_metageneration_not_match is not None: | ||
| qs_params.append(("ifMetaGenerationNotMatch", if_metageneration_not_match)) | ||
|
|
||
| upload_url += "?" + urlencode(qs_params) | ||
| payload = b'{"name": "blob-name"}' | ||
| expected_headers = { | ||
| "content-type": "application/json; charset=UTF-8", | ||
|
|
@@ -3318,6 +3342,26 @@ def test_create_resumable_upload_session_with_custom_timeout(self): | |
| def test_create_resumable_upload_session_with_origin(self): | ||
| self._create_resumable_upload_session_helper(origin="http://google.com") | ||
|
|
||
| def test_create_resumable_upload_session_with_generation_match(self): | ||
| self._create_resumable_upload_session_helper( | ||
| if_generation_match=123456, if_metageneration_match=2 | ||
| ) | ||
|
|
||
| def test_create_resumable_upload_session_with_generation_not_match(self): | ||
| self._create_resumable_upload_session_helper( | ||
| if_generation_not_match=0, if_metageneration_not_match=3 | ||
| ) | ||
|
|
||
| def test_create_resumable_upload_session_with_conditional_retry_success(self): | ||
| self._create_resumable_upload_session_helper( | ||
| retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED, if_generation_match=123456 | ||
| ) | ||
|
|
||
| def test_create_resumable_upload_session_with_conditional_retry_failure(self): | ||
| self._create_resumable_upload_session_helper( | ||
| retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this test verify that retries don't happen?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tritone, I've tested the retry behaviors against the emulator that things are working as intended. The unit test asserts if the mock call was made with the corresponding url and query params.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! |
||
|
|
||
| def test_create_resumable_upload_session_with_failure(self): | ||
| from google.resumable_media import InvalidResponse | ||
| from google.cloud import exceptions | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.