Add and test ping command
This commit is contained in:
parent
31b1bc957c
commit
81bbcf8761
3 changed files with 16 additions and 7 deletions
|
@ -8,7 +8,7 @@ import psutil
|
||||||
from catfish.router import start_router
|
from catfish.router import start_router
|
||||||
from catfish.utils.processes import terminate_processes
|
from catfish.utils.processes import terminate_processes
|
||||||
|
|
||||||
from .server import start_server, WORKER_SERVER_SOCKET
|
from .server import WORKER_SERVER_SOCKET, start_server
|
||||||
|
|
||||||
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
|
PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid")
|
||||||
|
|
||||||
|
@ -34,9 +34,7 @@ def wait_for_worker():
|
||||||
|
|
||||||
|
|
||||||
def wait_for_running_worker():
|
def wait_for_running_worker():
|
||||||
while not all([
|
while not all([os.path.exists(WORKER_SERVER_SOCKET)]):
|
||||||
os.path.exists(WORKER_SERVER_SOCKET)
|
|
||||||
]):
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
import ujson
|
import ujson
|
||||||
|
@ -40,6 +41,8 @@ async def client_connected(reader, writer):
|
||||||
env={**os.environ, "PYTHONUNBUFFERED": "1"}
|
env={**os.environ, "PYTHONUNBUFFERED": "1"}
|
||||||
)
|
)
|
||||||
write_data(writer, {"pid": proc.pid})
|
write_data(writer, {"pid": proc.pid})
|
||||||
|
elif data["type"] == "ping":
|
||||||
|
write_data(writer, {"ping": "pong"})
|
||||||
else:
|
else:
|
||||||
write_data(writer, {"error": "Invalid command"})
|
write_data(writer, {"error": "Invalid command"})
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
from tests import BaseWorkerTestCase
|
|
||||||
from catfish.worker.server import send_to_server
|
|
||||||
from catfish.utils.processes import is_process_running
|
from catfish.utils.processes import is_process_running
|
||||||
|
from catfish.worker.server import send_to_server
|
||||||
|
from tests import BaseWorkerTestCase
|
||||||
|
|
||||||
|
|
||||||
class WorkerServerTestCase(BaseWorkerTestCase):
|
class WorkerServerTestCase(BaseWorkerTestCase):
|
||||||
def test_server_creates_process(self):
|
def test_server_creates_process(self):
|
||||||
response = send_to_server({"type": "process", "command": "yes"})
|
response = send_to_server({"type": "process", "command": "yes"})
|
||||||
self.assertTrue(is_process_running(response['pid']))
|
self.assertTrue(is_process_running(response["pid"]))
|
||||||
|
|
||||||
|
def test_unknown_command_type(self):
|
||||||
|
response = send_to_server({"type": "nothing"})
|
||||||
|
self.assertEqual(response, {"error": "Invalid command"})
|
||||||
|
|
||||||
|
def test_ping(self):
|
||||||
|
response = send_to_server({"type": "ping"})
|
||||||
|
self.assertEqual(response, {"ping": "pong"})
|
||||||
|
|
Reference in a new issue