diff --git a/tests/cores/mega/test_microservice.py b/tests/cores/mega/test_microservice.py index 0b8b9c7f2..4a3afba5a 100644 --- a/tests/cores/mega/test_microservice.py +++ b/tests/cores/mega/test_microservice.py @@ -7,7 +7,7 @@ from fastapi.testclient import TestClient -from comps import TextDoc, opea_microservices, register_microservice +from comps import TextDoc, opea_microservices, register_microservice, MicroService @register_microservice(name="s1", host="0.0.0.0", port=8080, endpoint="/v1/add") @@ -18,15 +18,21 @@ async def add(request: TextDoc) -> TextDoc: text += "OPEA Project!" return {"text": text} +def sum_test(): + return 1 + 1 class TestMicroService(unittest.TestCase): def setUp(self): self.client = TestClient(opea_microservices["s1"].app) + opea_microservices["s1"].add_route("/v1/sum", sum_test, methods=["GET"]) self.process1 = multiprocessing.Process(target=opea_microservices["s1"].start, daemon=False, name="s1") self.process1.start() + # test conflict port + self.assertRaises(RuntimeError, MicroService, name="s2", host="0.0.0.0", port=8080, endpoint="/v1/add") + def tearDown(self): opea_microservices["s1"].stop() self.process1.terminate() @@ -38,6 +44,9 @@ def test_add_route(self): response = self.client.get("/metrics") self.assertEqual(response.status_code, 200) + response = self.client.get("/v1/sum") + self.assertEqual(response.json(), 2) + if __name__ == "__main__": unittest.main()