From 3dd07b44ed8c42e979dbfe39612e05e37dec99dd Mon Sep 17 00:00:00 2001 From: basheer-cloud Date: Sat, 11 Apr 2026 21:29:25 -0400 Subject: [PATCH] perf(demo): set a timeout on flask consumer fork HTTP calls --- demo/flask_consumer_fork.py | 2 +- tests/e2e/base/consumer/flask_consumer.py | 2 +- tests/plugin/base.py | 6 +- .../plugin/http/sw_httpx/services/consumer.py | 78 +++++++++---------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/demo/flask_consumer_fork.py b/demo/flask_consumer_fork.py index 6981f9ac..78947355 100644 --- a/demo/flask_consumer_fork.py +++ b/demo/flask_consumer_fork.py @@ -38,7 +38,7 @@ @app.route('/', methods=['POST', 'GET']) def application(): - res = requests.get('http://localhost:9999') + res = requests.get('http://localhost:9999', timeout=10.0) return res.json() diff --git a/tests/e2e/base/consumer/flask_consumer.py b/tests/e2e/base/consumer/flask_consumer.py index d1fe220f..844ea685 100644 --- a/tests/e2e/base/consumer/flask_consumer.py +++ b/tests/e2e/base/consumer/flask_consumer.py @@ -31,7 +31,7 @@ def artist(): try: time.sleep(random.random()) payload = request.get_json() - requests.post('http://provider:9090/artist-provider', data=payload) + requests.post('http://provider:9090/artist-provider', data=payload, timeout=10.0) return {'artist': 'song'} except Exception as e: # noqa diff --git a/tests/plugin/base.py b/tests/plugin/base.py index 64634e38..7951a695 100644 --- a/tests/plugin/base.py +++ b/tests/plugin/base.py @@ -41,15 +41,15 @@ def validate(self, expected_file_name=None): with open(expected_file_name) as expected_data_file: expected_data = os.linesep.join(expected_data_file.readlines()) - response = requests.post(url='http://localhost:12800/dataValidate', data=expected_data) + response = requests.post(url='http://localhost:12800/dataValidate', data=expected_data, timeout=10.0) if response.status_code != 200: # heuristically retry once time.sleep(10) - response = requests.post(url='http://localhost:12800/dataValidate', data=expected_data) + response = requests.post(url='http://localhost:12800/dataValidate', data=expected_data, timeout=10.0) if response.status_code != 200: - res = requests.get('http://localhost:12800/receiveData') + res = requests.get('http://localhost:12800/receiveData', timeout=10.0) actual_data = yaml.dump(yaml.load(res.content, Loader=Loader)) diff --git a/tests/plugin/http/sw_httpx/services/consumer.py b/tests/plugin/http/sw_httpx/services/consumer.py index b096ca19..482f8d23 100644 --- a/tests/plugin/http/sw_httpx/services/consumer.py +++ b/tests/plugin/http/sw_httpx/services/consumer.py @@ -1,39 +1,39 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import uvicorn -from fastapi import FastAPI -import httpx - -async_client = httpx.AsyncClient() -client = httpx.Client() -app = FastAPI() - - -@app.post('/users') -async def application(): - try: - await async_client.post('http://provider:9091/users') - res = client.post('http://provider:9091/users') - - return res.json() - except Exception: # noqa - return {'message': 'Error'} - - -if __name__ == '__main__': - uvicorn.run(app, host='0.0.0.0', port=9090) +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import uvicorn +from fastapi import FastAPI +import httpx + +async_client = httpx.AsyncClient() +client = httpx.Client() +app = FastAPI() + + +@app.post('/users') +async def application(): + try: + await async_client.post('http://provider:9091/users', timeout=10.0) + res = client.post('http://provider:9091/users', timeout=10.0) + + return res.json() + except Exception: # noqa + return {'message': 'Error'} + + +if __name__ == '__main__': + uvicorn.run(app, host='0.0.0.0', port=9090)