diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..b6038a8 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,31 @@ +# https://hub.docker.com/r/library/python/tags/ +image: python:latest + +# Change pip's cache directory to be inside the project directory since we can +# only cache local items. +variables: + PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" + +# Pip's cache doesn't store the python packages +# https://pip.pypa.io/en/stable/reference/pip_install/#caching +# +# If you want to also cache the installed packages, you have to install +# them in a virtualenv and cache it as well. +cache: + paths: + - .cache/pip + - venv/ + +before_script: + - python -V # Print out python version for debugging + - pip install pylint + - pip install -R requirements.txt + - cd YouTubeMDBot + +test:pylint: + script: + - pylint --ignored-classes=_socketobject *.py + +test: + script: + - python -m unittest $(pwd)/YouTubeMDBot/tests/*.py diff --git a/YouTubeMDBot/tests/__main__.py b/YouTubeMDBot/tests/__main__.py deleted file mode 100644 index 5cb134d..0000000 --- a/YouTubeMDBot/tests/__main__.py +++ /dev/null @@ -1,4 +0,0 @@ -from .downloader import DownloadTest - -if __name__ == '__main__': - DownloadTest.test_multithread_download() diff --git a/YouTubeMDBot/tests/downloader.py b/YouTubeMDBot/tests/downloader.py index 3402283..cb53f74 100644 --- a/YouTubeMDBot/tests/downloader.py +++ b/YouTubeMDBot/tests/downloader.py @@ -1,10 +1,15 @@ import threading import unittest +from time import sleep from YouTubeMDBot.downloader import YouTubeDownloader class DownloadTest(unittest.TestCase): + _elements = 0 + _max = 0 + _lock = threading.Lock() + def test_multithread_download(self): yt1 = YouTubeDownloader(url="https://www.youtube.com/watch?v=Inm-N5rLUSI") yt2 = YouTubeDownloader(url="https://www.youtube.com/watch?v=-_ZwpOdXXcA") @@ -15,17 +20,26 @@ def test_multithread_download(self): t3 = threading.Thread(target=self.write_to_file, args=(yt3, "v3.m4a",)) t4 = threading.Thread(target=self.write_to_file, args=(yt4, "v4.m4a",)) + self._max = 4 + t1.start() t2.start() t3.start() t4.start() - @staticmethod - def write_to_file(yt: YouTubeDownloader, name: str): + while self._elements < self._max: + sleep(1) + + def barrier(self): + with self._lock: + self._elements += 1 + + def write_to_file(self, yt: YouTubeDownloader, name: str): _, data = yt.download() print(name + " downloaded") with open(name, "wb") as f: f.write(data) + self.barrier() if __name__ == '__main__':