| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 74c9ecc commit 08bc708
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,10 +128,11 @@ async def create_mrd( | |||
| 128 | 128 | client: AsyncGrpcClient, | |
| 129 | 129 | bucket_name: str, | |
| 130 | 130 | object_name: str, | |
| 131 | - generation_number: Optional[int] = None, | ||
| 131 | + generation: Optional[int] = None, | ||
| 132 | 132 | read_handle: Optional[_storage_v2.BidiReadHandle] = None, | |
| 133 | 133 | retry_policy: Optional[AsyncRetry] = None, | |
| 134 | 134 | metadata: Optional[List[Tuple[str, str]]] = None, | |
| 135 | + **kwargs, | ||
| 135 | 136 | ) -> AsyncMultiRangeDownloader: | |
| 136 | 137 | """Initializes a MultiRangeDownloader and opens the underlying bidi-gRPC | |
| 137 | 138 | object for reading. | |
@@ -145,8 +146,8 @@ async def create_mrd( | |||
| 145 | 146 | :type object_name: str | |
| 146 | 147 | :param object_name: The name of the object to be read. | |
| 147 | 148 | ||
| 148 | - :type generation_number: int | ||
| 149 | - :param generation_number: (Optional) If present, selects a specific | ||
| 149 | + :type generation: int | ||
| 150 | + :param generation: (Optional) If present, selects a specific | ||
| 150 | 151 | revision of this object. | |
| 151 | 152 | ||
| 152 | 153 | :type read_handle: _storage_v2.BidiReadHandle | |
@@ -162,7 +163,14 @@ async def create_mrd( | |||
| 162 | 163 | :rtype: :class:`~google.cloud.storage.asyncio.async_multi_range_downloader.AsyncMultiRangeDownloader` | |
| 163 | 164 | :returns: An initialized AsyncMultiRangeDownloader instance for reading. | |
| 164 | 165 | """ | |
| 165 | - mrd = cls(client, bucket_name, object_name, generation_number, read_handle) | ||
| 166 | + mrd = cls( | ||
| 167 | + client, | ||
| 168 | + bucket_name, | ||
| 169 | + object_name, | ||
| 170 | + generation=generation, | ||
| 171 | + read_handle=read_handle, | ||
| 172 | + **kwargs, | ||
| 173 | + ) | ||
| 166 | 174 | await mrd.open(retry_policy=retry_policy, metadata=metadata) | |
| 167 | 175 | return mrd | |
| 168 | 176 | ||
@@ -171,8 +179,9 @@ def __init__( | |||
| 171 | 179 | client: AsyncGrpcClient, | |
| 172 | 180 | bucket_name: str, | |
| 173 | 181 | object_name: str, | |
| 174 | - generation_number: Optional[int] = None, | ||
| 182 | + generation: Optional[int] = None, | ||
| 175 | 183 | read_handle: Optional[_storage_v2.BidiReadHandle] = None, | |
| 184 | + **kwargs, | ||
| 176 | 185 | ) -> None: | |
| 177 | 186 | """Constructor for AsyncMultiRangeDownloader, clients are not adviced to | |
| 178 | 187 | use it directly. Instead it's adviced to use the classmethod `create_mrd`. | |
@@ -186,20 +195,27 @@ def __init__( | |||
| 186 | 195 | :type object_name: str | |
| 187 | 196 | :param object_name: The name of the object to be read. | |
| 188 | 197 | ||
| 189 | - :type generation_number: int | ||
| 190 | - :param generation_number: (Optional) If present, selects a specific revision of | ||
| 198 | + :type generation: int | ||
| 199 | + :param generation: (Optional) If present, selects a specific revision of | ||
| 191 | 200 | this object. | |
| 192 | 201 | ||
| 193 | 202 | :type read_handle: _storage_v2.BidiReadHandle | |
| 194 | 203 | :param read_handle: (Optional) An existing read handle. | |
| 195 | 204 | """ | |
| 205 | + if "generation_number" in kwargs: | ||
| 206 | + if generation is not None: | ||
| 207 | + raise TypeError( | ||
| 208 | + "Cannot set both 'generation' and 'generation_number'. " | ||
| 209 | + "Use 'generation' for new code." | ||
| 210 | + ) | ||
| 211 | + generation = kwargs.pop("generation_number") | ||
| 196 | 212 | ||
| 197 | 213 | raise_if_no_fast_crc32c() | |
| 198 | 214 | ||
| 199 | 215 | self.client = client | |
| 200 | 216 | self.bucket_name = bucket_name | |
| 201 | 217 | self.object_name = object_name | |
| 202 | - self.generation_number = generation_number | ||
| 218 | + self.generation = generation | ||
| 203 | 219 | self.read_handle: Optional[_storage_v2.BidiReadHandle] = read_handle | |
| 204 | 220 | self.read_obj_str: Optional[_AsyncReadObjectStream] = None | |
| 205 | 221 | self._is_stream_open: bool = False | |
@@ -276,7 +292,7 @@ async def _do_open(): | |||
| 276 | 292 | client=self.client.grpc_client, | |
| 277 | 293 | bucket_name=self.bucket_name, | |
| 278 | 294 | object_name=self.object_name, | |
| 279 | - generation_number=self.generation_number, | ||
| 295 | + generation_number=self.generation, | ||
| 280 | 296 | read_handle=self.read_handle, | |
| 281 | 297 | ) | |
| 282 | 298 | ||
@@ -291,7 +307,7 @@ async def _do_open(): | |||
| 291 | 307 | ) | |
| 292 | 308 | ||
| 293 | 309 | if self.read_obj_str.generation_number: | |
| 294 | - self.generation_number = self.read_obj_str.generation_number | ||
| 310 | + self.generation = self.read_obj_str.generation_number | ||
| 295 | 311 | if self.read_obj_str.read_handle: | |
| 296 | 312 | self.read_handle = self.read_obj_str.read_handle | |
| 297 | 313 | if self.read_obj_str.persisted_size is not None: | |
@@ -435,7 +451,7 @@ async def generator(): | |||
| 435 | 451 | client=self.client.grpc_client, | |
| 436 | 452 | bucket_name=self.bucket_name, | |
| 437 | 453 | object_name=self.object_name, | |
| 438 | - generation_number=self.generation_number, | ||
| 454 | + generation_number=self.generation, | ||
| 439 | 455 | read_handle=current_handle, | |
| 440 | 456 | ) | |
| 441 | 457 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -264,7 +264,8 @@ async def _run(): | |||
| 264 | 264 | ||
| 265 | 265 | event_loop.run_until_complete(_run()) | |
| 266 | 266 | ||
| 267 | - @pytest.mark.skip(reason='Flaky test b/478129078') | ||
| 267 | + | ||
| 268 | + @pytest.mark.skip(reason="Flaky test b/478129078") | ||
| 268 | 269 | def test_mrd_open_with_read_handle(event_loop, grpc_client_direct): | |
| 269 | 270 | object_name = f"test_read_handl-{str(uuid.uuid4())[:4]}" | |
| 270 | 271 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,9 +27,7 @@ | |||
| 27 | 27 | from google.cloud._storage_v2.types.storage import BidiReadObjectRedirectedError | |
| 28 | 28 | ||
| 29 | 29 | _READ_ID = 1 | |
| 30 | - LOGGER_NAME = ( | ||
| 31 | - "google.cloud.storage.asyncio.retry.reads_resumption_strategy" | ||
| 32 | - ) | ||
| 30 | + LOGGER_NAME = "google.cloud.storage.asyncio.retry.reads_resumption_strategy" | ||
| 33 | 31 | ||
| 34 | 32 | ||
| 35 | 33 | class TestDownloadState(unittest.TestCase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,7 +49,7 @@ async def _make_mock_mrd( | |||
| 49 | 49 | mock_cls_async_read_object_stream, | |
| 50 | 50 | bucket_name=_TEST_BUCKET_NAME, | |
| 51 | 51 | object_name=_TEST_OBJECT_NAME, | |
| 52 | - generation_number=_TEST_GENERATION_NUMBER, | ||
| 52 | + generation=_TEST_GENERATION_NUMBER, | ||
| 53 | 53 | read_handle=_TEST_READ_HANDLE, | |
| 54 | 54 | ): | |
| 55 | 55 | mock_client = mock.MagicMock() | |
@@ -62,7 +62,7 @@ async def _make_mock_mrd( | |||
| 62 | 62 | mock_stream.read_handle = _TEST_READ_HANDLE | |
| 63 | 63 | ||
| 64 | 64 | mrd = await AsyncMultiRangeDownloader.create_mrd( | |
| 65 | - mock_client, bucket_name, object_name, generation_number, read_handle | ||
| 65 | + mock_client, bucket_name, object_name, generation, read_handle | ||
| 66 | 66 | ) | |
| 67 | 67 | ||
| 68 | 68 | return mrd, mock_client | |
@@ -89,7 +89,7 @@ async def test_create_mrd(self, mock_cls_async_read_object_stream): | |||
| 89 | 89 | assert mrd.client == mock_client | |
| 90 | 90 | assert mrd.bucket_name == _TEST_BUCKET_NAME | |
| 91 | 91 | assert mrd.object_name == _TEST_OBJECT_NAME | |
| 92 | - assert mrd.generation_number == _TEST_GENERATION_NUMBER | ||
| 92 | + assert mrd.generation == _TEST_GENERATION_NUMBER | ||
| 93 | 93 | assert mrd.read_handle == _TEST_READ_HANDLE | |
| 94 | 94 | assert mrd.persisted_size == _TEST_OBJECT_SIZE | |
| 95 | 95 | assert mrd.is_stream_open | |
@@ -303,9 +303,7 @@ async def test_downloading_without_opening_should_throw_error(self): | |||
| 303 | 303 | assert not mrd.is_stream_open | |
| 304 | 304 | ||
| 305 | 305 | @mock.patch("google.cloud.storage.asyncio._utils.google_crc32c") | |
| 306 | - def test_init_raises_if_crc32c_c_extension_is_missing( | ||
| 307 | - self, mock_google_crc32c | ||
| 308 | - ): | ||
| 306 | + def test_init_raises_if_crc32c_c_extension_is_missing(self, mock_google_crc32c): | ||
| 309 | 307 | mock_google_crc32c.implementation = "python" | |
| 310 | 308 | mock_client = mock.MagicMock() | |
| 311 | 309 | ||
@@ -317,9 +315,7 @@ def test_init_raises_if_crc32c_c_extension_is_missing( | |||
| 317 | 315 | ) | |
| 318 | 316 | ||
| 319 | 317 | @pytest.mark.asyncio | |
| 320 | - @mock.patch( | ||
| 321 | - "google.cloud.storage.asyncio.retry.reads_resumption_strategy.Checksum" | ||
| 322 | - ) | ||
| 318 | + @mock.patch("google.cloud.storage.asyncio.retry.reads_resumption_strategy.Checksum") | ||
| 323 | 319 | async def test_download_ranges_raises_on_checksum_mismatch( | |
| 324 | 320 | self, mock_checksum_class | |
| 325 | 321 | ): | |
@@ -405,3 +401,47 @@ async def close_side_effect(): | |||
| 405 | 401 | ||
| 406 | 402 | mock_close.assert_called_once() | |
| 407 | 403 | assert not mrd.is_stream_open | |
| 404 | + | ||
| 405 | + @mock.patch( | ||
| 406 | + "google.cloud.storage.asyncio.async_multi_range_downloader._AsyncReadObjectStream" | ||
| 407 | + ) | ||
| 408 | + @pytest.mark.asyncio | ||
| 409 | + async def test_create_mrd_with_generation_number( | ||
| 410 | + self, mock_cls_async_read_object_stream | ||
| 411 | + ): | ||
| 412 | + # Arrange | ||
| 413 | + mock_client = mock.MagicMock() | ||
| 414 | + mock_client.grpc_client = mock.AsyncMock() | ||
| 415 | + | ||
| 416 | + mock_stream = mock_cls_async_read_object_stream.return_value | ||
| 417 | + mock_stream.open = AsyncMock() | ||
| 418 | + mock_stream.generation_number = _TEST_GENERATION_NUMBER | ||
| 419 | + mock_stream.persisted_size = _TEST_OBJECT_SIZE | ||
| 420 | + mock_stream.read_handle = _TEST_READ_HANDLE | ||
| 421 | + | ||
| 422 | + # Act | ||
| 423 | + mrd = await AsyncMultiRangeDownloader.create_mrd( | ||
| 424 | + mock_client, | ||
| 425 | + _TEST_BUCKET_NAME, | ||
| 426 | + _TEST_OBJECT_NAME, | ||
| 427 | + generation_number=_TEST_GENERATION_NUMBER, | ||
| 428 | + read_handle=_TEST_READ_HANDLE, | ||
| 429 | + ) | ||
| 430 | + | ||
| 431 | + # Assert | ||
| 432 | + assert mrd.generation == _TEST_GENERATION_NUMBER | ||
| 433 | + | ||
| 434 | + @pytest.mark.asyncio | ||
| 435 | + async def test_create_mrd_with_both_generation_and_generation_number(self): | ||
| 436 | + # Arrange | ||
| 437 | + mock_client = mock.MagicMock() | ||
| 438 | + | ||
| 439 | + # Act & Assert | ||
| 440 | + with pytest.raises(TypeError): | ||
| 441 | + await AsyncMultiRangeDownloader.create_mrd( | ||
| 442 | + mock_client, | ||
| 443 | + _TEST_BUCKET_NAME, | ||
| 444 | + _TEST_OBJECT_NAME, | ||
| 445 | + generation=_TEST_GENERATION_NUMBER, | ||
| 446 | + generation_number=_TEST_GENERATION_NUMBER, | ||
| 447 | + ) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,9 +79,7 @@ async def instantiate_read_obj_stream_with_read_handle( | |||
| 79 | 79 | return read_obj_stream | |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | - @mock.patch( | ||
| 83 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 84 | - ) | ||
| 82 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 85 | 83 | @mock.patch( | |
| 86 | 84 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 87 | 85 | ) | |
@@ -110,9 +108,7 @@ def test_init_with_bucket_object_generation(mock_client, mock_async_bidi_rpc): | |||
| 110 | 108 | assert read_obj_stream.rpc == rpc_sentinel | |
| 111 | 109 | ||
| 112 | 110 | ||
| 113 | - @mock.patch( | ||
| 114 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 115 | - ) | ||
| 111 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 116 | 112 | @mock.patch( | |
| 117 | 113 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 118 | 114 | ) | |
@@ -136,9 +132,7 @@ async def test_open(mock_client, mock_cls_async_bidi_rpc): | |||
| 136 | 132 | assert read_obj_stream.is_stream_open | |
| 137 | 133 | ||
| 138 | 134 | ||
| 139 | - @mock.patch( | ||
| 140 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 141 | - ) | ||
| 135 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 142 | 136 | @mock.patch( | |
| 143 | 137 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 144 | 138 | ) | |
@@ -162,9 +156,7 @@ async def test_open_with_read_handle(mock_client, mock_cls_async_bidi_rpc): | |||
| 162 | 156 | assert read_obj_stream.is_stream_open | |
| 163 | 157 | ||
| 164 | 158 | ||
| 165 | - @mock.patch( | ||
| 166 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 167 | - ) | ||
| 159 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 168 | 160 | @mock.patch( | |
| 169 | 161 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 170 | 162 | ) | |
@@ -185,9 +177,7 @@ async def test_open_when_already_open_should_raise_error( | |||
| 185 | 177 | assert str(exc.value) == "Stream is already open" | |
| 186 | 178 | ||
| 187 | 179 | ||
| 188 | - @mock.patch( | ||
| 189 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 190 | - ) | ||
| 180 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 191 | 181 | @mock.patch( | |
| 192 | 182 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 193 | 183 | ) | |
@@ -208,9 +198,7 @@ async def test_close(mock_client, mock_cls_async_bidi_rpc): | |||
| 208 | 198 | assert not read_obj_stream.is_stream_open | |
| 209 | 199 | ||
| 210 | 200 | ||
| 211 | - @mock.patch( | ||
| 212 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 213 | - ) | ||
| 201 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 214 | 202 | @mock.patch( | |
| 215 | 203 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 216 | 204 | ) | |
@@ -232,9 +220,7 @@ async def test_requests_done(mock_client, mock_cls_async_bidi_rpc): | |||
| 232 | 220 | read_obj_stream.socket_like_rpc.recv.assert_called_once() | |
| 233 | 221 | ||
| 234 | 222 | ||
| 235 | - @mock.patch( | ||
| 236 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 237 | - ) | ||
| 223 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 238 | 224 | @mock.patch( | |
| 239 | 225 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 240 | 226 | ) | |
@@ -255,9 +241,7 @@ async def test_close_without_open_should_raise_error( | |||
| 255 | 241 | assert str(exc.value) == "Stream is not open" | |
| 256 | 242 | ||
| 257 | 243 | ||
| 258 | - @mock.patch( | ||
| 259 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 260 | - ) | ||
| 244 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 261 | 245 | @mock.patch( | |
| 262 | 246 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 263 | 247 | ) | |
@@ -278,9 +262,7 @@ async def test_send(mock_client, mock_cls_async_bidi_rpc): | |||
| 278 | 262 | ) | |
| 279 | 263 | ||
| 280 | 264 | ||
| 281 | - @mock.patch( | ||
| 282 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 283 | - ) | ||
| 265 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 284 | 266 | @mock.patch( | |
| 285 | 267 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 286 | 268 | ) | |
@@ -301,9 +283,7 @@ async def test_send_without_open_should_raise_error( | |||
| 301 | 283 | assert str(exc.value) == "Stream is not open" | |
| 302 | 284 | ||
| 303 | 285 | ||
| 304 | - @mock.patch( | ||
| 305 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 306 | - ) | ||
| 286 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 307 | 287 | @mock.patch( | |
| 308 | 288 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 309 | 289 | ) | |
@@ -326,9 +306,7 @@ async def test_recv(mock_client, mock_cls_async_bidi_rpc): | |||
| 326 | 306 | assert response == bidi_read_object_response | |
| 327 | 307 | ||
| 328 | 308 | ||
| 329 | - @mock.patch( | ||
| 330 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 331 | - ) | ||
| 309 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 332 | 310 | @mock.patch( | |
| 333 | 311 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 334 | 312 | ) | |
@@ -349,9 +327,7 @@ async def test_recv_without_open_should_raise_error( | |||
| 349 | 327 | assert str(exc.value) == "Stream is not open" | |
| 350 | 328 | ||
| 351 | 329 | ||
| 352 | - @mock.patch( | ||
| 353 | - "google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc" | ||
| 354 | - ) | ||
| 330 | + @mock.patch("google.cloud.storage.asyncio.async_read_object_stream.AsyncBidiRpc") | ||
| 355 | 331 | @mock.patch( | |
| 356 | 332 | "google.cloud.storage.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client" | |
| 357 | 333 | ) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments