fix and test main api

This commit is contained in:
Victor Shyba 2021-10-15 03:39:36 -03:00
parent b1db42acf4
commit e449cd01ba
2 changed files with 21 additions and 4 deletions

View file

@ -388,6 +388,7 @@ class BackgroundDownloader(Component):
self.status = {'pending': 0, 'ongoing': 0} self.status = {'pending': 0, 'ongoing': 0}
self.task: typing.Optional[asyncio.Task] = None self.task: typing.Optional[asyncio.Task] = None
self.download_loop_delay_seconds = 60 self.download_loop_delay_seconds = 60
self.finished_iteration = asyncio.Event()
@property @property
def component(self) -> 'BackgroundDownloader': def component(self) -> 'BackgroundDownloader':
@ -398,7 +399,6 @@ class BackgroundDownloader(Component):
return self.status return self.status
async def loop(self): async def loop(self):
return
db: SQLiteStorage = self.component_manager.get_component(DATABASE_COMPONENT) db: SQLiteStorage = self.component_manager.get_component(DATABASE_COMPONENT)
while True: while True:
for channel_id, download_latest, download_all in await db.get_subscriptions(): for channel_id, download_latest, download_all in await db.get_subscriptions():
@ -406,6 +406,8 @@ class BackgroundDownloader(Component):
if not amount: if not amount:
continue continue
await self.ensure_download(channel_id, amount) await self.ensure_download(channel_id, amount)
self.finished_iteration.set()
self.finished_iteration.clear()
await asyncio.sleep(self.download_loop_delay_seconds) await asyncio.sleep(self.download_loop_delay_seconds)
async def ensure_download(self, channel_id, amount): async def ensure_download(self, channel_id, amount):
@ -414,9 +416,10 @@ class BackgroundDownloader(Component):
ledger = wallet.ledger ledger = wallet.ledger
claims, _, _, _ = await ledger.claim_search( claims, _, _, _ = await ledger.claim_search(
ledger.accounts, channel_id=channel_id, order_by=['release_time', '^height']) ledger.accounts, channel_id=channel_id, order_by=['release_time', '^height'])
page = 0 offset = 0
while claims and amount > 0: while claims and amount > 0:
for claim in claims: for claim in claims:
offset += 1
if not claim.script.source or claim.has_price: if not claim.script.source or claim.has_price:
continue continue
stream = await file_manager.download_from_uri( stream = await file_manager.download_from_uri(
@ -427,9 +430,8 @@ class BackgroundDownloader(Component):
amount -= 1 amount -= 1
if amount == 0: if amount == 0:
break break
page += 1
claims, _, _, _ = await ledger.claim_search( claims, _, _, _ = await ledger.claim_search(
ledger.accounts, channel_id=channel_id, order_by=['release_time', '^height'], page=page) ledger.accounts, channel_id=channel_id, order_by=['release_time', '^height'], offset=offset)
async def start(self): async def start(self):
self.task = asyncio.create_task(self.loop()) self.task = asyncio.create_task(self.loop())

View file

@ -603,3 +603,18 @@ class TestProactiveDownloaderComponent(CommandTestCase):
# ignores reposts # ignores reposts
await proactive_downloader.ensure_download(channel_id, 4) await proactive_downloader.ensure_download(channel_id, 4)
await self.assertFileList(content1, content2) await self.assertFileList(content1, content2)
await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertEqual(0, len(await self.file_list()))
await proactive_downloader.stop()
await self.daemon.jsonrpc_channel_subscribe(channel_id, 1)
await proactive_downloader.start()
await proactive_downloader.finished_iteration.wait()
await self.assertFileList(content1)
await self.daemon.jsonrpc_file_delete(delete_all=True)
await self.daemon.jsonrpc_channel_subscribe(channel_id, download_all=True)
await proactive_downloader.stop()
await proactive_downloader.start()
await proactive_downloader.finished_iteration.wait()
await self.assertFileList(content1, content2)