diff --git a/tests/integration/test_file_commands.py b/tests/integration/test_file_commands.py index a8800fd25..9f660d051 100644 --- a/tests/integration/test_file_commands.py +++ b/tests/integration/test_file_commands.py @@ -70,6 +70,7 @@ class FileCommands(CommandTestCase): async def test_incomplete_downloads_erases_output_file_on_stop(self): claim = await self.make_claim('foo', '0.01') sd_hash = claim['output']['value']['stream']['source']['source'] + file_info = self.daemon.jsonrpc_file_list()[0] await self.daemon.jsonrpc_file_delete(claim_name='foo') all_except_sd = [ blob_hash for blob_hash in self.server.blob_manager.completed_blob_hashes if blob_hash != sd_hash @@ -78,7 +79,42 @@ class FileCommands(CommandTestCase): resp = await self.daemon.jsonrpc_get('lbry://foo', timeout=2) self.assertIn('error', resp) + self.assertFalse(os.path.isfile(os.path.join(self.daemon.conf.download_dir, file_info['file_name']))) + + async def test_incomplete_downloads_retry(self): + claim = await self.make_claim('foo', '0.01') + sd_hash = claim['output']['value']['stream']['source']['source'] + await self.daemon.jsonrpc_file_delete(claim_name='foo') + all_except_sd = [ + blob_hash for blob_hash in self.server.blob_manager.completed_blob_hashes if blob_hash != sd_hash + ] + + # backup server blobs + for blob_hash in all_except_sd: + blob = self.server_blob_manager.get_blob(blob_hash) + os.rename(blob.file_path, blob.file_path + '__') + + # erase all except sd blob + await self.server.blob_manager.delete_blobs(all_except_sd) + + # fails, as expected + resp = await self.daemon.jsonrpc_get('lbry://foo', timeout=2) + self.assertIn('error', resp) + self.assertEqual(len(self.daemon.jsonrpc_file_list()), 1) + self.assertEqual('stopped', self.daemon.jsonrpc_file_list()[0]['status']) + + # recover blobs + for blob_hash in all_except_sd: + blob = self.server_blob_manager.get_blob(blob_hash) + os.rename(blob.file_path + '__', blob.file_path) + self.server_blob_manager.blobs.clear() + await self.server_blob_manager.blob_completed(self.server_blob_manager.get_blob(blob_hash)) + resp = await self.daemon.jsonrpc_get('lbry://foo', timeout=2) + await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) + self.assertNotIn('error', resp) file_info = self.daemon.jsonrpc_file_list()[0] - self.assertTrue(os.path.isfile(os.path.join(file_info['download_path']))) - await self.daemon.jsonrpc_file_set_status('stop', sd_hash=sd_hash) - self.assertFalse(os.path.isfile(os.path.join(file_info['download_path']))) + self.assertEqual(file_info['blobs_completed'], file_info['blobs_in_stream']) + + async def wait_files_to_complete(self): + while self.daemon.jsonrpc_file_list(status='running'): + await asyncio.sleep(0.01)