Skip to content

Commit

Permalink
Merge pull request #7387 from xoriole/fix/channel_updates
Browse files Browse the repository at this point in the history
Fixes #7386
  • Loading branch information
xoriole authored Apr 26, 2023
2 parents 620de76 + c0de8bc commit d8bc6a6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def check_channels_updates(self):
infohash = bytes(channel.infohash)
if self.download_manager.metainfo_requests.get(infohash):
continue
status = self.download_manager.get_download(infohash).get_state().get_status()
if not self.download_manager.download_exists(infohash):
self._logger.info(
"Downloading new channel version %s ver %i->%i",
Expand All @@ -200,7 +199,10 @@ def check_channels_updates(self):
channel.timestamp,
)
self.download_channel(channel)
elif status == DownloadStatus.SEEDING:
continue

channel_download = self.download_manager.get_download(infohash)
if channel_download and channel_download.get_state().get_status() == DownloadStatus.SEEDING:
self._logger.info(
"Processing previously downloaded, but unprocessed channel torrent %s ver %i->%i",
channel.dirname,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import os
import random
from asyncio import Future
from datetime import datetime
from pathlib import Path
Expand Down Expand Up @@ -35,8 +37,8 @@ def personal_channel(metadata_store):
return chan


@pytest.fixture
async def gigachannel_manager(metadata_store):
@pytest.fixture(name="gigachannel_manager")
async def gigachannel_manager_fixture(metadata_store):
chanman = GigaChannelManager(
state_dir=metadata_store.channels_dir.parent,
metadata_store=metadata_store,
Expand Down Expand Up @@ -222,6 +224,60 @@ def mock_process_channel_dir(c, _):
assert not gigachannel_manager.channels_processing_queue


@db_session
def test_check_channel_updates_for_different_states(gigachannel_manager, metadata_store):
def random_subscribed_channel():
return metadata_store.ChannelMetadata(
title=f"Channel {random.randint(0, 100)}",
public_key=os.urandom(32),
signature=os.urandom(32),
skip_key_check=True,
timestamp=123,
local_version=122,
subscribed=True,
infohash=random_infohash(),
)

# Three channels in different states based on the setup
channel_with_metainfo = random_subscribed_channel()
already_downloaded_channel = random_subscribed_channel()
non_downloaded_channel = random_subscribed_channel()

# Setup 1: metainfo is already available for channel torrent.
def mock_get_metainfo(infohash):
return MagicMock() if infohash == channel_with_metainfo.infohash else None

gigachannel_manager.download_manager.metainfo_requests = MagicMock(get=mock_get_metainfo)

# Setup 2: Only one specific channel torrent is already downloaded.
def mock_download_exists(infohash):
return infohash == already_downloaded_channel.infohash

gigachannel_manager.download_manager.download_exists = mock_download_exists

# Setup 2 (contd): We expect non-downloaded channel to be downloaded
# so mocking download_channel() method.
gigachannel_manager.download_channel = MagicMock()

# Setup 3: Downloaded channel torrent is set on Seeding state.
def mock_get_download(infohash):
if infohash != already_downloaded_channel.infohash:
return None

seeding_state = MagicMock(get_status=lambda: DownloadStatus.SEEDING)
return MagicMock(get_state=lambda: seeding_state)

gigachannel_manager.download_manager.get_download = mock_get_download

# Act
gigachannel_manager.check_channels_updates()

# Assert
gigachannel_manager.download_channel.assert_called_once_with(non_downloaded_channel)
assert len(gigachannel_manager.channels_processing_queue) == 1
assert already_downloaded_channel.infohash in gigachannel_manager.channels_processing_queue


async def test_remove_cruft_channels(torrent_template, personal_channel, gigachannel_manager, metadata_store):
remove_list = []
with db_session:
Expand Down

0 comments on commit d8bc6a6

Please sign in to comment.