def test_torrent_discovered(self): torrent_inserteds = [] torrent_insert_callback = lambda source, infohash, name: torrent_inserteds.append( (source, infohash, name)) source = ChannelSource(self.session, self.cid, torrent_insert_callback) source.start() source.on_torrent_discovered(NTFY_TORRENT, NTFY_DISCOVERED, self.cid, { 'dispersy_cid': self.cid, 'infohash': '\00' * 20, 'name': 'torrent' }) self.assertIn((self.cid, '\00' * 20, 'torrent'), torrent_inserteds, 'ChannelSource failed to insert torrent') source.on_torrent_discovered(NTFY_TORRENT, NTFY_DISCOVERED, self.cid, { 'dispersy_cid': '1' * 40, 'infohash': '\01' * 20, 'name': 'torrent' }) self.assertTrue( len(torrent_inserteds) == 1, 'ChannelSource inserted torrent with wrong dispersy_cid') source.stop()
def test_channel_lookup(self): source = ChannelSource(self.session, self.cid, lambda: None) source.start() self.assertIsInstance( source.community, ChannelCommunity, 'ChannelSource failed to create ChannelCommunity') source.stop()
def test_channel_lookup(self): test_deferred = Deferred() with db_session: my_channel = self.session.lm.mds.ChannelMetadata.create_channel( 'test', 'test') _ = self.session.lm.mds.TorrentMetadata( title='testtorrent', infohash=str(random.getrandbits(160))) source = ChannelSource(self.session, str(my_channel.public_key), lambda *_: test_deferred.callback(None)) source.start() return test_deferred
def test_torrent_from_db(self): # Torrent is a tuple: (channel_id, dispersy_id, peer_id, infohash, timestamp, name, files, trackers) torrent = (0, self.cid, 42, '\00' * 20, 0, u'torrent', [], []) channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST) channel_db_handler.on_torrents_from_dispersy([torrent]) torrent_inserteds = [] torrent_insert_callback = lambda source, infohash, name: torrent_inserteds.append( (source, infohash, name)) source = ChannelSource(self.session, self.cid, torrent_insert_callback) source.start() yield deferLater(reactor, 1, lambda: None) self.assertIn((self.cid, hexlify(torrent[3]), torrent[5]), torrent_inserteds, 'ChannelSource failed to insert torrent') source.stop()
def test_existing_channel_lookup(self): # Find AllChannel for community in self.session.lm.dispersy.get_communities(): if isinstance(community, AllChannelCommunity): allchannelcommunity = community # Load the channel community = ChannelCommunity.init_community( self.session.lm.dispersy, self.session.lm.dispersy.get_member(mid=unhexlify(self.cid)), allchannelcommunity.my_member, self.session) # Check if we find the channel source = ChannelSource(self.session, self.cid, lambda: None) source.start() self.assertEqual( source.community, community, 'ChannelSource failed to find existing ChannelCommunity') source.stop()
def add_source(self, source_str): """ Add new source to the credit mining manager """ if source_str not in self.sources: num_torrents = len(self.torrents) if isinstance(source_str, basestring) and len(source_str) == 40: source = ChannelSource(self.session, source_str, self.on_torrent_insert) else: self._logger.error('Cannot add unknown source %s', source_str) return self.sources[source_str] = source source.start() self._logger.info('Added source %s', source_str) # If we don't have any torrents and the select LoopingCall is running, stop it. # It will restart immediately after we have enough torrents. if num_torrents == 0 and self.select_lc.running: self.select_lc.stop() else: self._logger.info('Already have source %s', source_str)