Skip to content

Commit

Permalink
Move channel creation with dispersy function to more general class
Browse files Browse the repository at this point in the history
  • Loading branch information
ardhipoetra committed Nov 30, 2016
1 parent d444886 commit 648124d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 47 deletions.
21 changes: 2 additions & 19 deletions Tribler/Test/Core/CreditMining/test_creditmining_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent
from Tribler.Policies.BoostingManager import BoostingManager, BoostingSettings
from Tribler.Test.Core.CreditMining.mock_creditmining import MockLtTorrent, ResourceFailClass
from Tribler.Test.Core.Modules.Channel.base_test_channel import BaseTestChannel
from Tribler.Test.test_as_server import TestAsServer
from Tribler.Test.common import TORRENT_UBUNTU_FILE, TORRENT_UBUNTU_FILE_INFOHASH, TESTS_DATA_DIR
from Tribler.Test.util import prepare_xml_rss
Expand Down Expand Up @@ -274,7 +275,7 @@ def check_archive(_):
return d


class TestBoostingManagerSysChannel(TestBoostingManagerSys):
class TestBoostingManagerSysChannel(TestBoostingManagerSys, BaseTestChannel):
"""
testing class for channel source
"""
Expand All @@ -293,24 +294,6 @@ def setUp(self, autoload_discovery=True):
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
self.channel_db_handler._get_my_dispersy_cid = lambda: "myfakedispersyid"

def insert_channel_in_db(self, dispersy_cid, peer_id, name, description):
return self.channel_db_handler.on_channel_from_dispersy(dispersy_cid, peer_id, name, description)

def insert_torrents_into_channel(self, torrent_list):
self.channel_db_handler.on_torrents_from_dispersy(torrent_list)

@blocking_call_on_reactor_thread
def create_fake_allchannel_community(self):
"""
This method creates a fake AllChannel community so we can check whether a request is made in the community
when doing stuff with a channel.
"""
self.session.lm.dispersy._database.open()
fake_member = DummyMember(self.session.lm.dispersy, 1, "a" * 20)
member = self.session.lm.dispersy.get_new_member(u"curve25519")
fake_community = AllChannelCommunity(self.session.lm.dispersy, fake_member, member)
self.session.lm.dispersy._communities = {"allchannel": fake_community}

def set_boosting_settings(self):
super(TestBoostingManagerSysChannel, self).set_boosting_settings()
self.bsettings.swarm_interval = 1
Expand Down
53 changes: 49 additions & 4 deletions Tribler/Test/Core/Modules/Channel/base_test_channel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from Tribler.Test.Core.base_test import TriblerCoreTest, MockObject
from Tribler.Core.simpledefs import NTFY_CHANNELCAST
from Tribler.Core.simpledefs import NTFY_VOTECAST
from Tribler.Test.Core.base_test import MockObject
from Tribler.Test.test_as_server import TestAsServer
from Tribler.community.allchannel.community import AllChannelCommunity
from Tribler.dispersy.dispersy import Dispersy
from Tribler.dispersy.endpoint import ManualEnpoint
from Tribler.dispersy.member import DummyMember
from Tribler.dispersy.util import blocking_call_on_reactor_thread


class BaseTestChannel(TriblerCoreTest):
class BaseTestChannel(TestAsServer):

def setUp(self, annotate=True):
def setUp(self, annotate=True, autoload_discovery=True):
"""
Setup some classes and files that are used by the tests in this module.
"""
super(BaseTestChannel, self).setUp(annotate=annotate)
super(BaseTestChannel, self).setUp(autoload_discovery=autoload_discovery)

if annotate:
self.annotate(self._testMethodName, start=True)

self.fake_session = MockObject()
self.fake_session.get_state_dir = lambda: self.session_base_dir
Expand All @@ -22,3 +33,37 @@ def setUp(self, annotate=True):
self.fake_channel_community.get_channel_id = lambda: 42
self.fake_channel_community.cid = 'a' * 20
self.fake_channel_community.get_channel_name = lambda: "my fancy channel"

self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
self.votecast_db_handler = self.session.open_dbhandler(NTFY_VOTECAST)

self.session.get_dispersy = lambda: True
self.session.lm.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())

def setUpPreSession(self):
super(BaseTestChannel, self).setUpPreSession()
self.config.set_megacache(True)

def insert_channel_in_db(self, dispersy_cid, peer_id, name, description):
return self.channel_db_handler.on_channel_from_dispersy(dispersy_cid, peer_id, name, description)

def insert_torrents_into_channel(self, torrent_list):
self.channel_db_handler.on_torrents_from_dispersy(torrent_list)

@blocking_call_on_reactor_thread
def create_fake_allchannel_community(self):
"""
This method creates a fake AllChannel community so we can check whether a request is made in the community
when doing stuff with a channel.
"""
self.session.lm.dispersy._database.open()
fake_member = DummyMember(self.session.lm.dispersy, 1, "a" * 20)
member = self.session.lm.dispersy.get_new_member(u"curve25519")
fake_community = AllChannelCommunity(self.session.lm.dispersy, fake_member, member)
self.session.lm.dispersy._communities = {"allchannel": fake_community}
return fake_community

def tearDown(self):
self.session.lm.dispersy.cancel_all_pending_tasks()
self.session.lm.dispersy = None
super(BaseTestChannel, self).tearDown()
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,11 @@ class AbstractTestChannelsEndpoint(AbstractApiTest):

def setUp(self, autoload_discovery=True):
super(AbstractTestChannelsEndpoint, self).setUp(autoload_discovery)
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
self.votecast_db_handler = self.session.open_dbhandler(NTFY_VOTECAST)
self.channel_db_handler._get_my_dispersy_cid = lambda: "myfakedispersyid"

def insert_channel_in_db(self, dispersy_cid, peer_id, name, description):
return self.channel_db_handler.on_channel_from_dispersy(dispersy_cid, peer_id, name, description)

def vote_for_channel(self, cid, vote_time):
self.votecast_db_handler.on_votes_from_dispersy([[cid, None, 'random', 2, vote_time]])

def insert_torrents_into_channel(self, torrent_list):
self.channel_db_handler.on_torrents_from_dispersy(torrent_list)

def create_my_channel(self, name, description):
self.channel_db_handler._get_my_dispersy_cid = lambda: "myfakedispersyid"
self.channel_db_handler.on_channel_from_dispersy('fakedispersyid', None, name, description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ def setUp(self, autoload_discovery=True):
self.expected_votecast_vote = None
self.create_votecast_called = False

self.session.get_dispersy = lambda: True
self.session.lm.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
self.create_fake_allchannel_community()
fake_community = self.create_fake_allchannel_community()
fake_community.disp_create_votecast = self.on_dispersy_create_votecast

for i in xrange(0, 10):
self.insert_channel_in_db('rand%d' % i, 42 + i, 'Test channel %d' % i, 'Test description %d' % i)
Expand All @@ -43,18 +42,6 @@ def on_dispersy_create_votecast(self, cid, vote, _):
self.create_votecast_called = True
return succeed(None)

@blocking_call_on_reactor_thread
def create_fake_allchannel_community(self):
"""
This method creates a fake AllChannel community so we can check whether a request is made in the community
when doing stuff with a channel.
"""
self.session.lm.dispersy._database.open()
fake_member = DummyMember(self.session.lm.dispersy, 1, "a" * 20)
member = self.session.lm.dispersy.get_new_member(u"curve25519")
fake_community = AllChannelCommunity(self.session.lm.dispersy, fake_member, member)
fake_community.disp_create_votecast = self.on_dispersy_create_votecast
self.session.lm.dispersy._communities = {"allchannel": fake_community}

@blocking_call_on_reactor_thread
@inlineCallbacks
Expand Down
3 changes: 2 additions & 1 deletion Tribler/Test/Core/Modules/RestApi/base_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from Tribler.Core.Utilities.network_utils import get_random_port
from Tribler.Core.Utilities.twisted_thread import reactor
from Tribler.Core.version import version_id
from Tribler.Test.Core.Modules.Channel.base_test_channel import BaseTestChannel
from Tribler.Test.test_as_server import TestAsServer
from Tribler.dispersy.util import blocking_call_on_reactor_thread

Expand All @@ -34,7 +35,7 @@ def startProducing(self, consumer):
return succeed(None)


class AbstractBaseApiTest(TestAsServer):
class AbstractBaseApiTest(BaseTestChannel):
"""
Tests for the Tribler HTTP API should create a subclass of this class.
"""
Expand Down

0 comments on commit 648124d

Please sign in to comment.