samedi 30 mai 2015

How do I know that the Flask application is ready to process the requests without actually polling it?

During a smoke test, I want to ensure that a Flask application is handling correctly a few basic requests. This involves starting the 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 the tests which perform the requests with requests library.

If the code is left as is, the tests are often run before the server is actually started, which results in ConnectionRefusedError. To prevent this from happening, I appended the following code to setUpClass:

while True:
    try:
        requests.get("http://localhost:49201/", timeout=0.5)
        return
    except requests.exceptions.ConnectionError:
        pass

While this works, it looks ugly. Given that the test case is in control of the Flask application, there should be a way for it to be notified once the application is ready to process the requests. Unfortunately, the closest thing I've found is got_first_request, which doesn't help (unless once again I'm polling the server).

How is it possible to determine that the Flask application started and is ready to process the requests when running it asynchronously?

Aucun commentaire:

Enregistrer un commentaire