Add base test case for worker

This commit is contained in:
Jake Howard 2018-12-12 21:55:45 +00:00
parent 81789ec29e
commit 3ed8ba7eb3
Signed by: jake
GPG key ID: 57AFB45680EDD477
6 changed files with 31 additions and 8 deletions

View file

@ -5,7 +5,6 @@ import psutil
from catfish.utils.processes import terminate_processes from catfish.utils.processes import terminate_processes
import asyncio import asyncio
from .router import run_server from .router import run_server
from functools import partial
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid") PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
@ -36,9 +35,10 @@ def stop_worker():
terminate_processes([get_running_process()]) terminate_processes([get_running_process()])
async def run_worker(): async def run_worker(port):
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
await asyncio.gather(run_server(loop)) await asyncio.gather(run_server(loop, port))
run = partial(asyncio.run, run_worker()) def run(port=8080):
return asyncio.run(run_worker(port))

View file

@ -5,10 +5,12 @@ from aiohttp import web
async def handle_request(request): async def handle_request(request):
return web.json_response({}) return web.json_response({})
def get_server():
return web.Server(handle_request)
async def run_server(loop, port=8080): async def run_server(loop, port):
click.echo("Starting server...") click.echo("Starting server...")
aiohttp_server = web.Server(handle_request) aiohttp_server = get_server()
aio_server = await loop.create_server(aiohttp_server, "0.0.0.0", port) aio_server = await loop.create_server(aiohttp_server, "0.0.0.0", port)
click.echo("Server listening on port {}".format(port)) click.echo("Server listening on port {}".format(port))
await aio_server.serve_forever() await aio_server.serve_forever()

View file

@ -1,3 +1,4 @@
aiounittest==1.1.0
black==18.9b0 black==18.9b0
flake8==3.6.0 flake8==3.6.0
flake8-comprehensions==1.4.1 flake8-comprehensions==1.4.1

View file

@ -1,11 +1,13 @@
from unittest import TestCase from aiounittest import AsyncTestCase
from aiohttp.test_utils import unused_port
from click.testing import CliRunner from click.testing import CliRunner
from catfish.__main__ import cli from catfish.__main__ import cli
from catfish import worker from catfish import worker
import functools import functools
from multiprocessing import Process
class BaseTestCase(TestCase): class BaseTestCase(AsyncTestCase):
def setUp(self): def setUp(self):
worker.stop_worker() worker.stop_worker()
self.cli_runner = CliRunner() self.cli_runner = CliRunner()
@ -14,3 +16,15 @@ class BaseTestCase(TestCase):
def tearDown(self): def tearDown(self):
worker.stop_worker() worker.stop_worker()
class BaseWorkerTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.unused_port = unused_port()
self.router_process = Process(target=worker.run, args=(self.unused_port,), daemon=True)
self.router_process.start()
def tearDown(self):
self.router_process.terminate()
super().tearDown()

View file

View file

@ -0,0 +1,6 @@
from tests import BaseWorkerTestCase
class RouterTestCase(BaseWorkerTestCase):
def test_thing(self):
self.assertTrue(True)