How do I know if my Flask application is ready to handle requests without actually polling?
During my smoke test, I want to make sure that the Flask app handles a few basic requests correctly. This involves starting your Flask application asynchronously:
class TestSmoke(unittest.TestCase):
@staticmethod
def run_server():
app.run(port=49201)
@classmethod
def setUpClass(cls):
cls.flaskProcess = multiprocessing.Process(target=TestSmoke.run_server)
cls.flaskProcess.start()
and then run those tests that run queries against the library requests
.
If the code is left as is, tests are often executed before the server is started, resulting in ConnectionRefusedError
. To prevent this from happening, I added the following code to setUpClass
:
while True:
try:
requests.get("http://localhost:49201/", timeout=0.5)
return
except requests.exceptions.ConnectionError:
pass
As long as it works, it looks ugly. Given that the test case is in control of the Flask application, there must be a way to notify it after the application is ready to handle requests. Unfortunately the closest thing I've found got_first_request
that doesn't help (unless I've polled the server already).
How can you tell if a Flask application is running and ready to handle requests when it starts up asynchronously?
source to share
No one has answered this question yet
Check out similar questions: