Test worker stop/start works

This commit is contained in:
Jake Howard 2018-12-12 18:47:40 +00:00
parent 3212a3326d
commit 96c52a0328
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 73 additions and 9 deletions

View file

@ -1,8 +1,10 @@
import os
import sys
import click import click
import daemonize import daemonize
from catfish import __version__, worker from catfish import __version__, worker
from catfish.utils.processes import terminate_processes
@click.group() @click.group()
@ -13,26 +15,31 @@ def cli():
@cli.command() @cli.command()
@click.option("--no-fork", is_flag=True) @click.option("--no-fork", is_flag=True)
def start(no_fork): @click.pass_context
def start(ctx, no_fork):
if worker.is_running():
ctx.fail("Worker already running")
daemon = daemonize.Daemonize( daemon = daemonize.Daemonize(
"catfish", worker.PID_FILE, worker.main, foreground=no_fork "catfish", worker.PID_FILE, worker.main, foreground=no_fork
) )
try: try:
# HACK: Temporary hack until https://github.com/thesharp/daemonize/pull/70 is solved
os._exit = sys.exit
daemon.start() daemon.start()
except SystemExit: except SystemExit:
return worker.wait_for_worker()
proc = worker.get_running_process() proc = worker.get_running_process()
click.echo("Worker started with pid {}".format(proc.pid)) click.echo("Worker started with pid {}".format(proc.pid))
@cli.command() @cli.command()
def stop(): @click.pass_context
def stop(ctx):
if not worker.is_running(): if not worker.is_running():
click.echo("Worker not running") ctx.fail("Worker not running")
return
proc = worker.get_running_process() proc = worker.get_running_process()
click.echo("Terminating process {}".format(proc.pid)) click.echo("Terminating process {}".format(proc.pid))
terminate_processes([proc]) worker.stop_worker()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -2,6 +2,7 @@ import time
import os import os
import tempfile import tempfile
import psutil import psutil
from catfish.utils.processes import terminate_processes
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid") PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
@ -17,6 +18,21 @@ def get_running_process() -> psutil.Process:
return psutil.Process(int(f.read())) return psutil.Process(int(f.read()))
def wait_for_worker():
while True:
try:
get_running_process()
return
except ValueError:
pass
time.sleep(0.1)
def stop_worker():
if is_running():
terminate_processes([get_running_process()])
def main(): def main():
while True: while True:
time.sleep(1) time.sleep(1)

View file

@ -1,9 +1,16 @@
from unittest import TestCase from unittest import TestCase
from click.testing import CliRunner from click.testing import CliRunner
from catfish.__main__ import cli from catfish.__main__ import cli
from catfish import worker
import functools
class BaseTestCase(TestCase): class BaseTestCase(TestCase):
def setUp(self): def setUp(self):
worker.stop_worker()
self.cli_runner = CliRunner() self.cli_runner = CliRunner()
self.cli = cli self.cli = cli
self.run_cli = functools.partial(self.cli_runner.invoke, self.cli)
def tearDown(self):
worker.stop_worker()

View file

@ -1,10 +1,44 @@
from catfish import __version__ from catfish import __version__, worker
from tests import BaseTestCase from tests import BaseTestCase
class MainCLITestCase(BaseTestCase): class MainCLITestCase(BaseTestCase):
def test_version(self): def test_version(self):
result = self.cli_runner.invoke(self.cli, ["--version"]) result = self.run_cli(["--version"])
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0)
self.assertIn(__version__, result.output) self.assertIn(__version__, result.output)
self.assertIn("catfish", result.output) self.assertIn("catfish", result.output)
class WorkerControlTestCase(BaseTestCase):
def test_starts_worker(self):
result = self.run_cli(["start"])
self.assertEqual(result.exit_code, 0)
self.assertTrue(worker.is_running())
worker_process = worker.get_running_process()
self.assertIn(str(worker_process.pid), result.output)
def test_stops_worker(self):
result = self.run_cli(["start"])
self.assertEqual(result.exit_code, 0)
self.assertTrue(worker.is_running())
worker_pid = worker.get_running_process().pid
result = self.run_cli(["stop"])
self.assertEqual(result.exit_code, 0)
self.assertIn(str(worker_pid), result.output)
self.assertFalse(worker.is_running())
def test_starting_when_already_running(self):
result = self.run_cli(["start"])
self.assertEqual(result.exit_code, 0)
self.assertTrue(worker.is_running())
result = self.run_cli(["start"])
self.assertEqual(result.exit_code, 2)
self.assertIn("Worker already running", result.output)
self.assertTrue(worker.is_running())
def test_stop_when_not_running(self):
self.assertFalse(worker.is_running())
result = self.run_cli(["stop"])
self.assertEqual(result.exit_code, 2)
self.assertIn("Worker not running", result.output)