more inlineCallback cleanup

This commit is contained in:
Job Evers-Meltzer 2016-12-30 10:47:34 -06:00
parent 0b53fde352
commit 1bf1b7624c
2 changed files with 40 additions and 43 deletions

View file

@ -97,25 +97,20 @@ class CryptStreamDownloader(object):
d.addCallback(lambda _: self.finished_deferred) d.addCallback(lambda _: self.finished_deferred)
return d return d
@defer.inlineCallbacks
def stop(self, err=None): def stop(self, err=None):
def check_if_stop_succeeded(success):
self.stopping = False
if success is True:
self.stopped = True
self._remove_download_manager()
return success
if self.stopped is True: if self.stopped is True:
raise AlreadyStoppedError() raise AlreadyStoppedError()
if self.stopping is True: if self.stopping is True:
raise CurrentlyStoppingError() raise CurrentlyStoppingError()
assert self.download_manager is not None assert self.download_manager is not None
self.stopping = True self.stopping = True
d = self.download_manager.stop_downloading() success = yield self.download_manager.stop_downloading()
d.addCallback(check_if_stop_succeeded) self.stopping = False
d.addCallback(lambda _: self._fire_completed_deferred(err)) if success is True:
return d self.stopped = True
self._remove_download_manager()
yield self._fire_completed_deferred(err)
def _start_failed(self): def _start_failed(self):
@ -204,7 +199,8 @@ class CryptStreamDownloader(object):
if err is not None: if err is not None:
d.errback(err) d.errback(err)
else: else:
d.callback(self._get_finished_deferred_callback_value()) value = self._get_finished_deferred_callback_value()
d.callback(value)
else: else:
log.debug("Not firing the completed deferred because d is None") log.debug("Not firing the completed deferred because d is None")

View file

@ -652,7 +652,8 @@ class TestTransfer(TestCase):
options = metadata.options options = metadata.options
factories = metadata.factories factories = metadata.factories
chosen_options = [ chosen_options = [
o.default_value for o in options.get_downloader_options(info_validator, prm)] o.default_value for o in options.get_downloader_options(info_validator, prm)
]
return factories[0].make_downloader(metadata, chosen_options, prm) return factories[0].make_downloader(metadata, chosen_options, prm)
def download_file(sd_hash): def download_file(sd_hash):
@ -669,17 +670,14 @@ class TestTransfer(TestCase):
hashsum.update(f.read()) hashsum.update(f.read())
self.assertEqual(hashsum.hexdigest(), "4ca2aafb4101c1e42235aad24fbb83be") self.assertEqual(hashsum.hexdigest(), "4ca2aafb4101c1e42235aad24fbb83be")
@defer.inlineCallbacks
def start_transfer(sd_hash): def start_transfer(sd_hash):
logging.debug("Starting the transfer") logging.debug("Starting the transfer")
yield self.session.setup()
d = self.session.setup() yield add_lbry_file_to_sd_identifier(sd_identifier)
d.addCallback(lambda _: add_lbry_file_to_sd_identifier(sd_identifier)) yield self.lbry_file_manager.setup()
d.addCallback(lambda _: self.lbry_file_manager.setup()) yield download_file(sd_hash)
d.addCallback(lambda _: download_file(sd_hash)) yield check_md5_sum()
d.addCallback(lambda _: check_md5_sum())
return d
def stop(arg): def stop(arg):
if isinstance(arg, Failure): if isinstance(arg, Failure):
@ -914,25 +912,30 @@ class TestTransfer(TestCase):
self.stream_info_manager = DBEncryptedFileMetadataManager(self.session.db_dir) self.stream_info_manager = DBEncryptedFileMetadataManager(self.session.db_dir)
self.lbry_file_manager = EncryptedFileManager(self.session, self.stream_info_manager, sd_identifier) self.lbry_file_manager = EncryptedFileManager(self.session, self.stream_info_manager, sd_identifier)
@defer.inlineCallbacks
def make_downloader(metadata, prm): def make_downloader(metadata, prm):
info_validator = metadata.validator info_validator = metadata.validator
options = metadata.options options = metadata.options
factories = metadata.factories factories = metadata.factories
chosen_options = [o.default_value for o in options.get_downloader_options(info_validator, prm)] chosen_options = [
return factories[0].make_downloader(metadata, chosen_options, prm) o.default_value for o in options.get_downloader_options(info_validator, prm)
]
downloader = yield factories[0].make_downloader(metadata, chosen_options, prm)
defer.returnValue(downloader)
def append_downloader(downloader): def append_downloader(downloader):
downloaders.append(downloader) downloaders.append(downloader)
return downloader return downloader
@defer.inlineCallbacks
def download_file(sd_hash): def download_file(sd_hash):
prm = self.session.payment_rate_manager prm = self.session.payment_rate_manager
d = download_sd_blob(self.session, sd_hash, prm) sd_blob = yield download_sd_blob(self.session, sd_hash, prm)
d.addCallback(sd_identifier.get_metadata_for_sd_blob) metadata = yield sd_identifier.get_metadata_for_sd_blob(sd_blob)
d.addCallback(make_downloader, prm) downloader = yield make_downloader(metadata, prm)
d.addCallback(append_downloader) downloaders.append(downloader)
d.addCallback(lambda downloader: downloader.start()) finished_value = yield downloader.start()
return d defer.returnValue(finished_value)
def check_md5_sum(): def check_md5_sum():
f = open('test_file') f = open('test_file')
@ -959,20 +962,18 @@ class TestTransfer(TestCase):
d.addCallback(check_status_report) d.addCallback(check_status_report)
return d return d
@defer.inlineCallbacks
def start_transfer(sd_hash): def start_transfer(sd_hash):
logging.debug("Starting the transfer") logging.debug("Starting the transfer")
yield self.session.setup()
d = self.session.setup() yield self.stream_info_manager.setup()
d.addCallback(lambda _: self.stream_info_manager.setup()) yield add_lbry_file_to_sd_identifier(sd_identifier)
d.addCallback(lambda _: add_lbry_file_to_sd_identifier(sd_identifier)) yield self.lbry_file_manager.setup()
d.addCallback(lambda _: self.lbry_file_manager.setup()) yield download_file(sd_hash)
d.addCallback(lambda _: download_file(sd_hash)) yield check_md5_sum()
d.addCallback(lambda _: check_md5_sum()) yield download_file(sd_hash)
d.addCallback(lambda _: download_file(sd_hash)) yield delete_lbry_file()
d.addCallback(lambda _: delete_lbry_file()) yield check_lbry_file()
d.addCallback(lambda _: check_lbry_file())
return d
def stop(arg): def stop(arg):
if isinstance(arg, Failure): if isinstance(arg, Failure):