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 Jul 2, 2016
1 parent 83264c5 commit 069fb18
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 66 deletions.
33 changes: 2 additions & 31 deletions Tribler/Test/Core/CreditMining/test_creditmining_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -270,7 +271,7 @@ def check_archive(_):


@skip("Disabled credit mining tests until they are stable again")
class TestBoostingManagerSysChannel(TestBoostingManagerSys):
class TestBoostingManagerSysChannel(TestBoostingManagerSys, BaseTestChannel):
"""
testing class for channel source
"""
Expand All @@ -284,36 +285,8 @@ def __init__(self, *argv, **kwargs):

def setUp(self, autoload_discovery=True):
super(TestBoostingManagerSysChannel, self).setUp()
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)

def on_dispersy_create_votecast(self, cid, vote, _):
"""
Check whether we have the expected parameters when this method is called.
"""
self.assertEqual(cid, self.expected_votecast_cid)
self.assertEqual(vote, self.expected_votecast_vote)
self.create_votecast_called = True

@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}

def set_boosting_settings(self):
super(TestBoostingManagerSysChannel, self).set_boosting_settings()
self.bsettings.swarm_interval = 1
Expand Down Expand Up @@ -557,6 +530,4 @@ def check_torrents_channel(src, defer_param=None):

def tearDown(self):
self.session.lm.dispersy._communities['allchannel'].cancel_all_pending_tasks()
self.session.lm.dispersy.cancel_all_pending_tasks()
self.session.lm.dispersy = None
super(TestBoostingManagerSysChannel, self).tearDown()
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()
4 changes: 2 additions & 2 deletions Tribler/Test/Core/Modules/Channel/test_channel_rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def setUp(self, annotate=True):
self.channel_rss.initialize()
self.should_shutdown_after_test = True

def tearDown(self, annotate=True):
super(TestChannelRss, self).tearDown(annotate=annotate)
def tearDown(self):
super(TestChannelRss, self).tearDown()

if self.should_shutdown_after_test:
self.channel_rss.shutdown()
Expand Down
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 @@ -24,9 +24,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 @@ -39,22 +38,7 @@ def on_dispersy_create_votecast(self, cid, vote, _):
self.assertEqual(vote, self.expected_votecast_vote)
self.create_votecast_called = True

@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}

def tearDown(self):
self.session.lm.dispersy = None
super(TestChannelsSubscriptionEndpoint, self).tearDown()

@deferred(timeout=10)
def test_subscribe_channel_not_exist(self):
Expand Down
4 changes: 2 additions & 2 deletions Tribler/Test/Core/Modules/RestApi/base_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from zope.interface import implements
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


Expand All @@ -25,15 +26,14 @@ 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.
"""

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

def do_request(self, endpoint, request_type, post_data):
agent = Agent(reactor)
Expand Down
2 changes: 1 addition & 1 deletion Tribler/Test/Core/Modules/RestApi/test_events_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def dataReceived(self, data):
class TestEventsEndpoint(AbstractApiTest):

def setUp(self, autoload_discovery=True):
super(TestEventsEndpoint, self).setUp(autoload_discovery=autoload_discovery)
super(TestEventsEndpoint, self).setUp()
self.events_deferred = Deferred()
self.socket_open_deferred = self.tribler_started_deferred.addCallback(self.open_events_socket)
self.messages_to_wait_for = 0
Expand Down

0 comments on commit 069fb18

Please sign in to comment.