Skip to content

Commit

Permalink
Added search request & response payload structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Jun 26, 2018
1 parent 3a2e974 commit 0c3d7f1
Show file tree
Hide file tree
Showing 16 changed files with 1,635 additions and 159 deletions.
Empty file.
15 changes: 15 additions & 0 deletions Tribler/Test/Community/Market/_trial_temp/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2018-06-08 11:29:52+0200 [-] Log opened.
2018-06-08 11:29:52+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_cancel <--
2018-06-08 11:29:52+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_counter_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_create_ask <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_create_bid <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_decline_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_e2e_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_failing_payment <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_info_message <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_orderbook_sync <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_proposed_trade_timeout <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_churn_matchmaker <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_e2e_trade <--
2018-06-08 11:29:55+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_offline_matchmaker <--
2018-06-08 11:29:55+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_partial_trade <--
Empty file.
3 changes: 3 additions & 0 deletions Tribler/Test/Community/Triblerchain/_trial_temp/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2018-05-18 12:22:51+0200 [-] Log opened.
2018-05-18 12:22:51+0200 [-] --> Tribler.Test.Community.Triblerchain.test_community.TestTriblerChainCrawlerCommunity.test_crawl_request <--
2018-05-18 12:22:51+0200 [-] PythonLoggingObserver hooked up
Empty file.
190 changes: 190 additions & 0 deletions Tribler/Test/Community/Tunnel/FullSession/_trial_temp/test.log

Large diffs are not rendered by default.

Empty file.
456 changes: 456 additions & 0 deletions Tribler/Test/Community/_trial_temp/test.log

Large diffs are not rendered by default.

175 changes: 132 additions & 43 deletions Tribler/Test/Community/popular/test_community.py

Large diffs are not rendered by default.

179 changes: 179 additions & 0 deletions Tribler/Test/Community/popular/test_payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import random
import string
from unittest import TestCase

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


class TestSerializer(TestCase):

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

def random_string(self, size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))

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

def test_content_subscription(self):
""" Test serialization/deserialization of Content subscription """
subscribe = True
subscription = ContentSubscription(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.assertTrue(deserialized_subscription.subscribe)

def test_torrent_health_payload(self):
""" Test serialization/deserialization of Torrent health payload """
infohash = 'a' * 20
num_seeders = 10
num_leechers = 5
timestamp = 123123123

health_payload = TorrentHealthPayload(infohash, num_seeders, num_leechers, timestamp)
serialized = self.serializer.pack_multiple(health_payload.to_pack_list())

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

self.assertEqual(infohash, deserialized_payload.infohash)
self.assertEqual(num_seeders, deserialized_payload.num_seeders)
self.assertEqual(num_leechers, deserialized_payload.num_leechers)
self.assertEqual(timestamp, deserialized_payload.timestamp)

def test_channel_health_payload(self):
""" Test serialization/deserialization of Channel health payload """
channel_id = self.random_string(size=20)
num_votes = 100
num_torrents = 5
swarm_size_sum = 20
timestamp = 123123123

health_payload = ChannelHealthPayload(channel_id, num_votes, num_torrents, swarm_size_sum, timestamp)
serialized = self.serializer.pack_multiple(health_payload.to_pack_list())

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

self.assertEqual(channel_id, deserialized_payload.channel_id)
self.assertEqual(num_votes, deserialized_payload.num_votes)
self.assertEqual(num_torrents, deserialized_payload.num_torrents)
self.assertEqual(swarm_size_sum, deserialized_payload.swarm_size_sum)
self.assertEqual(timestamp, deserialized_payload.timestamp)

def test_search_result_payload_serialization(self):
""" Test serialization & deserialization of search payload """
# sample search response items
sample_items = []
for index in range(10):
infohash = self.random_infohash()
name = self.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 = self.random_string(size=20)

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

# Search identifier
identifier = 111
response_type = 1

# 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, response_type, 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[2])
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)

def test_pagination(self):
""" Test if pagination serialization & deserialization works as expected. """
page_num = 1
page_size =10
max_results = 50
more = False

page = Pagination(page_num, page_size, max_results, more)
serialized_page = page.serialize()

# Deserialize and test the parameters
deserialized_page = Pagination.deserialize(serialized_page)
self.assertEqual(page.page_number, deserialized_page.page_number)
self.assertEqual(page.page_size, deserialized_page.page_size)
self.assertEqual(page.max_results, deserialized_page.max_results)
self.assertEqual(page.more, deserialized_page.more)

def test_content_info_request(self):
""" Test serialization & deserialization of content info request """
identifier = 1
content_type = 1
query_list = "ubuntu 18.04".split()
limit = 10

# Serialize request
in_request = ContentInfoRequest(identifier, content_type, query_list, limit)
serialized_request = self.serializer.pack_multiple(in_request.to_pack_list())

# Deserialize request and test it
(deserialized_request, _) = self.serializer.unpack_multiple(ContentInfoRequest.format_list, serialized_request)
out_request = ContentInfoRequest.from_unpack_list(*deserialized_request)
self.assertEqual(in_request.identifier, out_request.identifier)
self.assertEqual(in_request.query_list, out_request.query_list)
self.assertEqual(in_request.content_type, out_request.content_type)
self.assertEqual(in_request.limit, out_request.limit)

def test_content_info_response(self):
""" Test serialization & deserialization of content info response """
identifier = 1
content_type = 1
response = self.random_string(size=128)
more = True
pagination = Pagination(1, 10, 50, more)

# Serialize request
in_response = ContentInfoResponse(identifier, content_type, response, pagination)
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, serialized_response)
out_request = ContentInfoResponse.from_unpack_list(*deserialized_ressponse)
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)
self.assertEqual(in_response.pagination.page_number, out_request.pagination.page_number)
self.assertEqual(in_response.pagination.page_size, out_request.pagination.page_size)
self.assertEqual(in_response.pagination.max_results, out_request.pagination.max_results)
25 changes: 15 additions & 10 deletions Tribler/Test/Community/popular/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class TestContentRepository(unittest.TestCase):

def setUp(self):
torrent_db = MockObject()
self.content_repository = ContentRepository(torrent_db)
channel_db = MockObject()
self.content_repository = ContentRepository(torrent_db, channel_db)

def test_add_content(self):
"""
Expand Down Expand Up @@ -57,8 +58,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 +71,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 +137,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 0c3d7f1

Please sign in to comment.