def test_connect_peer_port(self): """Test to ensure port is int for libtorrent""" atp = self.get_torrent_atp('test_torrent.file.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) self.assertFalse(self.torrent.connect_peer('127.0.0.1', 'text')) self.assertTrue(self.torrent.connect_peer('127.0.0.1', '1234'))
def run_test_set_prioritize_first_last(self, torrent_file, prioritized_piece_indexes): atp = self.get_torrent_atp(torrent_file) handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) priorities_original = handle.piece_priorities() prioritized_pieces, new_priorites = self.torrent.set_prioritize_first_last( True) priorities = handle.piece_priorities() non_prioritized_pieces = list(range(len(priorities))) # The prioritized indexes are the same as we expect self.assertEquals(prioritized_pieces, prioritized_piece_indexes) # Test the priority of the prioritized pieces for first, last in prioritized_pieces: for i in range(first, last): if i in non_prioritized_pieces: non_prioritized_pieces.remove(i) self.assertEquals(priorities[i], 7) # Test the priority of all the non-prioritized pieces for i in non_prioritized_pieces: self.assertEquals(priorities[i], 1) # The length of the list of new priorites is the same as the original self.assertEquals(len(priorities_original), len(new_priorites))
def test_set_prioritize_first_last_pieces_false(self): atp = self.get_torrent_atp('dir_with_6_files.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) # First set some pieces prioritized self.torrent.set_prioritize_first_last_pieces(True) # Reset pirorities self.torrent.set_prioritize_first_last_pieces(False) priorities = handle.piece_priorities() # Test the priority of the prioritized pieces for i in priorities: self.assertEqual(priorities[i], 4)
def test_rename_unicode(self): """Test renaming file/folders with unicode filenames.""" atp = self.get_torrent_atp('unicode_filenames.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) # Ignore TorrentManager method call TorrentManager.save_resume_data = mock.MagicMock result = self.torrent.rename_folder('unicode_filenames', 'Горбачёв') self.assertIsInstance(result, defer.DeferredList) result = self.torrent.rename_files([[0, 'new_рбачёв']]) self.assertIsNone(result)
def test_get_eta_downloading(self): atp = self.get_torrent_atp('test_torrent.file.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) self.assertEqual(self.torrent.get_eta(), 0) self.torrent.status = mock.MagicMock() self.torrent.status.download_payload_rate = 50 self.torrent.status.total_wanted = 10000 self.torrent.status.total_wanted_done = 5000 result = self.torrent.get_eta() self.assertEqual(result, 100) self.assertIsInstance(result, int)
def test_set_file_priorities(self): atp = self.get_torrent_atp('dir_with_6_files.torrent') handle = self.session.add_torrent(atp) torrent = Torrent(handle, {}) result = torrent.get_file_priorities() self.assertTrue(all(x == 4 for x in result)) new_priorities = [3, 1, 2, 0, 5, 6, 7] torrent.set_file_priorities(new_priorities) self.assertEqual(torrent.get_file_priorities(), new_priorities) # Test with handle.piece_priorities as handle.file_priorities async # updates and will return old value. Also need to remove a priority # value as one file is much smaller than piece size so doesn't show. piece_prio = handle.piece_priorities() result = all(p in piece_prio for p in [3, 2, 0, 5, 6, 7]) self.assertTrue(result)
def run_test_set_prioritize_first_last_pieces(self, torrent_file, prioritized_piece_indexes): atp = self.get_torrent_atp(torrent_file) handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) priorities_original = handle.piece_priorities() self.torrent.set_prioritize_first_last_pieces(True) priorities = handle.piece_priorities() # The length of the list of new priorites is the same as the original self.assertEqual(len(priorities_original), len(priorities)) # Test the priority of all the pieces against the calculated indexes. for idx, priority in enumerate(priorities): if idx in prioritized_piece_indexes: self.assertEqual(priorities[idx], 7) else: self.assertEqual(priorities[idx], 4)
def test_get_eta_seeding(self): atp = self.get_torrent_atp('test_torrent.file.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) self.assertEqual(self.torrent.get_eta(), 0) self.torrent.status = mock.MagicMock() self.torrent.status.upload_payload_rate = 5000 self.torrent.status.download_payload_rate = 0 self.torrent.status.all_time_download = 10000 self.torrent.status.all_time_upload = 500 self.torrent.is_finished = True self.torrent.options = {'stop_at_ratio': False} # Test finished and uploading but no stop_at_ratio set. self.assertEqual(self.torrent.get_eta(), 0) self.torrent.options = {'stop_at_ratio': True, 'stop_ratio': 1.5} result = self.torrent.get_eta() self.assertEqual(result, 2) self.assertIsInstance(result, int)
else: handle = self.session.add_torrent(add_torrent_params) except RuntimeError, e: log.warning("Error adding torrent: %s", e) if not handle or not handle.is_valid(): log.debug("torrent handle is invalid!") # The torrent was not added to the session component.resume("AlertManager") return log.debug("handle id: %s", str(handle.info_hash())) # Set auto_managed to False because the torrent is paused handle.auto_managed(False) # Create a Torrent object torrent = Torrent(handle, options, state, filename, magnet) # Add the torrent object to the dictionary self.torrents[torrent.torrent_id] = torrent if self.config["queue_new_to_top"]: handle.queue_position_top() component.resume("AlertManager") # Resume the torrent if needed if not options["add_paused"]: torrent.resume() # Add to queued torrents set self.queued_torrents.add(torrent.torrent_id) # Write the .torrent file to the state directory
def test_get_name_unicode(self): """Test retrieving a unicode torrent name from libtorrent.""" atp = self.get_torrent_atp('unicode_file.torrent') handle = self.session.add_torrent(atp) self.torrent = Torrent(handle, {}) self.assertEqual(self.torrent.get_name(), 'সুকুমার রায়.mkv')