コード例 #1
0
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)