Example #1
0
    def test_can_create_engine_wrapper_retry(self):
        impl = LibtorrentDownloadImpl(self.session, None)
        impl.cew_scheduled = True
        def set_cew_false():
            self.session.lm.ltmgr.is_dht_ready = lambda: True
            impl.cew_scheduled = False

        # Simulate Tribler changing the cew, the calllater should have fired by now
        # and before it executed the cew is false, firing the deferred.
        reactor.callLater(2, set_cew_false)
        return impl.can_create_engine_wrapper()
    def test_chn_exist_lookup(self):
        """
        testing existing channel as a source.

        It also tests how boosting manager cope with unknown channel with retrying
        the lookup
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        # channel is exist
        community = ChannelCommunity.init_community(
            self.session.lm.dispersy,
            self.session.lm.dispersy.get_member(mid=dispersy_cid),
            self.session.lm.dispersy._communities['allchannel']._my_member,
            self.session)

        # make the id unknown so boosting manager can test repeating search
        id_tmp = community._channel_id
        community._channel_id = 0

        def _set_id_channel(channel_id):
            """
            set channel id manually (emulate finding)
            """
            community._channel_id = channel_id

        reactor.callLater(5, _set_id_channel, id_tmp)

        self.boosting_manager.add_source(dispersy_cid)
        chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

        chn_obj._load_torrent = self._load

        def clean_community(_):
            """
            cleanly exit the community we are in
            """
            if chn_obj.community:
                chn_obj.community.cancel_all_pending_tasks()

            chn_obj.kill_tasks()

        d = self.check_source(dispersy_cid)
        d.addCallback(clean_community)
        return d
        def check_torrents_channel(src, defer_param=None):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if len(src_obj.unavail_torrent) == 0:
                self.assertLessEqual(len(src_obj.torrents),
                                     src_obj.max_torrents)
            else:
                success = False
                reactor.callLater(1, check_torrents_channel, src, defer_param)

            if success:
                src_obj.community.cancel_all_pending_tasks()
                src_obj.kill_tasks()
                defer_param.callback(src)

            return defer_param
        def check_torrents_channel(src, defer_param=None, target=1):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if not src_obj or len(src_obj.torrents) < target:
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents.get(
                    TORRENT_UBUNTU_FILE_INFOHASH, None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents[
                    TORRENT_UBUNTU_FILE_INFOHASH].get('download', None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)

            if success:
                self.boosting_manager.set_enable_mining(src,
                                                        False,
                                                        force_restart=True)
                if src_obj.community:
                    src_obj.community.cancel_all_pending_tasks()

                self.assertEqual(src_obj.get_source_text(), 'Simple Channel')

                defer_param.callback(src)

            return defer_param
    def test_chn_max_torrents(self):
        """
        Test the restriction of max_torrents in a source.
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        pioneer_file = os.path.join(
            TESTS_DATA_DIR, "Pioneer.One.S01E06.720p.x264-VODO.torrent")
        pioneer_tdef = TorrentDef.load(pioneer_file)
        pioneer_ihash = binascii.unhexlify(
            "66ED7F30E3B30FA647ABAA19A36E7503AA071535")

        torrent_list = [[
            self.channel_id, 1, 1, pioneer_ihash, 1460000001, pioneer_file,
            pioneer_tdef.get_files_with_length(),
            pioneer_tdef.get_trackers_as_single_tuple()
        ]]
        self.insert_torrents_into_channel(torrent_list)

        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 _: defer.Deferred()

        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():
            """
            activate ltmgr and adjust max torrents to emulate overflow torrents
            """
            chn_obj.max_torrents = 1
            chn_obj._load_torrent = _load

        reactor.callLater(5, activate_mgr)

        def check_torrents_channel(src, defer_param=None):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if len(src_obj.unavail_torrent) == 0:
                self.assertLessEqual(len(src_obj.torrents),
                                     src_obj.max_torrents)
            else:
                success = False
                reactor.callLater(1, check_torrents_channel, src, defer_param)

            if success:
                src_obj.community.cancel_all_pending_tasks()
                src_obj.kill_tasks()
                defer_param.callback(src)

            return defer_param

        d = self.check_source(dispersy_cid)
        d.addCallback(check_torrents_channel)
        return d