-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Peers selected based on trust score - New torrent check results are shared to subscribers
- Loading branch information
Showing
12 changed files
with
631 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import random | ||
|
||
from Tribler.Test.Core.base_test import MockObject | ||
from Tribler.community.popular.community import PopularCommunity, MSG_TORRENT_HEALTH_RESPONSE | ||
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 | ||
from Tribler.pyipv8.ipv8.test.util import twisted_wrapper | ||
|
||
|
||
class TestPopularCommunityBase(TestBase): | ||
NUM_NODES = 2 | ||
|
||
def setUp(self): | ||
super(TestPopularCommunityBase, self).setUp() | ||
self.initialize(PopularCommunity, self.NUM_NODES) | ||
|
||
def create_node(self): | ||
def load_random_torrents(limit): | ||
return [ | ||
['\xfdC\xf9+V\x11A\xe7QG\xfb\xb1*6\xef\xa5\xaeu\xc2\xe0', | ||
random.randint(200, 250), random.randint(1, 10), 1525704192.166107] for _ in range(limit) | ||
] | ||
|
||
torrent_db = MockObject() | ||
torrent_db.getRecentlyCheckedTorrents = lambda limit: load_random_torrents(limit) | ||
|
||
return MockIPv8(u"curve25519", PopularCommunity, torrent_db=torrent_db) | ||
|
||
|
||
class TestPopularCommunity(TestPopularCommunityBase): | ||
__testing__ = False | ||
NUM_NODES = 2 | ||
|
||
def setUp(self): | ||
super(TestPopularCommunity, self).setUp() | ||
|
||
@twisted_wrapper | ||
def test_subscribe_peers(self): | ||
yield self.introduce_nodes() | ||
self.nodes[0].overlay.subscribe_peers() | ||
yield self.deliver_messages() | ||
|
||
# Node 0 should have a publisher added | ||
self.assertEqual(len(self.nodes[0].overlay.publishers), 1, "Expected 1 publisher") | ||
# Node 1 should have a subscriber added | ||
self.assertEqual(len(self.nodes[1].overlay.subscribers), 1, "Expected 1 subscriber") | ||
|
||
@twisted_wrapper | ||
def test_start(self): | ||
yield self.introduce_nodes() | ||
self.nodes[0].overlay.start() | ||
yield self.deliver_messages() | ||
|
||
# Node 0 should have a publisher added | ||
self.assertEqual(len(self.nodes[0].overlay.publishers), 1, "Expected 1 publisher") | ||
# Node 1 should have a subscriber added | ||
self.assertEqual(len(self.nodes[1].overlay.subscribers), 1, "Expected 1 subscriber") | ||
|
||
@twisted_wrapper | ||
def test_content_publishing(self): | ||
def on_torrent_health_response(peer, source_address, data): | ||
peer.torrent_health_response_received = True | ||
|
||
self.nodes[0].torrent_health_response_received = False | ||
self.nodes[0].overlay.decode_map[chr(MSG_TORRENT_HEALTH_RESPONSE)] = lambda source_address, data: \ | ||
on_torrent_health_response(self.nodes[0], source_address, data) | ||
|
||
yield self.introduce_nodes() | ||
self.nodes[0].overlay.subscribe_peers() | ||
yield self.deliver_messages() | ||
|
||
# Add something to queue | ||
health_info = ('a' * 20, random.randint(1, 100), random.randint(1, 10), random.randint(1, 111111)) | ||
self.nodes[1].overlay.queue_content(TYPE_TORRENT_HEALTH, health_info) | ||
|
||
self.nodes[1].overlay.publish_next_content() | ||
|
||
yield self.deliver_messages() | ||
|
||
self.assertTrue(self.nodes[0].torrent_health_response_received, "Expected to receive torrent response") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import time | ||
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 | ||
|
||
|
||
class TestContentRepository(unittest.TestCase): | ||
|
||
def setUp(self): | ||
torrent_db = MockObject() | ||
self.content_repository = ContentRepository(torrent_db) | ||
|
||
def test_add_content(self): | ||
""" | ||
Test adding and removing content works as expected. | ||
""" | ||
# Initial content queue is zero | ||
self.assertEqual(self.content_repository.num_content(), 0, "No item expected in queue initially") | ||
|
||
# Add a sample content and check the size | ||
sample_content = ('a' * 20, 6, 3, 123456789) | ||
sample_content_type = 1 | ||
self.content_repository.add_content(sample_content_type, sample_content) | ||
self.assertEqual(self.content_repository.num_content(), 1, "One item expected in queue") | ||
|
||
# Pop an item | ||
(content_type, content) = self.content_repository.pop_content() | ||
self.assertEqual(content_type, sample_content_type, "Content type should be equal") | ||
self.assertEqual(content, sample_content, "Content should be equal") | ||
|
||
# Check size again | ||
self.assertEqual(self.content_repository.num_content(), 0, "No item expected in queue") | ||
|
||
def test_get_top_torrents(self): | ||
""" | ||
Test if content repository returns expected top torrents. | ||
""" | ||
|
||
def get_fake_torrents(limit): | ||
return [[chr(x) * 20, x, 0, 1525704192] for x in range(limit)] | ||
|
||
self.content_repository.torrent_db.getRecentlyCheckedTorrents = lambda limit: get_fake_torrents(limit) | ||
|
||
limit = 10 | ||
self.assertEqual(self.content_repository.get_top_torrents(limit=limit), get_fake_torrents(limit)) | ||
|
||
def test_update_torrent_with_higher_trust(self): | ||
""" | ||
Scenario: The database torrent has still fresh last_check_time and you receive a new response from | ||
peer with trust > 1. | ||
Expect: Torrent in database is updated. | ||
""" | ||
# last_check_time for existing torrent in database | ||
db_last_time_check = time.time() - 10 | ||
# Peer trust, higher than 1 in this scenario | ||
peer_trust = 10 | ||
|
||
# Database record is expected to be updated | ||
self.assertTrue(self.try_torrent_update_with_options(db_last_time_check, peer_trust)) | ||
|
||
def test_update_torrent_with_stale_check_time(self): | ||
""" | ||
Scenario: The database torrent has stale last_check_time and you receive a new response from | ||
peer with no previous trust. | ||
Expect: Torrent in database is still updated. | ||
""" | ||
# last_check_time for existing torrent in database | ||
db_last_time_check = time.time() - DEFAULT_FRESHNESS_LIMIT | ||
# Peer trust, higher than 1 in this scenario | ||
peer_trust = 0 | ||
|
||
# Database record is expected to be updated | ||
self.assertTrue(self.try_torrent_update_with_options(db_last_time_check, peer_trust)) | ||
|
||
def try_torrent_update_with_options(self, db_last_check_time, peer_trust): | ||
""" | ||
Tries updating torrent considering the given last check time of existing torrent and a new response | ||
obtained from a peer with given peer_trust value. | ||
""" | ||
sample_infohash, seeders, leechers, timestamp = 'a' * 20, 10, 5, db_last_check_time | ||
sample_payload = TorrentHealthPayload(sample_infohash, seeders, leechers, timestamp) | ||
|
||
def update_torrent(content_repo, infohash, *args, **kw): | ||
content_repo.update_torrent_called = True | ||
|
||
def get_torrent(infohash, keys=None, include_mypref=False): | ||
return {'infohash': infohash, 'num_seeders': seeders, | ||
'num_leechers': leechers, 'last_tracker_check': timestamp} | ||
|
||
self.content_repository.torrent_db.getTorrent = lambda infohash, **kw: get_torrent(infohash, **kw) | ||
self.content_repository.torrent_db.hasTorrent = lambda infohash: infohash == sample_infohash | ||
self.content_repository.torrent_db.updateTorrent = \ | ||
lambda infohash, *args, **kw: update_torrent(self.content_repository, infohash, *args, **kw) | ||
|
||
self.content_repository.update_torrent_called = False | ||
self.content_repository.update_torrent(sample_payload, peer_trust=peer_trust) | ||
|
||
return self.content_repository.update_torrent_called | ||
|
||
self.assertTrue(self.content_repository.update_torrent_called) |
Empty file.
Oops, something went wrong.