|
21 | 21 | import mock
|
22 | 22 |
|
23 | 23 | from google.api_core.exceptions import RequestRangeNotSatisfiable
|
| 24 | +from google.cloud.storage.fileio import CHUNK_SIZE_MULTIPLE |
24 | 25 | from google.cloud.storage.retry import DEFAULT_RETRY
|
25 | 26 |
|
26 | 27 | TEST_TEXT_DATA = string.ascii_lowercase + "\n" + string.ascii_uppercase + "\n"
|
@@ -377,7 +378,7 @@ def test_write(self, mock_warn):
|
377 | 378 | # Write under chunk_size. This should be buffered and the upload not
|
378 | 379 | # initiated.
|
379 | 380 | writer.write(TEST_BINARY_DATA[0:4])
|
380 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 381 | + blob._initiate_resumable_upload.assert_not_called() |
381 | 382 |
|
382 | 383 | # Write over chunk_size. This should result in upload initialization
|
383 | 384 | # and multiple chunks uploaded.
|
@@ -426,6 +427,52 @@ def test_close_errors(self):
|
426 | 427 | with self.assertRaises(ValueError):
|
427 | 428 | writer.write(TEST_BINARY_DATA)
|
428 | 429 |
|
| 430 | + def test_terminate_after_initiate(self): |
| 431 | + blob = mock.Mock() |
| 432 | + |
| 433 | + upload = mock.Mock(upload_url="dummy") |
| 434 | + transport = mock.Mock() |
| 435 | + |
| 436 | + blob._initiate_resumable_upload.return_value = (upload, transport) |
| 437 | + |
| 438 | + with self.assertRaises(RuntimeError): |
| 439 | + with self._make_blob_writer(blob, chunk_size=CHUNK_SIZE_MULTIPLE) as writer: |
| 440 | + writer.write(bytes(CHUNK_SIZE_MULTIPLE + 1)) # initiate upload |
| 441 | + raise RuntimeError # should terminate the upload |
| 442 | + blob._initiate_resumable_upload.assert_called_once() # upload initiated |
| 443 | + self.assertTrue(writer.closed) # terminate called |
| 444 | + transport.delete.assert_called_with("dummy") # resumable upload terminated |
| 445 | + |
| 446 | + def test_terminate_before_initiate(self): |
| 447 | + blob = mock.Mock() |
| 448 | + |
| 449 | + upload = mock.Mock() |
| 450 | + transport = mock.Mock() |
| 451 | + |
| 452 | + blob._initiate_resumable_upload.return_value = (upload, transport) |
| 453 | + |
| 454 | + with self.assertRaises(RuntimeError): |
| 455 | + with self._make_blob_writer(blob, chunk_size=CHUNK_SIZE_MULTIPLE) as writer: |
| 456 | + writer.write(bytes(CHUNK_SIZE_MULTIPLE - 1)) # upload not yet initiated |
| 457 | + raise RuntimeError # there is no resumable upload to terminate |
| 458 | + blob._initiate_resumable_upload.assert_not_called() # upload not yet initiated |
| 459 | + self.assertTrue(writer.closed) # terminate called |
| 460 | + transport.delete.assert_not_called() # there's no resumable upload to terminate |
| 461 | + |
| 462 | + def test_terminate_skipped(self): |
| 463 | + blob = mock.Mock() |
| 464 | + |
| 465 | + upload = mock.Mock() |
| 466 | + transport = mock.Mock() |
| 467 | + |
| 468 | + blob._initiate_resumable_upload.return_value = (upload, transport) |
| 469 | + |
| 470 | + with self._make_blob_writer(blob, chunk_size=CHUNK_SIZE_MULTIPLE) as writer: |
| 471 | + writer.write(bytes(CHUNK_SIZE_MULTIPLE + 1)) # upload initiated |
| 472 | + blob._initiate_resumable_upload.assert_called() # upload initiated |
| 473 | + self.assertTrue(writer.closed) # close called |
| 474 | + transport.delete.assert_not_called() # terminate not called |
| 475 | + |
429 | 476 | def test_flush_fails(self):
|
430 | 477 | blob = mock.Mock(chunk_size=None)
|
431 | 478 | writer = self._make_blob_writer(blob)
|
@@ -468,7 +515,7 @@ def test_conditional_retry_failure(self):
|
468 | 515 | # Write under chunk_size. This should be buffered and the upload not
|
469 | 516 | # initiated.
|
470 | 517 | writer.write(TEST_BINARY_DATA[0:4])
|
471 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 518 | + blob._initiate_resumable_upload.assert_not_called() |
472 | 519 |
|
473 | 520 | # Write over chunk_size. This should result in upload initialization
|
474 | 521 | # and multiple chunks uploaded.
|
@@ -520,7 +567,7 @@ def test_conditional_retry_pass(self):
|
520 | 567 | # Write under chunk_size. This should be buffered and the upload not
|
521 | 568 | # initiated.
|
522 | 569 | writer.write(TEST_BINARY_DATA[0:4])
|
523 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 570 | + blob._initiate_resumable_upload.assert_not_called() |
524 | 571 |
|
525 | 572 | # Write over chunk_size. This should result in upload initialization
|
526 | 573 | # and multiple chunks uploaded.
|
@@ -573,7 +620,7 @@ def test_forced_default_retry(self):
|
573 | 620 | # Write under chunk_size. This should be buffered and the upload not
|
574 | 621 | # initiated.
|
575 | 622 | writer.write(TEST_BINARY_DATA[0:4])
|
576 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 623 | + blob._initiate_resumable_upload.assert_not_called() |
577 | 624 |
|
578 | 625 | # Write over chunk_size. This should result in upload initialization
|
579 | 626 | # and multiple chunks uploaded.
|
@@ -619,7 +666,7 @@ def test_num_retries_and_retry_conflict(self, mock_warn):
|
619 | 666 | # Write under chunk_size. This should be buffered and the upload not
|
620 | 667 | # initiated.
|
621 | 668 | writer.write(TEST_BINARY_DATA[0:4])
|
622 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 669 | + blob._initiate_resumable_upload.assert_not_called() |
623 | 670 |
|
624 | 671 | # Write over chunk_size. The mock will raise a ValueError, simulating
|
625 | 672 | # actual behavior when num_retries and retry are both specified.
|
@@ -673,7 +720,7 @@ def test_num_retries_only(self, mock_warn):
|
673 | 720 | # Write under chunk_size. This should be buffered and the upload not
|
674 | 721 | # initiated.
|
675 | 722 | writer.write(TEST_BINARY_DATA[0:4])
|
676 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 723 | + blob._initiate_resumable_upload.assert_not_called() |
677 | 724 |
|
678 | 725 | # Write over chunk_size. This should result in upload initialization
|
679 | 726 | # and multiple chunks uploaded.
|
@@ -965,7 +1012,7 @@ def test_write(self, mock_warn):
|
965 | 1012 | # Write under chunk_size. This should be buffered and the upload not
|
966 | 1013 | # initiated.
|
967 | 1014 | writer.write(TEST_MULTIBYTE_TEXT_DATA[0:2])
|
968 |
| - blob.initiate_resumable_upload.assert_not_called() |
| 1015 | + blob._initiate_resumable_upload.assert_not_called() |
969 | 1016 |
|
970 | 1017 | # Write all data and close.
|
971 | 1018 | writer.write(TEST_MULTIBYTE_TEXT_DATA[2:])
|
|
0 commit comments