async def test_piece_manager_get_piece(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) piece = await piece_manager.get_piece() assert type(piece) == Piece
def test_create_piece_manager_last_piece(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) last_piece = piece_manager.pieces[len(piece_manager.pieces) - 1] assert torrent_file.length % torrent_file.piece_length == last_piece.length
def test_all_pieces_in_status(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) status_dict = piece_manager.pieces_status() assert len(piece_manager.pieces) == sum( [i for i in status_dict.values()])
def test_create_piece_manager_all_pieces_available(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) pieces_manager = create_piece_manager(torrent_file) ptr = 0 for piece in pieces_manager.pieces: assert torrent_file.pieces[ptr:ptr + 20] == piece.piece_hash ptr += 20
def test_create_piece_manager_pieces_size(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) pieces_manager = create_piece_manager(torrent_file) size = 0 for piece in pieces_manager.pieces: size += piece.length assert torrent_file.length == size
def create_or_load_torrent_file(path: str): if torrent_file_exist(path): try: torrent_file, piece_manager = load(path) except TypeError: raise Exception("loading torrent file failed") else: torrent_file = generate_torrent_file(path) file_genrator(torrent_file) piece_manager = create_piece_manager(torrent_file) return torrent_file, piece_manager
async def test_request_creation(): torrent_file = generate_torrent_file(_files_list()[3]) piece_manager = create_piece_manager(torrent_file) block_size = 2 ** 14 for i in range(100): piece = await piece_manager.get_piece() piece_size = torrent_file.piece_length block_offset = 0 for i in range(int(piece_size / block_size)): request_message = Request(piece.index, block_offset, block_size) block_offset += block_size assert request_message
def load(torrent_path): torrent_file = load_torrent_file(torrent_path) if not torrent_file: return False file_path = get_download_path(torrent_file.name) fd = open_file(file_path) piece_manager = create_piece_manager(torrent_file) for piece in piece_manager.pieces: piece_data = read_piece(fd, piece) piece.add_block(piece_data) if piece.piece_done(): piece.piece_written() close_file(fd) return torrent_file, piece_manager
async def test_write_file(): path = _files_list()[3] #only not multi file in _file_list torrent_file = generate_torrent_file(path) file_genrator(torrent_file) delete_file_for_test_if_exsit(torrent_file) file_genrator(torrent_file) piece_manager = create_piece_manager(torrent_file) asyncio.gather(write_pieces_to_memory(torrent_file, piece_manager.done_queue)) for piece in piece_manager.pieces: full_piece_with_bytes(piece) await piece_manager.put_in_queue(piece) piece.piece_written() await piece_manager.done_queue.put(None) check_all_data_wirtten_in_right_place(torrent_file, piece_manager) #close process delete_file_for_test_if_exsit(torrent_file)
async def test_get_piece_many_pieces_at_the_same_time(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) await gathering(piece_manager)