Skip to content

Commit

Permalink
Resolved review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Jun 28, 2018
1 parent 2767aa5 commit 63a1847
Show file tree
Hide file tree
Showing 9 changed files with 543 additions and 384 deletions.
14 changes: 7 additions & 7 deletions Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self):
self.tunnel_community = None
self.trustchain_community = None
self.wallets = {}
self.popular_community = None
self.popularity_community = None

self.startup_deferred = Deferred()

Expand Down Expand Up @@ -290,18 +290,18 @@ def load_ipv8_overlays(self):

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

local_peer = Peer(self.session.trustchain_keypair)

self.popular_community = PopularCommunity(local_peer, self.ipv8.endpoint, self.ipv8.network,
torrent_db=self.session.lm.torrent_db, session=self.session)
self.popularity_community = PopularityCommunity(local_peer, self.ipv8.endpoint, self.ipv8.network,
torrent_db=self.session.lm.torrent_db, session=self.session)

self.ipv8.overlays.append(self.popular_community)
self.ipv8.overlays.append(self.popularity_community)

self.ipv8.strategies.append((RandomWalk(self.popular_community), 20))
self.ipv8.strategies.append((RandomWalk(self.popularity_community), 20))

self.popular_community.start()
self.popularity_community.start()

@blocking_call_on_reactor_thread
def load_dispersy_communities(self):
Expand Down
184 changes: 99 additions & 85 deletions Tribler/Test/Community/popular/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

from Tribler.Test.Core.base_test import MockObject
from Tribler.community.popular import constants
from Tribler.community.popular.community import PopularCommunity, MSG_TORRENT_HEALTH_RESPONSE, \
MSG_CHANNEL_HEALTH_RESPONSE, ERROR_UNKNOWN_PEER, MSG_SUBSCRIPTION, ERROR_NO_CONTENT, \
from Tribler.community.popular.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
from Tribler.community.popular.payload import SearchResponseItemPayload
from Tribler.community.popular.constants import SEARCH_TORRENT_REQUEST, MSG_TORRENT_INFO_RESPONSE, MSG_SUBSCRIPTION
from Tribler.community.popular.payload import SearchResponseItemPayload, TorrentInfoResponsePayload, \
TorrentHealthPayload, ContentSubscription
from Tribler.community.popular.repository import TYPE_TORRENT_HEALTH
from Tribler.pyipv8.ipv8.test.base import TestBase
from Tribler.pyipv8.ipv8.test.mocking.ipv8 import MockIPv8
Expand All @@ -19,7 +20,7 @@ class TestPopularCommunityBase(TestBase):

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

def create_node(self, *args, **kwargs):
def load_random_torrents(limit):
Expand All @@ -35,7 +36,7 @@ def load_random_torrents(limit):

channel_db = MockObject()

return MockIPv8(u"curve25519", PopularCommunity, torrent_db=torrent_db, channel_db=channel_db)
return MockIPv8(u"curve25519", PopularityCommunity, torrent_db=torrent_db, channel_db=channel_db)


class MockRepository(object):
Expand Down Expand Up @@ -466,21 +467,16 @@ def test_on_torrent_health_response_from_unknown_peer(self):
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

def fake_unpack_auth():
mock_auth = MockObject()
mock_payload = MockObject()
return mock_auth, None, mock_payload
infohash = 'a' * 20
num_seeders = 10
num_leechers = 5
timestamp = 123123123

def fake_get_peer_from_auth(peer):
return peer
payload = TorrentHealthPayload(infohash, num_seeders, num_leechers, timestamp)
source_address = ('1.1.1.1', 1024)
data = self.nodes[0].overlay.create_message_packet(MSG_TORRENT_HEALTH_RESPONSE, payload)

self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: self.nodes[1]

source_address = MockObject()
data = MockObject()

self.nodes[0].unknown_response = True
self.nodes[0].unknown_response = False
self.nodes[0].overlay.on_torrent_health_response(source_address, data)
yield self.deliver_messages()

Expand All @@ -489,113 +485,131 @@ def fake_get_peer_from_auth(peer):
# Restore logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_on_torrent_info_response_from_unknown_publisher(self):
@twisted_wrapper(500)
def test_on_torrent_health_response(self):
"""
Tests receiving torrent info response from unknown publisher
Tests receiving torrent health response from unknown peer
"""
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
def fake_update_torrent(peer):
peer.called_update_torrent = True

def fake_unpack_auth():
mock_auth = MockObject()
mock_payload = MockObject()
return mock_auth, None, mock_payload
self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[0].overlay.content_repository.update_torrent_health = lambda payload, peer_trust: \
fake_update_torrent(self.nodes[0])

self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: self.nodes[1]
infohash = 'a' * 20
num_seeders = 10
num_leechers = 5
timestamp = 123123123

source_address = MockObject()
data = MockObject()
payload = TorrentHealthPayload(infohash, num_seeders, num_leechers, timestamp)
data = self.nodes[1].overlay.create_message_packet(MSG_TORRENT_HEALTH_RESPONSE, payload)

self.nodes[0].unknown_response = False
self.nodes[0].overlay.on_torrent_info_response(source_address, data)
yield self.introduce_nodes()

# Add node 1 in publisher list of node 0
self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
self.nodes[0].overlay.on_torrent_health_response(self.nodes[1].my_peer.address, data)
yield self.deliver_messages()

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

# Restore logger
self.nodes[0].overlay.logger = original_logger
self.assertTrue(self.nodes[0].called_update_torrent)

@twisted_wrapper
def test_on_torrent_info_response(self):
"""
Tests receiving torrent info response from unknown publisher
Tests receiving torrent health 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)

def fake_update_torrent_info(peer):
peer.called_update_torrent_info = True
peer.called_update_torrent = True

def fake_unpack_auth():
mock_auth = MockObject()
mock_payload = MockObject()
return mock_auth, None, mock_payload

self.nodes[0].called_update_torrent_info = False
self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[0].overlay.content_repository.update_torrent_info = lambda payload: \
fake_update_torrent_info(self.nodes[0])
self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: self.nodes[1].my_peer

source_address = MockObject()
data = MockObject()
infohash = 'a' * 20
name = "ubuntu"
length = 100
creation_date = 123123123
num_files = 33
comment = ''

self.nodes[0].unknown_response = False
# Add the node to publisher
self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
payload = TorrentInfoResponsePayload(infohash, name, length, creation_date, num_files, comment)
data = self.nodes[1].overlay.create_message_packet(MSG_TORRENT_INFO_RESPONSE, payload)

self.nodes[0].overlay.on_torrent_info_response(source_address, data)
yield self.introduce_nodes()

# Add node 1 in publisher list of node 0
self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
self.nodes[0].overlay.on_torrent_info_response(self.nodes[1].my_peer.address, data)
yield self.deliver_messages()

self.assertFalse(self.nodes[0].unknown_response)
self.assertTrue(self.nodes[0].called_update_torrent_info)

# Restore logger
self.nodes[0].overlay.logger = original_logger
self.assertTrue(self.nodes[0].called_update_torrent)

@twisted_wrapper
def test_on_subscription_status(self):
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
def test_on_torrent_info_response_from_unknown_peer(self):
"""
Tests receiving torrent health response from unknown peer.
"""

def fake_update_torrent_info(peer):
peer.called_update_torrent_info = True

def fake_unpack_auth(subscribe):
mock_auth = MockObject()
mock_payload = MockObject()
mock_payload.subscribe = subscribe
return mock_auth, None, mock_payload
peer.called_update_torrent = True

self.nodes[0].called_update_torrent_info = False
self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[0].overlay.content_repository.update_torrent_info = lambda payload: \
fake_update_torrent_info(self.nodes[0])
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: self.nodes[1].my_peer

source_address = MockObject()
data = MockObject()
infohash = 'a' * 20
name = "ubuntu"
length = 100
creation_date = 123123123
num_files = 33
comment = ''

payload = TorrentInfoResponsePayload(infohash, name, length, creation_date, num_files, comment)
data = self.nodes[1].overlay.create_message_packet(MSG_TORRENT_INFO_RESPONSE, payload)

yield self.introduce_nodes()

self.nodes[0].called_update_torrent = False
self.nodes[0].overlay.on_torrent_info_response(self.nodes[1].my_peer.address, data)
yield self.deliver_messages()

self.assertFalse(self.nodes[0].called_update_torrent)

@twisted_wrapper
def test_on_subscription_status1(self):
"""
Tests receiving subscription status.
"""
subscribe = True
self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth(subscribe)
payload = ContentSubscription(subscribe)
data = self.nodes[1].overlay.create_message_packet(MSG_SUBSCRIPTION, payload)

# If subscribe=True in subscription status, peer is added to the publisher list
yield self.introduce_nodes()
self.assertEqual(len(self.nodes[0].overlay.publishers), 0)
self.nodes[0].overlay.on_subscription_status(source_address, data)

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), 1)

@twisted_wrapper
def test_on_subscription_status_with_unsubscribe(self):
"""
Tests receiving subscription status with unsubscribe status.
"""
yield self.introduce_nodes()
self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
self.assertEqual(len(self.nodes[0].overlay.publishers), 1)

# If subscribe=False in subscription status, peer is removed from the publisher list
subscribe = False
self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth(subscribe)
self.nodes[0].overlay.on_subscription_status(source_address, data)
payload = ContentSubscription(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)

# Restore logger
self.nodes[0].overlay.logger = original_logger
self.assertEqual(len(self.nodes[0].overlay.publishers), 0)

@twisted_wrapper(5)
def test_search_request_response(self):
Expand Down
Loading

0 comments on commit 63a1847

Please sign in to comment.