From 6694c029f89c36b839c2e4a41aefc3db6b380c41 Mon Sep 17 00:00:00 2001 From: Ardhi Putra Pratama H Date: Mon, 4 Jul 2016 05:14:43 +0200 Subject: [PATCH] Refactor to use deferred --- Tribler/Policies/BoostingSource.py | 28 ++++++++----------- .../CreditMining/test_creditmining_sys.py | 25 +++++++++-------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Tribler/Policies/BoostingSource.py b/Tribler/Policies/BoostingSource.py index 06e8b25288d..d755561d97b 100644 --- a/Tribler/Policies/BoostingSource.py +++ b/Tribler/Policies/BoostingSource.py @@ -255,7 +255,7 @@ def showtorrent(torrent): if len(self.unavail_torrent) and self.enabled: self._logger.debug("Unavailable #torrents : %d from %s", len(self.unavail_torrent), hexlify(self.source)) for torrent in self.unavail_torrent.values(): - self._load_torrent(torrent[2], showtorrent) + self._load_torrent(torrent[2]).addCallback(showtorrent) def _update(self): if len(self.torrents) < self.max_torrents and self.database_updated: @@ -284,7 +284,7 @@ def _on_database_updated(self, dummy_subject, dummy_change_type, dummy_infohash) def get_source_text(self): return str(self.channel_dict[2]) if self.channel_dict else None - def _load_torrent(self, infohash, callback=None): + def _load_torrent(self, infohash): """ function to load infohash and put in into database. """ @@ -297,26 +297,22 @@ def add_to_loaded(infohash): """ function to add loaded infohash to memory """ - tdef = TorrentDef.load_from_memory(self.session.get_collected_torrent(unhexlify(infohash))) - self.loaded_torrent[infohash] = tdef - - if callback is not None: - callback(tdef) + self.loaded_torrent[infohash].callback(TorrentDef.load_from_memory(self.session.get_collected_torrent( + unhexlify(infohash)))) + self.loaded_torrent[infohash] = None if infohash not in self.loaded_torrent: - if self.session.has_collected_torrent(infohash): - meta_dict = self.session.get_collected_torrent(infohash) - tdef = TorrentDef.load_from_memory(meta_dict) - else: + self.loaded_torrent[infohash] = defer.Deferred() + + if not self.session.has_collected_torrent(infohash): if self.session.has_download(infohash): return - tdef = None self.session.download_torrentfile(infohash, add_to_loaded, 0) - else: - tdef = self.loaded_torrent[infohash] - if not (tdef is None or callback is None): - callback(tdef) + deferred_load = self.loaded_torrent[infohash] + + return deferred_load + class RSSFeedSource(BoostingSource): """ diff --git a/Tribler/Test/Core/CreditMining/test_creditmining_sys.py b/Tribler/Test/Core/CreditMining/test_creditmining_sys.py index 861cbb198ed..9a2ddd56159 100644 --- a/Tribler/Test/Core/CreditMining/test_creditmining_sys.py +++ b/Tribler/Test/Core/CreditMining/test_creditmining_sys.py @@ -16,7 +16,6 @@ from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.Utilities.twisted_thread import deferred, reactor from Tribler.Core.simpledefs import NTFY_TORRENTS, NTFY_UPDATE, NTFY_CHANNELCAST -from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent from Tribler.Policies.BoostingManager import BoostingManager, BoostingSettings from Tribler.Test.Core.CreditMining.mock_creditmining import MockLtTorrent, ResourceFailClass from Tribler.Test.test_as_server import TestAsServer, TESTS_DATA_DIR @@ -357,9 +356,10 @@ def test_chn_lookup(self): self.boosting_manager.add_source(dispersy_cid) chn_obj = self.boosting_manager.get_source_object(dispersy_cid) - def _load(_, callback=None): - if callback is not None: - callback(self.tdef) + def _load(_): + defer_ret = defer.Deferred() + defer_ret.callback(self.tdef) + return defer_ret def check_torrents_channel(src, defer_param=None, target=1): """ @@ -433,9 +433,10 @@ def _set_id_channel(channel_id): self.boosting_manager.add_source(dispersy_cid) chn_obj = self.boosting_manager.get_source_object(dispersy_cid) - def _load(_, callback=None): - if callback is not None: - callback(self.tdef) + def _load(_): + defer_ret = defer.Deferred() + defer_ret.callback(self.tdef) + return defer_ret chn_obj._load_torrent = _load @@ -448,7 +449,6 @@ def clean_community(_): chn_obj.kill_tasks() - d = self.check_source(dispersy_cid) d.addCallback(clean_community) return d @@ -478,11 +478,12 @@ def test_chn_max_torrents(self): self.boosting_manager.add_source(dispersy_cid) chn_obj = self.boosting_manager.get_source_object(dispersy_cid) chn_obj.max_torrents = 2 - chn_obj._load_torrent = lambda dummy_1, dummy_2: None + chn_obj._load_torrent = lambda _: defer.Deferred() - def _load(infohash, callback=None): - if callback is not None: - callback(self.tdef if binascii.hexlify(infohash).startswith("fc") else pioneer_tdef) + def _load(infohash): + defer_ret = defer.Deferred() + defer_ret.callback(self.tdef if binascii.hexlify(infohash).startswith("fc") else pioneer_tdef) + return defer_ret def activate_mgr(): """