|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import io |
| 16 | +import os |
16 | 17 | import pytest |
17 | 18 |
|
18 | 19 | from google.api_core import exceptions |
|
21 | 22 | ResumableUploadConfig, |
22 | 23 | ResumableUploadSession, |
23 | 24 | ) |
24 | | -from google.showcase import UploadMediaResponse |
| 25 | +from google.showcase import UploadMediaRequest, UploadMediaResponse |
25 | 26 |
|
26 | 27 | from conftest import resume_resumable_upload |
27 | 28 |
|
@@ -500,3 +501,110 @@ class _ClientPauseError(Exception): |
500 | 501 | assert isinstance(session2.response, UploadMediaResponse) |
501 | 502 | assert session2.response.name == "golden_user_resume.mp4" |
502 | 503 | assert session2.response.size == total_size |
| 504 | + |
| 505 | + |
| 506 | +if os.environ.get("GAPIC_PYTHON_ASYNC", "true") == "true": |
| 507 | + |
| 508 | + @pytest.mark.asyncio |
| 509 | + async def test_async_client_upload_media_resume_direct( |
| 510 | + intercepted_resumable_upload_rest_async, |
| 511 | + ): |
| 512 | + """Verify direct async resumption via ResumableUploadServiceAsyncClient.""" |
| 513 | + client, _ = intercepted_resumable_upload_rest_async |
| 514 | + total_size = 1_500_000 |
| 515 | + chunk_size = 524_288 # 512 KiB |
| 516 | + data = b"R" * total_size |
| 517 | + stream = io.BytesIO(data) |
| 518 | + |
| 519 | + class _ClientPauseError(Exception): |
| 520 | + pass |
| 521 | + |
| 522 | + session1 = await client.upload_media( |
| 523 | + request=UploadMediaRequest(name="async_resume_direct.txt"), |
| 524 | + config=ResumableUploadConfig(chunk_size=chunk_size), |
| 525 | + ) |
| 526 | + with pytest.raises(_ClientPauseError): |
| 527 | + async for progress in session1.upload(stream=stream, size=total_size): |
| 528 | + if progress.bytes_uploaded >= chunk_size: |
| 529 | + raise _ClientPauseError("Simulated user pause after first chunk") |
| 530 | + |
| 531 | + saved_url = session1.upload_url |
| 532 | + saved_chunk_size = session1.chunk_size |
| 533 | + assert saved_url is not None |
| 534 | + assert saved_chunk_size == chunk_size |
| 535 | + |
| 536 | + stream.seek(0) |
| 537 | + session2 = await client.upload_media( |
| 538 | + request=UploadMediaRequest(name="async_resume_direct.txt"), |
| 539 | + config=ResumableUploadConfig(chunk_size=saved_chunk_size), |
| 540 | + ) |
| 541 | + response = await session2.resume( |
| 542 | + upload_url=saved_url, |
| 543 | + stream=stream, |
| 544 | + size=total_size, |
| 545 | + chunk_size=saved_chunk_size, |
| 546 | + ) |
| 547 | + |
| 548 | + assert session2.finished |
| 549 | + assert isinstance(response, UploadMediaResponse) |
| 550 | + assert response.name == "async_resume_direct.txt" |
| 551 | + assert response.size == total_size |
| 552 | + |
| 553 | + @pytest.mark.asyncio |
| 554 | + async def test_async_client_upload_media_resume_seekable( |
| 555 | + intercepted_resumable_upload_rest_async, |
| 556 | + ): |
| 557 | + """Verify async progress iteration during resumption via ResumableUploadServiceAsyncClient.""" |
| 558 | + client, _ = intercepted_resumable_upload_rest_async |
| 559 | + total_size = 1_500_000 |
| 560 | + chunk_size = 524_288 # 512 KiB |
| 561 | + data = b"G" * total_size |
| 562 | + stream = io.BytesIO(data) |
| 563 | + |
| 564 | + class _ClientPauseError(Exception): |
| 565 | + pass |
| 566 | + |
| 567 | + session1 = await client.upload_media( |
| 568 | + request=UploadMediaRequest(name="async_golden_user_resume.mp4"), |
| 569 | + config=ResumableUploadConfig(chunk_size=chunk_size), |
| 570 | + ) |
| 571 | + session1_snapshots = [] |
| 572 | + with pytest.raises(_ClientPauseError): |
| 573 | + async for progress in session1.upload(stream=stream, size=total_size): |
| 574 | + session1_snapshots.append(progress) |
| 575 | + if progress.bytes_uploaded >= chunk_size: |
| 576 | + raise _ClientPauseError("Simulated user pause after first chunk") |
| 577 | + |
| 578 | + saved_url = session1.upload_url |
| 579 | + saved_chunk_size = session1.chunk_size |
| 580 | + assert saved_url is not None |
| 581 | + assert saved_chunk_size == chunk_size |
| 582 | + assert [(p.state, p.bytes_uploaded) for p in session1_snapshots] == [ |
| 583 | + (ProgressState.STARTED, 0), |
| 584 | + (ProgressState.UPLOADING, 524_288), |
| 585 | + ] |
| 586 | + |
| 587 | + stream.seek(0) |
| 588 | + session2 = await client.upload_media( |
| 589 | + request=UploadMediaRequest(name="async_golden_user_resume.mp4"), |
| 590 | + config=ResumableUploadConfig(chunk_size=saved_chunk_size), |
| 591 | + ) |
| 592 | + session2_snapshots = [ |
| 593 | + p |
| 594 | + async for p in session2.resume( |
| 595 | + upload_url=saved_url, |
| 596 | + stream=stream, |
| 597 | + size=total_size, |
| 598 | + chunk_size=saved_chunk_size, |
| 599 | + ) |
| 600 | + ] |
| 601 | + |
| 602 | + assert [(p.state, p.bytes_uploaded) for p in session2_snapshots] == [ |
| 603 | + (ProgressState.OFFSET_RECEIVED, 524_288), |
| 604 | + (ProgressState.UPLOADING, 1_048_576), |
| 605 | + (ProgressState.FINALIZED, 1_500_000), |
| 606 | + ] |
| 607 | + assert session2.finished |
| 608 | + assert isinstance(session2.response, UploadMediaResponse) |
| 609 | + assert session2.response.name == "async_golden_user_resume.mp4" |
| 610 | + assert session2.response.size == total_size |
0 commit comments