Skip to content

Commit

Permalink
Renamed popular community to popularity community and added identifie…
Browse files Browse the repository at this point in the history
…r to subscription request
  • Loading branch information
xoriole committed Jun 28, 2018
1 parent 63a1847 commit c99e53e
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 138 deletions.
4 changes: 2 additions & 2 deletions Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ def load_ipv8_overlays(self):
self.ipv8.strategies.append((RandomWalk(self.market_community), 20))

# Popular Community
if self.session.config.get_popular_community_enabled():
from Tribler.community.popular.community import PopularityCommunity
if self.session.config.get_popularity_community_enabled():
from Tribler.community.popularity.community import PopularityCommunity

local_peer = Peer(self.session.trustchain_keypair)

Expand Down
2 changes: 1 addition & 1 deletion Tribler/Core/Config/config.spec
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ enabled = boolean(default=True)
sources = string_list(default=list())
max_disk_space = integer(min=0, default=53687091200)

[popular_community]
[popularity_community]
enabled = boolean(default=True)
cache_dir = string(default=health_cache)
8 changes: 4 additions & 4 deletions Tribler/Core/Config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ def get_dummy_wallets_enabled(self):

# Popular Community

def get_popular_community_enabled(self):
return self.config['popular_community']['enabled']
def get_popularity_community_enabled(self):
return self.config['popularity_community']['enabled']

def set_popular_community_enabled(self, value):
self.config['popular_community']['enabled'] = value
def set_popularity_community_enabled(self, value):
self.config['popularity_community']['enabled'] = value

# Torrent store

Expand Down
6 changes: 3 additions & 3 deletions Tribler/Core/Modules/search_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def search_for_torrents(self, keywords):
break

self._current_keywords = keywords
# If popular community is enabled, send the search request there as well
if self.session.lm.popular_community:
self.session.lm.popular_community.send_torrent_search_request(keywords)
# If popularity community is enabled, send the search request there as well
if self.session.lm.popularity_community:
self.session.lm.popularity_community.send_torrent_search_request(keywords)

return nr_requests_made

Expand Down
8 changes: 4 additions & 4 deletions Tribler/Core/TorrentChecker/torrent_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Tribler.Core.TorrentChecker.session import create_tracker_session, FakeDHTSession, UdpSocketManager
from Tribler.Core.Utilities.tracker_utils import MalformedTrackerURLException
from Tribler.Core.simpledefs import NTFY_TORRENTS
from Tribler.community.popular.repository import TYPE_TORRENT_HEALTH
from Tribler.community.popularity.repository import TYPE_TORRENT_HEALTH
from Tribler.dispersy.util import blocking_call_on_reactor_thread, call_on_reactor_thread
from Tribler.pyipv8.ipv8.taskmanager import TaskManager

Expand Down Expand Up @@ -199,7 +199,7 @@ def on_gui_request_completed(self, infohash, result):

self._update_torrent_result(torrent_update_dict)

# Add this result to popular community to publish to subscribers
# Add this result to popularity community to publish to subscribers
self.publish_torrent_result(torrent_update_dict)

return final_response
Expand Down Expand Up @@ -336,7 +336,7 @@ def publish_torrent_result(self, response):
self._logger.info("Not publishing zero seeded torrents")
return
content = (response['infohash'], response['seeders'], response['leechers'], response['last_check'])
if self.tribler_session.lm.popular_community:
self.tribler_session.lm.popular_community.queue_content(TYPE_TORRENT_HEALTH, content)
if self.tribler_session.lm.popularity_community:
self.tribler_session.lm.popularity_community.queue_content(TYPE_TORRENT_HEALTH, content)
else:
self._logger.info("Popular community not available to publish torrent checker result")
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
import string

from Tribler.Test.Core.base_test import MockObject
from Tribler.community.popular import constants
from Tribler.community.popular.community import PopularityCommunity, MSG_TORRENT_HEALTH_RESPONSE, \
from Tribler.community.popularity import constants
from Tribler.community.popularity.community import PopularityCommunity, MSG_TORRENT_HEALTH_RESPONSE, \
MSG_CHANNEL_HEALTH_RESPONSE, ERROR_UNKNOWN_PEER, ERROR_NO_CONTENT, \
ERROR_UNKNOWN_RESPONSE
from Tribler.community.popular.constants import SEARCH_TORRENT_REQUEST, MSG_TORRENT_INFO_RESPONSE, MSG_SUBSCRIPTION
from Tribler.community.popular.payload import SearchResponseItemPayload, TorrentInfoResponsePayload, \
from Tribler.community.popularity.constants import SEARCH_TORRENT_REQUEST, MSG_TORRENT_INFO_RESPONSE, MSG_SUBSCRIPTION
from Tribler.community.popularity.payload import SearchResponseItemPayload, TorrentInfoResponsePayload, \
TorrentHealthPayload, ContentSubscription
from Tribler.community.popular.repository import TYPE_TORRENT_HEALTH
from Tribler.community.popularity.repository import TYPE_TORRENT_HEALTH
from Tribler.pyipv8.ipv8.test.base import TestBase
from Tribler.pyipv8.ipv8.test.mocking.ipv8 import MockIPv8
from Tribler.pyipv8.ipv8.test.util import twisted_wrapper


class TestPopularCommunityBase(TestBase):
class TestPopularityCommunityBase(TestBase):
NUM_NODES = 2

def setUp(self):
super(TestPopularCommunityBase, self).setUp()
super(TestPopularityCommunityBase, self).setUp()
self.initialize(PopularityCommunity, self.NUM_NODES)

def create_node(self, *args, **kwargs):
Expand Down Expand Up @@ -101,7 +101,7 @@ def update_from_torrent_search_results(self, search_results):
pass


class TestPopularCommunity(TestPopularCommunityBase):
class TestPopularityCommunity(TestPopularityCommunityBase):
__testing__ = False
NUM_NODES = 2

Expand Down Expand Up @@ -274,49 +274,6 @@ def test_publish_no_content(self):
# Restore logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_subscription(self):
"""
Tests sending popular content subscription response.
"""
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

self.nodes[0].overlay.create_message_packet = lambda _type, _payload: \
self.fake_create_message_packet(self.nodes[0], _type, _payload)
self.nodes[0].overlay.broadcast_message = lambda packet, peer: \
self.fake_broadcast_message(self.nodes[0], packet, peer)

# Two default peers
default_peers = [self.create_node() for _ in range(2)]
# Assuming only one is connected
self.nodes[0].overlay.get_peers = lambda: default_peers[:1]

# Case1: Try to send subscribe response to non-connected peer
self.nodes[0].unknown_peer_found = False
self.nodes[0].logger_error_called = False
self.nodes[0].overlay.send_subscription_status(default_peers[1], subscribed=True)
yield self.deliver_messages()

# Expected unknown peer error log
self.assertTrue(self.nodes[0].logger_error_called)
self.assertTrue(self.nodes[0].unknown_peer_found)

# Case2: Try to send response to the connected peer
self.nodes[0].broadcast_called = False
self.nodes[0].broadcast_packet_type = None
self.nodes[0].overlay.send_subscription_status(default_peers[0], subscribed=True)
yield self.deliver_messages()

# Expect message to be sent
self.assertTrue(self.nodes[0].packet_created, "Create packet failed")
self.assertEqual(self.nodes[0].packet_type, MSG_SUBSCRIPTION, "Unexpected payload type found")
self.assertTrue(self.nodes[0].broadcast_called, "Should send a message to the peer")
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

# Restore logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_torrent_health_response(self):
"""
Expand Down Expand Up @@ -582,8 +539,11 @@ def test_on_subscription_status1(self):
Tests receiving subscription status.
"""
subscribe = True
payload = ContentSubscription(subscribe)
identifier = 123123123
payload = ContentSubscription(identifier, subscribe)
data = self.nodes[1].overlay.create_message_packet(MSG_SUBSCRIPTION, payload)
# Set the cache request
self.nodes[0].overlay.request_cache.pop = lambda prefix, identifer: MockObject()

yield self.introduce_nodes()
self.assertEqual(len(self.nodes[0].overlay.publishers), 0)
Expand All @@ -601,22 +561,31 @@ def test_on_subscription_status_with_unsubscribe(self):
yield self.introduce_nodes()
self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
self.assertEqual(len(self.nodes[0].overlay.publishers), 1)
# Set the cache request
self.nodes[0].overlay.request_cache.pop = lambda prefix, identifer: MockObject()

subscribe = False
payload = ContentSubscription(subscribe)
identifier = 123123123
payload = ContentSubscription(identifier, subscribe)
data = self.nodes[1].overlay.create_message_packet(MSG_SUBSCRIPTION, payload)

self.nodes[0].overlay.on_subscription_status(self.nodes[1].my_peer.address, data)
yield self.deliver_messages()

self.assertEqual(len(self.nodes[0].overlay.publishers), 0)

@twisted_wrapper(5)
@twisted_wrapper
def test_search_request_response(self):
self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[1].overlay.content_repository = MockRepository()
self.nodes[1].overlay.publish_latest_torrents = lambda *args, **kwargs: None

def fake_process_torrent_search_response(peer):
peer.called_process_torrent_search_response = True

self.nodes[0].overlay.process_torrent_search_response = lambda query, payload: \
fake_process_torrent_search_response(self.nodes[0])

yield self.introduce_nodes()
self.nodes[0].overlay.subscribe_peers()
yield self.deliver_messages()
Expand All @@ -627,6 +596,9 @@ def test_search_request_response(self):

yield self.deliver_messages()

self.assertTrue(self.nodes[0].called_process_torrent_search_response)


@twisted_wrapper
def test_send_content_info_request(self):
self.nodes[0].overlay.content_repository = MockRepository()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import string
from unittest import TestCase

from Tribler.community.popular.payload import SearchResponsePayload, SearchResponseItemPayload, ContentInfoRequest, \
from Tribler.community.popularity.payload import SearchResponsePayload, SearchResponseItemPayload, ContentInfoRequest, \
Pagination, ContentInfoResponse, ContentSubscription, TorrentHealthPayload, ChannelHealthPayload
from Tribler.pyipv8.ipv8.messaging.serialization import Serializer

Expand All @@ -21,13 +21,15 @@ def random_infohash(self):
def test_content_subscription(self):
""" Test serialization/deserialization of Content subscription """
subscribe = True
subscription = ContentSubscription(subscribe)
identifier = 123123
subscription = ContentSubscription(identifier, subscribe)
serialized = self.serializer.pack_multiple(subscription.to_pack_list())

# Deserialize and test it
(deserialized, _) = self.serializer.unpack_multiple(ContentSubscription.format_list, serialized)
deserialized_subscription = ContentSubscription.from_unpack_list(*deserialized)

self.assertEqual(deserialized_subscription.identifier, identifier)
self.assertTrue(deserialized_subscription.subscribe)

def test_torrent_health_payload(self):
Expand Down Expand Up @@ -169,9 +171,9 @@ def test_content_info_response(self):
serialized_response = self.serializer.pack_multiple(in_response.to_pack_list())

# Deserialize request and test it
(deserialized_ressponse, _) = self.serializer.unpack_multiple(ContentInfoResponse.format_list,
(deserialized_response, _) = self.serializer.unpack_multiple(ContentInfoResponse.format_list,
serialized_response)
out_request = ContentInfoResponse.from_unpack_list(*deserialized_ressponse)
out_request = ContentInfoResponse.from_unpack_list(*deserialized_response)
self.assertEqual(in_response.identifier, out_request.identifier)
self.assertEqual(in_response.response, out_request.response)
self.assertEqual(in_response.content_type, out_request.content_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import unittest

from Tribler.Test.Core.base_test import MockObject
from Tribler.community.popular.payload import TorrentHealthPayload
from Tribler.community.popular.repository import ContentRepository, DEFAULT_FRESHNESS_LIMIT
from Tribler.community.popularity.payload import TorrentHealthPayload
from Tribler.community.popularity.repository import ContentRepository, DEFAULT_FRESHNESS_LIMIT


class TestContentRepository(unittest.TestCase):
Expand Down Expand Up @@ -129,14 +129,15 @@ def test_update_torrent_info(self):
def fake_update_torrent(ref):
ref.called_update_torrent = True

self.content_repository.torrent_db.updateTorrent = lambda infohash, **kw: fake_update_torrent(self.content_repository)
self.content_repository.torrent_db.updateTorrent = lambda infohash, **kw: \
fake_update_torrent(self.content_repository)
self.content_repository.has_torrent = lambda infohash: False
torrent_info_response = MockObject()
torrent_info_response.infohash = 'a' * 20

torrent_info_response.name = 'ubuntu',
torrent_info_response.name = 'ubuntu'
torrent_info_response.length = 123
torrent_info_response.creation_date =123123123
torrent_info_response.creation_date = 123123123
torrent_info_response.num_files = 2
torrent_info_response.comment = 'Ubuntu ISO'

Expand Down Expand Up @@ -217,23 +218,21 @@ def test_search_channel(self):
def random_string(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))

def random_infohash():
return ''.join(random.choice('0123456789abcdef') for _ in range(20))

sample_channels = []
for index in range(10):
dbid = index
cid = random_string(size=20)
name = random_string()
description = random_string(20)
nr_torrents = random.randint(1, 10)
nr_favorite = random.randint(1,10)
nr_favorite = random.randint(1, 10)
nr_spam = random.randint(1, 10)
my_vote = 1
modified = random.randint(1, 10000000)
relevance_score = 0.0

sample_channels.append([dbid, cid, name, description, nr_torrents, nr_favorite, nr_spam, my_vote, modified, relevance_score])
sample_channels.append([dbid, cid, name, description, nr_torrents, nr_favorite, nr_spam, my_vote,
modified, relevance_score])

def fake_torrentdb_search_channels(_):
return sample_channels
Expand Down Expand Up @@ -289,8 +288,7 @@ def get_torrent(torrent_as_list):
'creation_date': torrent_as_list[5],
'seeders': torrent_as_list[6],
'leechers': torrent_as_list[7],
'cid': torrent_as_list[8],
}
'cid': torrent_as_list[8]}

def fake_update_torrent(ref):
ref.called_update_torrent = True
Expand Down
8 changes: 4 additions & 4 deletions Tribler/Test/Core/Config/test_tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ def test_get_set_methods_preview_channel_community(self):
self.tribler_config.set_preview_channel_community_enabled(True)
self.assertEqual(self.tribler_config.get_preview_channel_community_enabled(), True)

def test_get_set_methods_popular_community(self):
def test_get_set_methods_popularity_community(self):
"""
Check whether popular community get and set methods are working as expected.
Check whether popularity community get and set methods are working as expected.
"""
self.tribler_config.set_popular_community_enabled(True)
self.assertEqual(self.tribler_config.get_popular_community_enabled(), True)
self.tribler_config.set_popularity_community_enabled(True)
self.assertEqual(self.tribler_config.get_popularity_community_enabled(), True)

def test_get_set_methods_watch_folder(self):
"""
Expand Down
Loading

0 comments on commit c99e53e

Please sign in to comment.