Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: use threaded http server #35

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dvc_http/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import pytest

from .cloud import HTTP
from .httpd import StaticFileServer
from .httpd import static_file_server


@pytest.fixture(scope="session")
def http_server(tmp_path_factory):
directory = os.fspath(tmp_path_factory.mktemp("http"))
with StaticFileServer(directory=directory) as httpd:
yield httpd
with static_file_server(directory) as server:
yield server


@pytest.fixture
Expand Down
26 changes: 6 additions & 20 deletions dvc_http/tests/httpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os
import threading
from contextlib import contextmanager
from functools import partial
from http import HTTPStatus
from http.server import HTTPServer
from http.server import ThreadingHTTPServer

from RangeHTTPServer import RangeRequestHandler

Expand Down Expand Up @@ -67,22 +68,7 @@ def run_server_on_thread(server):
server.server_close()


class StaticFileServer:
_lock = threading.Lock()

def __init__(self, directory):
from functools import partial

addr = ("localhost", 0)
req = partial(TestRequestHandler, directory=directory)
server = HTTPServer(addr, req)
self.runner = run_server_on_thread(server)

# pylint: disable=no-member
def __enter__(self):
self._lock.acquire()
return self.runner.__enter__()

def __exit__(self, *args):
self.runner.__exit__(*args)
self._lock.release()
def static_file_server(directory):
req = partial(TestRequestHandler, directory=directory)
server = ThreadingHTTPServer(("localhost", 0), req)
return run_server_on_thread(server)