Пример #1
0
    def test_cancel_sync(self):
        global updated_progress
        updated_progress = None

        def set_progress(progress):
            global updated_progress
            updated_progress = progress

        class SyncThread(threading.Thread):
            def __init__(self, importer, repo, sync_conduit, config):
                threading.Thread.__init__(self)
                self.importer = importer
                self.repo = repo
                self.sync_conduit = sync_conduit
                self.config = config
                self.status = None
                self.summary = None
                self.details = None
                self.finished = False

            def run(self):
                status, summary, details = self.importer._sync_repo(self.repo, self.sync_conduit, self.config)
                self.status = status
                self.summary = summary
                self.details = details
                self.finished = True

        feed_url = "http://repos.fedorapeople.org/repos/pulp/pulp/v1/testing/6Server/x86_64/"
        repo = mock.Mock(spec=Repository)
        repo.working_dir = self.working_dir
        repo.id = "test_cancel_sync"
        sync_conduit = importer_mocks.get_sync_conduit(existing_units=[], pkg_dir=self.pkg_dir)
        sync_conduit.set_progress = mock.Mock()
        sync_conduit.set_progress.side_effect = set_progress
        config = importer_mocks.get_basic_config(feed_url=feed_url, num_threads=1, max_speed=25)
        importer = YumImporter()
        sync_thread = SyncThread(importer, repo, sync_conduit, config)
        sync_thread.start()
        # Wait to confirm that sync has started and we are downloading packages
        # We are intentionally setting the 'config' to use 1 thread and max_speed to be low so we will
        # have a chance to cancel the sync before it completes
        for i in range(30):
            if updated_progress and updated_progress.has_key("content") and updated_progress["content"].has_key("state") \
                and updated_progress["content"]["state"] == "IN_PROGRESS":
                break
            time.sleep(1)
        self.assertEquals(updated_progress["metadata"]["state"], "FINISHED")
        self.assertEquals(updated_progress["content"]["state"], "IN_PROGRESS")
        ###
        ### Issue Cancel
        ###
        importer.cancel_sync_repo(None, None)
        # Wait for cancel of sync
        for i in range(45):
            if sync_thread.finished:
                break
            time.sleep(1)
        self.assertEquals(updated_progress["content"]["state"], "CANCELED")
        self.assertFalse(sync_thread.status)