I want to test a Serve program with the FastApi TestClient and PyTest.
a) There is no response from the test program (test_waiter.py) when run with the while-time-loop (in waiter.py).
b) If the while-time-loop is removed, pytest responds with a 404 status for different paths. See the output below.
c) There is a connection to Serve because entering http://localhost:8000/api/spider
in the browser gives:
"Merde! Garcon, Garcon. There is a spider in my soup!"
What am I doing wrong? The code is:
# waiter.py
import time
import subprocess
import ray
from fastapi import FastAPI
from ray import serve
subprocess.run(["ray", "start", "--head", "--address", "127.0.0.1", "--port", "8787", "--disable-usage-stats"])
app = FastAPI()
ray.init(namespace="serve", ignore_reinit_error=True)
serve.start()
@serve.deployment(route_prefix="/api")
@serve.ingress(app)
class Garcon:
@app.get("/soup")
def root(self):
return "Garcon, Soup of the day please."
@app.get("/spider")
def root(self):
return "Merde! Garcon, Garcon. There is a spider in my soup!"
@app.get("/fly")
def root(self):
return "Mademoiselle, Do not worry. The spider catches the flies. Bon appetit."
Garcon.deploy()
while True:
time.sleep(2)
pass
# test_waiter.py
from fastapi.testclient import TestClient
from waiter import app
client = TestClient(app)
def test_path_type():
response = client.get("http://127.0.0.1:8000/api", headers={'content-type': 'application/json'})
assert response.status_code == 200
if __name__ == "__main__":
import sys
import pytest
sys.exit(pytest.main(["-v", "-s", __file__]))
_________________________ test_path_type _________________________
def test_path_type():
response = client.get("http://127.0.0.1:8000/api", headers={'content-type': 'application/json'})
> assert response.status_code == 200
E assert 404 == 200
E + where 404 = <Response [404]>.status_code
test_waiter.py:9: AssertionError
_________________________ test_path_type _________________________
def test_path_type():
response = client.get("http://127.0.0.1:8000/api/fly", headers={'content-type': 'application/json'})
> assert response.status_code == 200
E assert 404 == 200
E + where 404 = <Response [404]>.status_code
test_waiter.py:9: AssertionError
_________________________ test_path_type _________________________
def test_path_type():
response = client.get("http://127.0.0.1:8000/api/spider", headers={'content-type': 'application/json'})
> assert response.status_code == 200
E assert 404 == 200
E + where 404 = <Response [404]>.status_code
test_waiter.py:9: AssertionError