Skip to content

Commit

Permalink
WIP: Added search request & response payload structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Jun 18, 2018
1 parent 667c48f commit 4bb446e
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 70 deletions.
48 changes: 24 additions & 24 deletions Tribler/Test/Community/popular/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def test_publish_no_content(self):
"""
Tests publishing next content if no content is available.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

# Assume a subscribers exist
self.nodes[0].overlay.subscribers = [self.create_node()]
Expand All @@ -163,15 +163,15 @@ def test_publish_no_content(self):
self.assertTrue(self.nodes[0].no_content)

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

@twisted_wrapper
def test_send_popular_content_subscribe(self):
"""
Tests sending popular content subscribe request.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
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.broadcast_message = lambda packet, peer: \
self.fake_broadcast_message(self.nodes[0], packet, peer)
Expand Down Expand Up @@ -210,15 +210,15 @@ def test_send_popular_content_subscribe(self):
self.assertTrue(default_peers[0] not in self.nodes[0].overlay.publishers)

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

@twisted_wrapper
def test_send_popular_content_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)
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)
Expand Down Expand Up @@ -253,15 +253,15 @@ def test_send_popular_content_subscription(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

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

@twisted_wrapper
def test_send_torrent_health_response(self):
"""
Tests sending 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)
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)
Expand Down Expand Up @@ -297,15 +297,15 @@ def test_send_torrent_health_response(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

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

@twisted_wrapper
def test_send_channel_health_response(self):
"""
Tests sending 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)
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)
Expand Down Expand Up @@ -341,15 +341,15 @@ def test_send_channel_health_response(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

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

@twisted_wrapper
def test_on_torrent_health_response_from_unknown_peer(self):
"""
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)
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()
Expand All @@ -372,15 +372,15 @@ def fake_get_peer_from_auth(peer):
self.assertTrue(self.nodes[0].unknown_response)

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

@twisted_wrapper
def test_on_popular_content_subscribe_unknown_peer(self):
"""
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)
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()
Expand Down Expand Up @@ -408,15 +408,15 @@ def fake_publish_latest_torrents(my_peer, _peer):
self.assertTrue(self.nodes[0].unknown_peer_found)

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

@twisted_wrapper(500)
def test_on_popular_content_subscribe_ok(self):
"""
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)
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()
Expand Down Expand Up @@ -449,7 +449,7 @@ def fake_send_popular_content_subscription(my_peer):
self.assertTrue(self.nodes[0].publish_latest_torrents_called)

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

def fake_logger_error(self, my_peer, *args):
if ERROR_UNKNOWN_PEER in args[0]:
Expand Down
70 changes: 70 additions & 0 deletions Tribler/Test/Community/popular/test_payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import random
import string
from unittest import TestCase

from Tribler.community.popular.payload import SearchResponsePayload, SearchResponseItemPayload
from Tribler.pyipv8.ipv8.messaging.serialization import Serializer


class TestSerializer(TestCase):

def setUp(self):
self.serializer = Serializer()

def test_search_result_payload_serialization(self):
"""
Test serialization & deserialization of search payload
:return:
"""

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 search response items
sample_items = []
for index in range(10):
infohash = random_infohash()
name = random_string()
length = random.randint(1000, 9999)
num_files = random.randint(1, 10)
category_list = ['video', 'audio']
creation_date = random.randint(1000000, 111111111)
seeders = random.randint(10, 200)
leechers = random.randint(5, 1000)
cid = random_string(size=20)

sample_items.append(SearchResponseItemPayload(infohash, name, length, num_files, category_list,
creation_date, seeders, leechers, cid))

# Search identifier
identifier = 111

# Serialize the results
results = ''
for item in sample_items:
results += self.serializer.pack_multiple(item.to_pack_list())
serialized_results = self.serializer.pack_multiple(SearchResponsePayload(identifier, results).to_pack_list())

# De-serialize the response payload and check the identifier and get the results
response_format = SearchResponsePayload.format_list
(search_results, _) = self.serializer.unpack_multiple(response_format, serialized_results)

# De-serialize each individual search result items
item_format = SearchResponseItemPayload.format_list
(all_items, _) = self.serializer.unpack_multiple_as_list(item_format, search_results[1])
for index in xrange(len(all_items)):
response_item = SearchResponseItemPayload.from_unpack_list(*all_items[index])
sample_item = sample_items[index]

self.assertEqual(sample_item.infohash, response_item.infohash)
self.assertEqual(sample_item.name, response_item.name)
self.assertEqual(sample_item.length, response_item.length)
self.assertEqual(sample_item.num_files, response_item.num_files)
self.assertEqual(sample_item.creation_date, response_item.creation_date)
self.assertEqual(sample_item.category_list, response_item.category_list)
self.assertEqual(sample_item.seeders, response_item.seeders)
self.assertEqual(sample_item.leechers, response_item.leechers)
self.assertEqual(sample_item.cid, response_item.cid)
22 changes: 13 additions & 9 deletions Tribler/Test/Community/popular/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ def fake_logger_error(repo, *args):
repo.unknown_torrent = True
repo.logger_error_called = True

original_logger = self.content_repository._logger
self.content_repository._logger.error = lambda *args, **kw: fake_logger_error(self.content_repository, *args)
def update_torrent(repo, _):
repo.update_torrent_called = True

original_logger = self.content_repository.logger
self.content_repository.logger.error = lambda *args, **kw: fake_logger_error(self.content_repository, *args)

# Assume a fake torrent response
fake_torrent_health_payload = TorrentHealthPayload('a' * 20, 10, 4, time.time())
Expand All @@ -67,21 +70,22 @@ def fake_logger_error(repo, *args):
self.content_repository.torrent_db = None
self.content_repository.logger_error_called = False

self.content_repository.update_torrent(fake_torrent_health_payload, peer_trust=0)
self.content_repository.update_torrent_health(fake_torrent_health_payload, peer_trust=0)
self.assertTrue(self.content_repository.torrent_db_none)
self.assertTrue(self.content_repository.logger_error_called)

# Case2: torrent db does not have torrent
self.content_repository.torrent_db = MockObject()
self.content_repository.torrent_db.hasTorrent = lambda _: False
self.content_repository.torrent_db.updateTorrent = lambda infohash, *args, **kw: \
update_torrent(self.content_repository, infohash)
self.content_repository.logger_error_called = False
self.content_repository.has_torrent = lambda infohash: False

self.content_repository.update_torrent(fake_torrent_health_payload, peer_trust=0)
self.assertTrue(self.content_repository.logger_error_called)
self.assertTrue(self.content_repository.unknown_torrent)
self.content_repository.update_torrent_health(fake_torrent_health_payload, peer_trust=0)
self.assertTrue(self.content_repository.update_torrent_called)

# restore logger
self.content_repository._logger = original_logger
self.content_repository.logger = original_logger

def test_update_torrent_with_higher_trust(self):
"""
Expand Down Expand Up @@ -132,6 +136,6 @@ def get_torrent(infohash):
lambda infohash, *args, **kw: update_torrent(self.content_repository, infohash)

self.content_repository.update_torrent_called = False
self.content_repository.update_torrent(sample_payload, peer_trust=peer_trust)
self.content_repository.update_torrent_health(sample_payload, peer_trust=peer_trust)

return self.content_repository.update_torrent_called
Loading

0 comments on commit 4bb446e

Please sign in to comment.