Пример #1
0
class TestProcessPoolTransferFuture(unittest.TestCase):
    def setUp(self):
        self.monitor = TransferMonitor()
        self.transfer_id = self.monitor.notify_new_transfer()
        self.meta = ProcessPoolTransferMeta(transfer_id=self.transfer_id,
                                            call_args=CallArgs())
        self.future = ProcessPoolTransferFuture(monitor=self.monitor,
                                                meta=self.meta)

    def test_meta(self):
        self.assertEqual(self.future.meta, self.meta)

    def test_done(self):
        self.assertFalse(self.future.done())
        self.monitor.notify_done(self.transfer_id)
        self.assertTrue(self.future.done())

    def test_result(self):
        self.monitor.notify_done(self.transfer_id)
        self.assertIsNone(self.future.result())

    def test_result_with_exception(self):
        self.monitor.notify_exception(self.transfer_id, RuntimeError())
        self.monitor.notify_done(self.transfer_id)
        with self.assertRaises(RuntimeError):
            self.future.result()

    def test_result_with_keyboard_interrupt(self):
        mock_monitor = mock.Mock(TransferMonitor)
        mock_monitor._connect = mock.Mock()
        mock_monitor.poll_for_result.side_effect = KeyboardInterrupt()
        future = ProcessPoolTransferFuture(monitor=mock_monitor,
                                           meta=self.meta)
        with self.assertRaises(KeyboardInterrupt):
            future.result()
        self.assertTrue(mock_monitor._connect.called)
        self.assertTrue(mock_monitor.notify_exception.called)
        call_args = mock_monitor.notify_exception.call_args[0]
        self.assertEqual(call_args[0], self.transfer_id)
        self.assertIsInstance(call_args[1], CancelledError)

    def test_cancel(self):
        self.future.cancel()
        self.monitor.notify_done(self.transfer_id)
        with self.assertRaises(CancelledError):
            self.future.result()
Пример #2
0
class TestTransferMonitor(unittest.TestCase):
    def setUp(self):
        self.monitor = TransferMonitor()
        self.transfer_id = self.monitor.notify_new_transfer()

    def test_notify_new_transfer_creates_new_state(self):
        monitor = TransferMonitor()
        transfer_id = monitor.notify_new_transfer()
        self.assertFalse(monitor.is_done(transfer_id))
        self.assertIsNone(monitor.get_exception(transfer_id))

    def test_notify_new_transfer_increments_transfer_id(self):
        monitor = TransferMonitor()
        self.assertEqual(monitor.notify_new_transfer(), 0)
        self.assertEqual(monitor.notify_new_transfer(), 1)

    def test_notify_get_exception(self):
        exception = Exception()
        self.monitor.notify_exception(self.transfer_id, exception)
        self.assertEqual(self.monitor.get_exception(self.transfer_id),
                         exception)

    def test_get_no_exception(self):
        self.assertIsNone(self.monitor.get_exception(self.transfer_id))

    def test_notify_jobs(self):
        self.monitor.notify_expected_jobs_to_complete(self.transfer_id, 2)
        self.assertEqual(self.monitor.notify_job_complete(self.transfer_id), 1)
        self.assertEqual(self.monitor.notify_job_complete(self.transfer_id), 0)

    def test_notify_jobs_for_multiple_transfers(self):
        self.monitor.notify_expected_jobs_to_complete(self.transfer_id, 2)
        other_transfer_id = self.monitor.notify_new_transfer()
        self.monitor.notify_expected_jobs_to_complete(other_transfer_id, 2)
        self.assertEqual(self.monitor.notify_job_complete(self.transfer_id), 1)
        self.assertEqual(self.monitor.notify_job_complete(other_transfer_id),
                         1)

    def test_done(self):
        self.assertFalse(self.monitor.is_done(self.transfer_id))
        self.monitor.notify_done(self.transfer_id)
        self.assertTrue(self.monitor.is_done(self.transfer_id))

    def test_poll_for_result(self):
        self.monitor.notify_done(self.transfer_id)
        self.assertIsNone(self.monitor.poll_for_result(self.transfer_id))

    def test_poll_for_result_raises_error(self):
        self.monitor.notify_exception(self.transfer_id, RuntimeError())
        self.monitor.notify_done(self.transfer_id)
        with self.assertRaises(RuntimeError):
            self.monitor.poll_for_result(self.transfer_id)

    def test_poll_for_result_waits_till_done(self):
        event_order = []

        def sleep_then_notify_done():
            time.sleep(0.05)
            event_order.append('notify_done')
            self.monitor.notify_done(self.transfer_id)

        t = threading.Thread(target=sleep_then_notify_done)
        t.start()

        self.monitor.poll_for_result(self.transfer_id)
        event_order.append('done_polling')
        self.assertEqual(event_order, ['notify_done', 'done_polling'])

    def test_notify_cancel_all_in_progress(self):
        monitor = TransferMonitor()
        transfer_ids = []
        for _ in range(10):
            transfer_ids.append(monitor.notify_new_transfer())
        monitor.notify_cancel_all_in_progress()
        for transfer_id in transfer_ids:
            self.assertIsInstance(monitor.get_exception(transfer_id),
                                  CancelledError)
            # Cancelling a transfer does not mean it is done as there may
            # be cleanup work left to do.
            self.assertFalse(monitor.is_done(transfer_id))

    def test_notify_cancel_does_not_affect_done_transfers(self):
        self.monitor.notify_done(self.transfer_id)
        self.monitor.notify_cancel_all_in_progress()
        self.assertTrue(self.monitor.is_done(self.transfer_id))
        self.assertIsNone(self.monitor.get_exception(self.transfer_id))