From 98ecfa40efcee55544314329b0f6e0e6fb3b14da Mon Sep 17 00:00:00 2001 From: Stefan Scherzinger Date: Tue, 9 Jul 2024 09:52:18 +0200 Subject: [PATCH] Add boilerplate code for a web server This will mimic the real gripper's web server. --- schunk_egu_egk_dummy/README.md | 28 +++++++++++++++++++ schunk_egu_egk_dummy/main.py | 37 +++++++++++++++++++++++++ schunk_egu_egk_dummy/src/__init__.py | 0 schunk_egu_egk_dummy/src/dummy.py | 29 +++++++++++++++++++ schunk_egu_egk_dummy/test/__init__.py | 0 schunk_egu_egk_dummy/test/test_dummy.py | 27 ++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 schunk_egu_egk_dummy/README.md create mode 100644 schunk_egu_egk_dummy/main.py create mode 100644 schunk_egu_egk_dummy/src/__init__.py create mode 100644 schunk_egu_egk_dummy/src/dummy.py create mode 100644 schunk_egu_egk_dummy/test/__init__.py create mode 100644 schunk_egu_egk_dummy/test/test_dummy.py diff --git a/schunk_egu_egk_dummy/README.md b/schunk_egu_egk_dummy/README.md new file mode 100644 index 0000000..5eb6018 --- /dev/null +++ b/schunk_egu_egk_dummy/README.md @@ -0,0 +1,28 @@ +# Schunk EGU/EGK Dummy +A minimalist protocol simulator for system tests. + +## Install +Use a virtual environment and +```bash +pip install fastapi uvicorn +``` + +## Getting started +1. In a new terminal, start the dummy + ```bash + uvicorn main:server --port 8000 --reload + ``` + +## Run tests locally + +Inside a virtual environment (.venv) + +```bash +pip install pytest coverage +``` + +And then inside this repository +```bash +coverage run -m pytest test/ +coverage report +``` diff --git a/schunk_egu_egk_dummy/main.py b/schunk_egu_egk_dummy/main.py new file mode 100644 index 0000000..f9e6839 --- /dev/null +++ b/schunk_egu_egk_dummy/main.py @@ -0,0 +1,37 @@ +from src.dummy import Dummy + +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from typing import Optional +from pydantic import BaseModel + +# Components +dummy = Dummy() +dummy.start() +server = FastAPI() +client = ["http://localhost:8001"] + + +server.add_middleware( + CORSMiddleware, + allow_origins=client, + allow_credentials=True, + allow_methods=["*"], + allow_headers=[""], +) + + +class Message(BaseModel): + message: str + optional: Optional[str] = None + + +@server.put("/") +async def put(msg: Message): + dummy.process(msg.message) + return True + + +@server.get("/") +async def get(): + return "Dummy ready" diff --git a/schunk_egu_egk_dummy/src/__init__.py b/schunk_egu_egk_dummy/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/schunk_egu_egk_dummy/src/dummy.py b/schunk_egu_egk_dummy/src/dummy.py new file mode 100644 index 0000000..5e9d2b6 --- /dev/null +++ b/schunk_egu_egk_dummy/src/dummy.py @@ -0,0 +1,29 @@ +from threading import Thread +import time + + +class Dummy(object): + def __init__(self): + self.thread = Thread(target=self._run) + self.running = False + self.done = False + + def start(self) -> None: + if self.running: + return + self.thread.start() + self.running = True + + def stop(self) -> None: + self.done = True + self.thread.join() + self.running = False + + def _run(self): + while not self.done: + time.sleep(1) + print("Done") + + def process(self, msg: str) -> bool: + print(msg) + return True diff --git a/schunk_egu_egk_dummy/test/__init__.py b/schunk_egu_egk_dummy/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/schunk_egu_egk_dummy/test/test_dummy.py b/schunk_egu_egk_dummy/test/test_dummy.py new file mode 100644 index 0000000..8cb76f3 --- /dev/null +++ b/schunk_egu_egk_dummy/test/test_dummy.py @@ -0,0 +1,27 @@ +from src.dummy import Dummy + + +def test_dummy_starts_a_background_thread(): + dummy = Dummy() + assert not dummy.running + dummy.start() + assert dummy.running + dummy.stop() + assert not dummy.running + + +def test_dummy_survives_repeated_starts_and_stops(): + dummy = Dummy() + for _ in range(3): + dummy.start() + assert dummy.running + + for _ in range(3): + dummy.stop() + assert not dummy.running + + +def test_dummy_processes_messages(): + dummy = Dummy() + msg = "Hello simulator!" + assert dummy.process(msg)