Skip to content

Commit

Permalink
properly mock LibtorrentDownloadImpl in test_multifile_torrent and te…
Browse files Browse the repository at this point in the history
…st_restart
  • Loading branch information
brussee committed Jul 13, 2016
1 parent 7029916 commit 5fdcbb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
12 changes: 6 additions & 6 deletions Tribler/Core/Libtorrent/LibtorrentMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,19 @@ def add_torrent(self, torrentdl, atp):

if infohash in self.metainfo_requests:
self._logger.info("killing get_metainfo request for %s", infohash)
handle = self.metainfo_requests.pop(infohash)['handle']
if handle:
ltsession.remove_torrent(handle, 0)
request_handle = self.metainfo_requests.pop(infohash)['handle']
if request_handle:
ltsession.remove_torrent(request_handle, 0)

handle = ltsession.add_torrent(encode_atp(atp))
infohash = str(handle.info_hash())
torrent_handle = ltsession.add_torrent(encode_atp(atp))
infohash = str(torrent_handle.info_hash())
if infohash in self.torrents:
raise DuplicateDownloadException()
self.torrents[infohash] = (torrentdl, ltsession)

self._logger.debug("added torrent %s", infohash)

return handle
return torrent_handle

def remove_torrent(self, torrentdl, removecontent=False):
handle = torrentdl.handle
Expand Down
50 changes: 25 additions & 25 deletions Tribler/Test/Core/Libtorrent/test_libtorrent_download_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,44 @@ def test_restart(self):
tdef = self.create_tdef()

impl = LibtorrentDownloadImpl(self.session, tdef)
impl.handle = None
impl.handle = MockObject()
impl.handle.set_priority = lambda _: None
impl.handle.set_sequential_download = lambda _: None
impl.handle.resume = lambda: None
impl.handle.status = lambda: fake_status
fake_status = MockObject()
fake_status.share_mode = False
# Create a dummy download config
impl.dlconfig = DownloadStartupConfig().dlconfig.copy()
impl.session.lm.on_download_wrapper_created = lambda _: True
impl.restart()

@deferred(timeout=20)
def test_multifile_torrent(self):
t = TorrentDef()
tdef = TorrentDef()

dn = os.path.join(TESTS_DATA_DIR, "contentdir")
t.add_content(dn, "dirintorrent")
tdef.add_content(dn, "dirintorrent")

fn = os.path.join(TESTS_DATA_DIR, "video.avi")
t.add_content(fn, os.path.join("dirintorrent", "video.avi"))
tdef.add_content(fn, os.path.join("dirintorrent", "video.avi"))

t.set_tracker("http://tribler.org/announce")
t.finalize()
tdef.set_tracker("http://tribler.org/announce")
tdef.finalize()

impl = LibtorrentDownloadImpl(self.session, t)
# Override the addtorrent because it will be called
impl.ltmgr = self.session.lm.ltmgr
impl.ltmgr.add_torrent = lambda ignored, ignored2: False
impl = LibtorrentDownloadImpl(self.session, tdef)
# Override the add_torrent because it will be called
impl.ltmgr = MockObject()
impl.ltmgr.add_torrent = lambda _, _dummy2: fake_handler
impl.set_selected_files = lambda: None
fake_handler = MockObject()
fake_handler.is_valid = lambda: True
fake_handler.status = lambda: fake_status
fake_handler.set_share_mode = lambda _: None
fake_handler.resume = lambda: None
fake_handler.resolve_countries = lambda _: None
fake_status = MockObject()
fake_status.share_mode = False
# Create a dummy download config
impl.dlconfig = DownloadStartupConfig().dlconfig.copy()
# Create a dummy pstate
Expand Down Expand Up @@ -185,20 +200,6 @@ def test_get_share_mode(self):
self.libtorrent_download_impl.handle.status().share_mode = True
self.assertTrue(self.libtorrent_download_impl.get_share_mode())

def test_set_share_mode(self):
"""
Test whether we set the right share mode in LibtorrentDownloadImpl
"""
def mocked_set_share_mode(val):
self.assertTrue(val)
mocked_set_share_mode.called = True

mocked_set_share_mode.called = False
self.libtorrent_download_impl.handle.set_share_mode = mocked_set_share_mode

self.libtorrent_download_impl.set_share_mode(True)
self.assertTrue(mocked_set_share_mode.called)

def test_set_priority(self):
"""
Test whether setting the priority calls the right methods in LibtorrentDownloadImpl
Expand Down Expand Up @@ -362,7 +363,6 @@ def test_setup_exception(self):
"""
Testing whether an exception in the setup method of LibtorrentDownloadImpl is handled correctly
"""
self.libtorrent_download_impl.handle.set_share_mode = lambda _: None
self.libtorrent_download_impl.setup()
self.assertIsInstance(self.libtorrent_download_impl.error, Exception)

Expand Down

0 comments on commit 5fdcbb1

Please sign in to comment.