当前位置: 首页>>代码示例>>Python>>正文


Python MultipartUploadContext.cancel_upload方法代码示例

本文整理汇总了Python中awscli.customizations.s3.tasks.MultipartUploadContext.cancel_upload方法的典型用法代码示例。如果您正苦于以下问题:Python MultipartUploadContext.cancel_upload方法的具体用法?Python MultipartUploadContext.cancel_upload怎么用?Python MultipartUploadContext.cancel_upload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在awscli.customizations.s3.tasks.MultipartUploadContext的用法示例。


在下文中一共展示了MultipartUploadContext.cancel_upload方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestMultipartUploadContext

# 需要导入模块: from awscli.customizations.s3.tasks import MultipartUploadContext [as 别名]
# 或者: from awscli.customizations.s3.tasks.MultipartUploadContext import cancel_upload [as 别名]

#.........这里部分代码省略.........
        self.start_thread(arbitrary_waiting_thread)

        # Then finally the CreateMultipartUpload completes and we
        # announce the upload id.
        self.create_upload('my_upload_id')
        # The upload_part thread can now proceed as well as the complete
        # multipart upload thread.
        self.join_threads()

        self.assertIsNone(self.caught_exception)
        # We can verify that the invariants still hold.
        self.assertEqual(len(self.calls), 4)
        # First there should be three calls, create, upload, complete.
        self.assertEqual(self.calls[0][0], 'create_multipart_upload')
        self.assertEqual(self.calls[1][0], 'upload_part')
        self.assertEqual(self.calls[2][0], 'complete_upload')
        # Then anything that was waiting for the operation to complete should
        # be called afterwards.
        self.assertEqual(self.calls[3][0], 'arbitrary_post_complete_operation')

        # Verify the correct args were used.
        self.assertEqual(self.calls[0][1], 'my_upload_id')
        self.assertEqual(self.calls[1][1:], (1, 'my_upload_id'))
        self.assertEqual(
            self.calls[2][1:],
            ('my_upload_id', [{'ETag': 'etag1', 'PartNumber': 1}]))

    def test_randomized_stress_test(self):
        # Now given that we've verified the functionality from
        # the two tests above, we randomize the threading to ensure
        # that the order doesn't actually matter.  The invariant that
        # the CreateMultipartUpload is called first, then UploadPart
        # operations are called with the appropriate upload_id, then
        # CompleteMultipartUpload with the appropriate upload_id and
        # parts list should hold true regardless of how the threads
        # are ordered.

        # I've run this with much larger values, but 100 is a good
        # tradeoff with coverage vs. execution time.
        for i in range(100):
            expected_parts = random.randint(2, 50)
            self.context = MultipartUploadContext(expected_parts=expected_parts)
            self.threads = []
            self.calls = []
            all_threads = [
                threading.Thread(target=self.complete_upload),
                threading.Thread(target=self.create_upload,
                                args=('my_upload_id',)),
                threading.Thread(target=self.wait_for_upload_complete),
            ]
            for i in range(1, expected_parts + 1):
                all_threads.append(
                    threading.Thread(target=self.upload_part, args=(i,))
                )
            random.shuffle(all_threads)
            for thread in all_threads:
                self.start_thread(thread)
            self.join_threads()
            self.assertEqual(self.calls[0][0], 'create_multipart_upload')
            self.assertEqual(self.calls[-1][0],
                             'arbitrary_post_complete_operation')
            self.assertEqual(self.calls[-2][0], 'complete_upload')
            parts = set()
            for call in self.calls[1:-2]:
                self.assertEqual(call[0], 'upload_part')
                self.assertEqual(call[2], 'my_upload_id')
                parts.add(call[1])
            self.assertEqual(len(parts), expected_parts)

    def test_can_cancel_tasks(self):
        # Let's say that we want have a thread waiting for the upload id.
        upload_part_thread = threading.Thread(target=self.upload_part,
                                            args=(1,))
        self.start_thread(upload_part_thread)
        # But for whatever reason we aren't able to call CreateMultipartUpload.
        # We'd like to let the other thread know that it should abort.
        self.context.cancel_upload()
        # The start thread should be finished.
        self.join_threads()
        # No s3 calls should have been made.
        self.assertEqual(self.calls, [])
        # And any thread that tries to wait for data will get an exception.
        with self.assertRaises(UploadCancelledError):
            self.context.wait_for_upload_id()
        with self.assertRaises(UploadCancelledError):
            self.context.wait_for_parts_to_finish()

    def test_cancel_threads_waiting_for_completion(self):
        # So we have a thread waiting for the entire upload to complete.
        arbitrary_waiting_thread = threading.Thread(target=self.wait_for_upload_complete)
        self.start_thread(arbitrary_waiting_thread)

        # And as it's waiting, something happens and we cancel the upload.
        self.context.cancel_upload()

        # The thread should exit.
        self.join_threads()

        # And we should have seen an exception being raised.
        self.assertIsInstance(self.caught_exception, UploadCancelledError)
开发者ID:BillTheBest,项目名称:aws-cli,代码行数:104,代码来源:test_tasks.py


注:本文中的awscli.customizations.s3.tasks.MultipartUploadContext.cancel_upload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。