Example #1
0
    def test_callback_overwrite(self):
        def _progress_cb(progress):
            return False

        def _url_resolve_cb(item):
            return item

        dl = pyloader.Loader(daemon=True)

        dummy1 = pyloader.DLable(resources['5MB'], target, 'dummy1.zip')
        dl.queue(dummy1)

        dl.start()

        with self.assertRaises(RuntimeError):
            dl.progress_cb = _progress_cb

        with self.assertRaises(RuntimeError):
            dl.url_resolve_cb = _url_resolve_cb

        while dl.is_active():
            time.sleep(0.25)

        dl.progress_cb = _progress_cb
        dl.url_resolve_cb = _url_resolve_cb

        dl.exit()
Example #2
0
    def test_callback_cancel(self):
        def _callback(progress):
            self.assertIsNotNone(progress.dlable)
            self.assertIsNone(progress.error)

            if progress.status == pyloader.Status.IN_PROGRESS:
                self.assertGreater(progress.mb_total, 0)
                self.assertGreater(progress.mb_current, 0)
                self.assertGreater(progress.mb_left, 0)
                self.assertGreater(progress.percent, 0)
                self.assertGreater(progress.time_spent, 0)
                self.assertGreater(progress.time_left, 0)

                self.assertTrue(progress.http_status >= 200
                                and progress.http_status <= 299)

                return True

            else:
                return False

        dl = pyloader.Loader(progress_cb=_callback,
                             update_interval=1,
                             daemon=True)

        dl.start()

        dl.download(dummy)

        # Wait for all downloads to end
        while dl.is_active:
            time.sleep(0.25)

        dl.exit()
Example #3
0
    def test_stop(self):
        dl = pyloader.Loader(daemon=True)

        dummy1 = pyloader.DLable(resources['1GB'], target, 'dummy1.zip')
        dummy2 = pyloader.DLable(resources['1GB'], target, 'dummy2.zip')

        dl.queue(dummy1)
        dl.queue(dummy2)

        self.assertTrue(dl.is_active)
        self.assertIs(dl.active, 0)

        dl.start()

        # Make sure the download started
        time.sleep(1.0)
        self.assertIs(dl.active, 2)

        dl.stop(dummy1.uid)
        # Make sure the stop triggered
        time.sleep(1.0)
        self.assertIs(dl.active, 1)

        dl.stop(dummy2.uid)
        # Make sure the stop triggered
        time.sleep(1.0)
        self.assertIs(dl.active, 0)

        self.assertFalse(dl.is_active)

        dl.exit()
Example #4
0
    def test_url_headers_list(self):
        dl = pyloader.Loader(daemon=True)

        dummy = pyloader.DLable(
            [resources['5MB'], resources['5MB'], resources['5MB']],
            target,
            'dummy.zip',
            headers=[
                {
                    'Range': 'bytes=0-1048576'
                },
                {
                    'Range': 'bytes=0-2097152'
                },
                {
                    'Range': 'bytes=0-3145728'
                },
            ],
            content_length=6291456,
        )

        dl.download(dummy)
        dl.start()

        while dl.is_active():
            time.sleep(0.25)

        target_file = os.path.join(target, 'dummy.zip')
        with open(target_file, 'rb') as f:
            self.assertEqual(
                hashlib.md5(f.read()).hexdigest(),
                '037aabe7a96bc84c16c337da159b171b')

        dl.exit()
Example #5
0
    def test_callback_cancel(self):
        mock = Mock(return_value=True)
        dummy1 = pyloader.DLable(resources['5MB'], target, 'dummy1.zip')
        dl = pyloader.Loader(progress_cb=mock, update_interval=1, daemon=True)
        dl.start()
        dl.download(dummy1)

        # Wait for all downloads to end
        while dl.is_active():
            time.sleep(0.25)

        dl.exit()

        mock.assert_called_once()
Example #6
0
    def test_download_success(self):
        mock = Mock(return_value=False)
        dummy1 = pyloader.DLable(resources['5MB'], target, 'dummy1.zip')
        dl = pyloader.Loader(progress_cb=mock, update_interval=1, daemon=True)
        dl.start()
        dl.download(dummy1)

        # Wait for all downloads to end
        while dl.is_active():
            time.sleep(0.25)

        dl.exit()

        progress = mock.mock_calls[-1][1][0]
        self.assertEqual(progress.status, pyloader.Status.FINISHED)
Example #7
0
    def test_clear_queued(self):
        dl = pyloader.Loader(daemon=True)

        dummy1 = pyloader.DLable(resources['1GB'], target, 'dummy1.zip')
        dummy2 = pyloader.DLable(resources['1GB'], target, 'dummy2.zip')

        dl.queue(dummy1)
        dl.queue(dummy2)

        self.assertEqual(2, dl.queued)

        dl.clear_queued()

        self.assertEqual(0, dl.queued)

        dl.exit()
Example #8
0
    def test_callback_exception(self):
        def _callback(progress):
            raise Exception('Expected exception')

        dl = pyloader.Loader(progress_cb=_callback,
                             update_interval=1,
                             daemon=True)

        dl.start()

        dl.download(dummy)

        # Wait for all downloads to end
        while dl.is_active():
            time.sleep(0.25)

        dl.exit()
Example #9
0
    def test_url_resolve_cb(self):
        _url_resolved = threading.Event()

        def _url_resolver(url):
            _url_resolved.set()

            return resources['1GB']

        dl = pyloader.Loader(update_interval=1,
                             daemon=True,
                             url_resolve_cb=_url_resolver)
        dl.start()

        # Use normal url and not call the resolve callback
        dummy = pyloader.DLable(resources['1GB'], target, resolve_url=False)
        dl.download(dummy)

        time.sleep(1.0)
        dl.stop(dummy.uid)

        self.assertTrue(not _url_resolved.is_set())
        _url_resolved.clear()

        # Use "something-else" as url and let the callback resolve it
        dummy = pyloader.DLable('prot://some.url?id=123',
                                target,
                                resolve_url=True)
        dl.download(dummy)

        time.sleep(1.0)
        dl.stop(dummy.uid)

        self.assertTrue(_url_resolved.is_set())
        _url_resolved.clear()

        # Wait for all downloads to end
        while dl.is_active:
            time.sleep(0.25)

        dl.exit()
Example #10
0
    def test_properties(self):
        dl = pyloader.Loader(daemon=True)

        self.assertFalse(dl.is_active)
        self.assertFalse(dl.is_alive)

        self.assertIs(dl.queued, 0)
        self.assertIs(dl.active, 0)

        dl.queue(dummy)

        self.assertIs(dl.queued, 1)
        self.assertIs(dl.active, 0)

        self.assertTrue(dl.is_active)
        self.assertFalse(dl.is_alive)

        dl.start()

        # Give the loader some time to start the download
        time.sleep(1.0)

        self.assertIs(dl.queued, 0)
        self.assertIs(dl.active, 1)

        self.assertTrue(dl.is_active)
        self.assertTrue(dl.is_alive)

        dl.exit()

        # Give the loader some time to exit
        time.sleep(1.0)

        self.assertIs(dl.queued, 0)
        self.assertIs(dl.active, 0)

        self.assertFalse(dl.is_active)
        self.assertFalse(dl.is_alive)
Example #11
0
    def test_url_list(self):
        dl = pyloader.Loader(daemon=True)

        dummy = pyloader.DLable(
            [resources['5MB'], resources['5MB'], resources['5MB']],
            target,
            'dummy.zip',
            content_length=15728640,
        )

        dl.download(dummy)
        dl.start()

        while dl.is_active():
            time.sleep(0.25)

        target_file = os.path.join(target, 'dummy.zip')
        with open(target_file, 'rb') as f:
            self.assertEqual(
                hashlib.md5(f.read()).hexdigest(),
                'd7197443eb84599f02a36830a33f917f')

        dl.exit()