Beispiel #1
0
class TestMultipartDownloadContext(unittest.TestCase):
    def setUp(self):
        self.context = MultipartDownloadContext(num_parts=2)
        self.calls = []
        self.threads = []
        self.call_lock = threading.Lock()
        self.caught_exception = None

    def tearDown(self):
        self.join_threads()

    def join_threads(self):
        for thread in self.threads:
            thread.join()

    def download_stream_part(self, part_number):
        try:
            self.context.wait_for_turn(part_number)
            with self.call_lock:
                self.calls.append(('download_part', str(part_number)))
            self.context.done_with_turn()
        except Exception as e:
            self.caught_exception = e
            return

    def start_thread(self, thread):
        thread.start()
        self.threads.append(thread)

    def test_stream_context(self):
        part_thread = threading.Thread(target=self.download_stream_part,
                                       args=(1,))
        # Once this thread starts it will immediately block becasue it is
        # waiting for part zero to finish submitting its task.
        self.start_thread(part_thread)

        # Now create the thread that should submit its task first.
        part_thread2 = threading.Thread(target=self.download_stream_part,
                                        args=(0,))
        self.start_thread(part_thread2)
        self.join_threads()

        self.assertIsNone(self.caught_exception)

        # We can verify that the invariants still hold.
        self.assertEqual(len(self.calls), 2)
        # First there should be three calls, create, upload, complete.
        self.assertEqual(self.calls[0][0], 'download_part')
        self.assertEqual(self.calls[1][0], 'download_part')

        # Verify the correct order were used.
        self.assertEqual(self.calls[0][1], '0')
        self.assertEqual(self.calls[1][1], '1')
class TestMultipartDownloadContext(unittest.TestCase):
    def setUp(self):
        self.context = MultipartDownloadContext(num_parts=2)
        self.calls = []
        self.threads = []
        self.call_lock = threading.Lock()
        self.caught_exception = None

    def tearDown(self):
        self.join_threads()

    def join_threads(self):
        for thread in self.threads:
            thread.join()

    def download_stream_part(self, part_number):
        try:
            self.context.wait_for_turn(part_number)
            with self.call_lock:
                self.calls.append(('download_part', str(part_number)))
            self.context.done_with_turn()
        except Exception as e:
            self.caught_exception = e
            return

    def start_thread(self, thread):
        thread.start()
        self.threads.append(thread)

    def test_stream_context(self):
        part_thread = threading.Thread(target=self.download_stream_part,
                                       args=(1, ))
        # Once this thread starts it will immediately block becasue it is
        # waiting for part zero to finish submitting its task.
        self.start_thread(part_thread)

        # Now create the thread that should submit its task first.
        part_thread2 = threading.Thread(target=self.download_stream_part,
                                        args=(0, ))
        self.start_thread(part_thread2)
        self.join_threads()

        self.assertIsNone(self.caught_exception)

        # We can verify that the invariants still hold.
        self.assertEqual(len(self.calls), 2)
        # First there should be three calls, create, upload, complete.
        self.assertEqual(self.calls[0][0], 'download_part')
        self.assertEqual(self.calls[1][0], 'download_part')

        # Verify the correct order were used.
        self.assertEqual(self.calls[0][1], '0')
        self.assertEqual(self.calls[1][1], '1')
Beispiel #3
0
 def setUp(self):
     self.context = MultipartDownloadContext(num_parts=2)
     self.calls = []
     self.threads = []
     self.call_lock = threading.Lock()
     self.caught_exception = None
 def setUp(self):
     self.context = MultipartDownloadContext(num_parts=2)
     self.calls = []
     self.threads = []
     self.call_lock = threading.Lock()
     self.caught_exception = None