class TestLaunchManyCore(TriblerCoreTest): """ This class contains various small unit tests for the LaunchManyCore class. """ DATA_DIR = os.path.join( os.path.abspath(os.path.dirname(os.path.realpath(__file__))), 'data') def setUp(self, annotate=True): TriblerCoreTest.setUp(self, annotate=annotate) self.lm = TriblerLaunchMany() self.lm.session_lock = NoDispersyRLock() self.lm.session = MockObject() self.lm.session.config = MockObject() self.lm.session.config.get_max_upload_rate = lambda: 100 self.lm.session.config.get_max_download_rate = lambda: 100 self.lm.session.config.get_default_number_hops = lambda: 0 # Ignore notifications mock_notifier = MockObject() mock_notifier.notify = lambda *_: None self.lm.session.notifier = mock_notifier @raises(ValueError) def test_add_tdef_not_finalized(self): """ Testing whether a ValueError is raised when a non-finalized tdef is added as download. """ self.lm.add(TorrentDef(), None) def test_load_download_pstate(self): """ Testing whether a pstate is successfully loaded """ config_file_path = os.path.abspath( os.path.join(self.DATA_DIR, u"config_files", u"config1.conf")) config = self.lm.load_download_pstate(config_file_path) self.assertIsInstance(config, CallbackConfigParser) self.assertEqual(config.get('general', 'version'), 11) @deferred(timeout=10) def test_dlstates_cb_error(self): """ Testing whether a download is stopped on error in the download states callback in LaunchManyCore """ error_stop_deferred = Deferred() def mocked_stop(): error_stop_deferred.callback(None) error_tdef = TorrentDef() error_tdef.get_infohash = lambda: 'aaaa' fake_error_download = MockObject() fake_error_download.get_def = lambda: error_tdef fake_error_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_error_download.stop = mocked_stop fake_error_state = MockObject() fake_error_state.get_infohash = lambda: 'aaaa' fake_error_state.get_error = lambda: "test error" fake_error_state.get_status = lambda: DLSTATUS_STOPPED_ON_ERROR fake_error_state.get_download = lambda: fake_error_download self.lm.downloads = {'aaaa': fake_error_download} self.lm.sesscb_states_callback([fake_error_state]) return error_stop_deferred def test_readd_download_safe_seeding(self): """ Test whether a download is re-added when doing safe seeding """ readd_deferred = Deferred() def mocked_update_download_hops(*_): readd_deferred.callback(None) self.lm.update_download_hops = mocked_update_download_hops tdef = TorrentDef() tdef.get_infohash = lambda: 'aaaa' fake_download = MockObject() fake_download.get_def = lambda: tdef fake_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_download.get_hops = lambda: 0 fake_download.get_safe_seeding = lambda: True dl_state = MockObject() dl_state.get_infohash = lambda: 'aaaa' dl_state.get_status = lambda: DLSTATUS_SEEDING dl_state.get_download = lambda: fake_download self.lm.downloads = {'aaaa': fake_download} self.lm.sesscb_states_callback([dl_state]) return readd_deferred def test_load_checkpoint(self): """ Test whether we are resuming downloads after loading checkpoint """ def mocked_resume_download(filename, setupDelay=3): self.assertTrue(filename.endswith('abcd.state')) self.assertEqual(setupDelay, 0) mocked_resume_download.called = True mocked_resume_download.called = False self.lm.session.get_downloads_pstate_dir = lambda: self.session_base_dir with open( os.path.join(self.lm.session.get_downloads_pstate_dir(), 'abcd.state'), 'wb') as state_file: state_file.write("hi") self.lm.initComplete = True self.lm.resume_download = mocked_resume_download self.lm.load_checkpoint() self.assertTrue(mocked_resume_download.called) def test_resume_download(self): with open(os.path.join(TESTS_DATA_DIR, "bak_single.torrent"), mode='rb') as torrent_file: torrent_data = torrent_file.read() def mocked_load_download_pstate(_): raise ValueError() def mocked_add(tdef, dscfg, pstate, **_): self.assertTrue(tdef) self.assertTrue(dscfg) self.assertIsNone(pstate) mocked_add.called = True mocked_add.called = False self.lm.load_download_pstate = mocked_load_download_pstate self.lm.torrent_store = MockObject() self.lm.torrent_store.get = lambda _: torrent_data self.lm.add = mocked_add self.lm.mypref_db = MockObject() self.lm.mypref_db.getMyPrefStatsInfohash = lambda _: TESTS_DATA_DIR self.lm.resume_download('%s.state' % ('a' * 20)) self.assertTrue(mocked_add.called)
class TestLaunchManyCore(TriblerCoreTest): """ This class contains various small unit tests for the LaunchManyCore class. """ DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), 'data') def setUp(self): TriblerCoreTest.setUp(self) self.lm = TriblerLaunchMany() self.lm.session_lock = NoDispersyRLock() self.lm.session = MockObject() self.lm.session.config = MockObject() self.lm.session.config.get_max_upload_rate = lambda: 100 self.lm.session.config.get_max_download_rate = lambda: 100 self.lm.session.config.get_default_number_hops = lambda: 0 # Ignore notifications mock_notifier = MockObject() mock_notifier.notify = lambda *_: None self.lm.session.notifier = mock_notifier @staticmethod def create_fake_download_and_state(): """ Create a fake download and state which can be passed to the global download callback. """ tdef = TorrentDef() tdef.get_infohash = lambda: 'aaaa' fake_peer = {'extended_version': 'Tribler', 'id': 'a' * 20, 'dtotal': 10 * 1024 * 1024} fake_download = MockObject() fake_download.get_def = lambda: tdef fake_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_download.get_hops = lambda: 0 fake_download.get_safe_seeding = lambda: True fake_download.get_peerlist = lambda: [fake_peer] dl_state = MockObject() dl_state.get_infohash = lambda: 'aaaa' dl_state.get_status = lambda: DLSTATUS_SEEDING dl_state.get_download = lambda: fake_download return fake_download, dl_state @raises(ValueError) def test_add_tdef_not_finalized(self): """ Testing whether a ValueError is raised when a non-finalized tdef is added as download. """ self.lm.add(TorrentDef(), None) def test_load_download_pstate(self): """ Testing whether a pstate is successfully loaded """ config_file_path = os.path.abspath(os.path.join(self.DATA_DIR, u"config_files", u"config1.conf")) config = self.lm.load_download_pstate(config_file_path) self.assertIsInstance(config, CallbackConfigParser) self.assertEqual(config.get('general', 'version'), 11) @trial_timeout(10) def test_dlstates_cb_error(self): """ Testing whether a download is stopped on error in the download states callback in LaunchManyCore """ error_stop_deferred = Deferred() def mocked_stop(): error_stop_deferred.callback(None) fake_error_download, fake_error_state = TestLaunchManyCore.create_fake_download_and_state() fake_error_download.stop = mocked_stop fake_error_state.get_status = lambda: DLSTATUS_STOPPED_ON_ERROR fake_error_state.get_error = lambda: "test error" self.lm.downloads = {'aaaa': fake_error_download} self.lm.sesscb_states_callback([fake_error_state]) return error_stop_deferred def test_readd_download_safe_seeding(self): """ Test whether a download is re-added when doing safe seeding """ readd_deferred = Deferred() def mocked_update_download_hops(*_): readd_deferred.callback(None) self.lm.update_download_hops = mocked_update_download_hops fake_download, dl_state = TestLaunchManyCore.create_fake_download_and_state() self.lm.downloads = {'aaaa': fake_download} self.lm.sesscb_states_callback([dl_state]) return readd_deferred def test_update_payout_balance(self): """ Test whether the balance of peers is correctly updated """ fake_download, dl_state = TestLaunchManyCore.create_fake_download_and_state() dl_state.get_status = lambda: DLSTATUS_DOWNLOADING fake_tc = MockObject() fake_tc.add_listener = lambda *_: None self.lm.payout_manager = PayoutManager(fake_tc, None) self.lm.state_cb_count = 4 self.lm.downloads = {'aaaa': fake_download} self.lm.sesscb_states_callback([dl_state]) self.assertTrue(self.lm.payout_manager.tribler_peers) def test_load_checkpoint(self): """ Test whether we are resuming downloads after loading checkpoint """ def mocked_resume_download(filename, setupDelay=3): self.assertTrue(filename.endswith('abcd.state')) self.assertEqual(setupDelay, 0) mocked_resume_download.called = True mocked_resume_download.called = False self.lm.session.get_downloads_pstate_dir = lambda: self.session_base_dir with open(os.path.join(self.lm.session.get_downloads_pstate_dir(), 'abcd.state'), 'wb') as state_file: state_file.write("hi") self.lm.initComplete = True self.lm.resume_download = mocked_resume_download self.lm.load_checkpoint() self.assertTrue(mocked_resume_download.called) def test_resume_download(self): with open(os.path.join(TESTS_DATA_DIR, "bak_single.torrent"), mode='rb') as torrent_file: torrent_data = torrent_file.read() def mocked_load_download_pstate(_): raise ValueError() def mocked_add(tdef, dscfg, pstate, **_): self.assertTrue(tdef) self.assertTrue(dscfg) self.assertIsNone(pstate) mocked_add.called = True mocked_add.called = False self.lm.load_download_pstate = mocked_load_download_pstate self.lm.torrent_store = MockObject() self.lm.torrent_store.get = lambda _: torrent_data self.lm.add = mocked_add self.lm.mypref_db = MockObject() self.lm.mypref_db.getMyPrefStatsInfohash = lambda _: TESTS_DATA_DIR self.lm.resume_download('%s.state' % ('a' * 20)) self.assertTrue(mocked_add.called)
class TestLaunchManyCore(TriblerCoreTest): """ This class contains various small unit tests for the LaunchManyCore class. """ DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), 'data') def setUp(self, annotate=True): TriblerCoreTest.setUp(self, annotate=annotate) self.lm = TriblerLaunchMany() self.lm.sesslock = NoDispersyRLock() self.lm.session = MockObject() # Ignore notifications mock_notifier = MockObject() mock_notifier.notify = lambda *_: None self.lm.session.notifier = mock_notifier @raises(ValueError) def test_add_tdef_not_finalized(self): """ Testing whether a ValueError is raised when a non-finalized tdef is added as download. """ self.lm.add(TorrentDef(), None) @raises(DuplicateDownloadException) def test_add_duplicate_download(self): """ Testing whether a DuplicateDownloadException is raised when a download is added twice """ self.lm.downloads = {"abcd": None} tdef = TorrentDef() tdef.metainfo_valid = True tdef.infohash = "abcd" self.lm.add(tdef, DefaultDownloadStartupConfig.getInstance()) def test_load_download_pstate(self): """ Testing whether a pstate is successfully loaded """ config_file_path = os.path.abspath(os.path.join(self.DATA_DIR, u"config_files", u"config1.conf")) config = self.lm.load_download_pstate(config_file_path) self.assertIsInstance(config, CallbackConfigParser) self.assertEqual(config.get('general', 'version'), 11) def test_sessconfig_changed_cb(self): """ Testing whether the callback works correctly when changing session config parameters """ self.assertFalse(self.lm.sessconfig_changed_callback('blabla', 'fancyname', '3', '4')) self.lm.ltmgr = MockObject() def mocked_set_utp(val): self.assertEqual(val, '42') mocked_set_utp.called = True self.lm.ltmgr.set_utp = mocked_set_utp mocked_set_utp.called = False self.assertTrue(self.lm.sessconfig_changed_callback('libtorrent', 'utp', '42', '3')) self.assertTrue(mocked_set_utp.called) self.assertTrue(self.lm.sessconfig_changed_callback('libtorrent', 'anon_listen_port', '42', '43')) @deferred(timeout=10) def test_dlstates_cb_error(self): """ Testing whether a download is stopped on error in the download states callback in LaunchManyCore """ error_stop_deferred = Deferred() def mocked_stop(): error_stop_deferred.callback(None) error_tdef = TorrentDef() error_tdef.get_infohash = lambda: 'aaaa' fake_error_download = MockObject() fake_error_download.get_def = lambda: error_tdef fake_error_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_error_download.stop = mocked_stop fake_error_state = MockObject() fake_error_state.get_infohash = lambda: 'aaaa' fake_error_state.get_error = lambda: "test error" fake_error_state.get_status = lambda: DLSTATUS_STOPPED_ON_ERROR fake_error_state.get_download = lambda: fake_error_download self.lm.downloads = {'aaaa': fake_error_download} self.lm.sesscb_states_callback([fake_error_state]) return error_stop_deferred @deferred(timeout=10) def test_dlstates_cb_seeding(self): """ Testing whether a download is readded when safe seeding in the download states callback in LaunchManyCore """ readd_deferred = Deferred() def mocked_start_download(tdef, dscfg): self.assertEqual(tdef, seed_tdef) self.assertEqual(dscfg, seed_download) readd_deferred.callback(None) def mocked_remove_download(download): self.assertEqual(download, seed_download) self.lm.session.start_download_from_tdef = mocked_start_download self.lm.session.remove_download = mocked_remove_download seed_tdef = TorrentDef() seed_tdef.get_infohash = lambda: 'aaaa' seed_download = MockObject() seed_download.get_def = lambda: seed_tdef seed_download.get_def().get_name_as_unicode = lambda: "test.iso" seed_download.get_hops = lambda: 0 seed_download.get_safe_seeding = lambda: True seed_download.copy = lambda: seed_download seed_download.set_hops = lambda _: None fake_seed_download_state = MockObject() fake_seed_download_state.get_infohash = lambda: 'aaaa' fake_seed_download_state.get_status = lambda: DLSTATUS_SEEDING fake_seed_download_state.get_download = lambda: seed_download self.lm.sesscb_states_callback([fake_seed_download_state]) return readd_deferred def test_load_checkpoint(self): """ Test whether we are resuming downloads after loading checkpoint """ def mocked_resume_download(filename, setupDelay=3): self.assertTrue(filename.endswith('abcd.state')) self.assertEqual(setupDelay, 0) mocked_resume_download.called = True mocked_resume_download.called = False self.lm.session.get_downloads_pstate_dir = lambda: self.session_base_dir with open(os.path.join(self.lm.session.get_downloads_pstate_dir(), 'abcd.state'), 'wb') as state_file: state_file.write("hi") self.lm.initComplete = True self.lm.resume_download = mocked_resume_download self.lm.load_checkpoint() self.assertTrue(mocked_resume_download.called) def test_resume_download(self): with open(os.path.join(TESTS_DATA_DIR, "bak_single.torrent"), mode='rb') as torrent_file: torrent_data = torrent_file.read() def mocked_load_download_pstate(_): raise ValueError() def mocked_add(tdef, dscfg, pstate, **_): self.assertTrue(tdef) self.assertTrue(dscfg) self.assertIsNone(pstate) mocked_add.called = True mocked_add.called = False self.lm.load_download_pstate = mocked_load_download_pstate self.lm.torrent_store = MockObject() self.lm.torrent_store.get = lambda _: torrent_data self.lm.add = mocked_add self.lm.mypref_db = MockObject() self.lm.mypref_db.getMyPrefStatsInfohash = lambda _: TESTS_DATA_DIR self.lm.resume_download('%s.state' % ('a' * 20)) self.assertTrue(mocked_add.called)