-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathtest_timeouts.py
265 lines (193 loc) · 7.76 KB
/
test_timeouts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright 2024 Google LLC
#
# Licensed 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 pathlib
import socket
import time
from multiprocessing import Process
import pytest
import requests
ff_gunicorn = pytest.importorskip("functions_framework._http.gunicorn")
from functions_framework import create_app
TEST_FUNCTIONS_DIR = pathlib.Path(__file__).resolve().parent / "test_functions"
TEST_HOST = "0.0.0.0"
TEST_PORT = "8080"
@pytest.fixture(autouse=True)
def run_around_tests():
# the test samples test also listens on 8080, so let's be good stewards of
# the port and make sure it's free
_wait_for_no_listen(TEST_HOST, TEST_PORT)
yield
_wait_for_no_listen(TEST_HOST, TEST_PORT)
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_no_timeout_allows_request_processing_to_finish():
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
assert result.status_code == 200
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_timeout_but_not_threaded_timeout_enabled_does_not_kill(monkeypatch):
monkeypatch.setenv("CLOUD_RUN_TIMEOUT_SECONDS", "1")
monkeypatch.setenv("THREADED_TIMEOUT_ENABLED", "false")
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
assert result.status_code == 200
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_timeout_and_threaded_timeout_enabled_kills(monkeypatch):
monkeypatch.setenv("CLOUD_RUN_TIMEOUT_SECONDS", "1")
monkeypatch.setenv("THREADED_TIMEOUT_ENABLED", "true")
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
# Any exception raised in execution is a 500 error. Cloud Functions 1st gen and
# 2nd gen/Cloud Run infrastructure doing the timeout will return a 408 (gen 1)
# or 504 (gen 2/CR) at the infrastructure layer when request timeouts happen,
# and this code will only be available to the user in logs.
assert result.status_code == 500
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_timeout_and_threaded_timeout_enabled_but_timeout_not_exceeded_doesnt_kill(
monkeypatch,
):
monkeypatch.setenv("CLOUD_RUN_TIMEOUT_SECONDS", "2")
monkeypatch.setenv("THREADED_TIMEOUT_ENABLED", "true")
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
assert result.status_code == 200
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_timeout_sync_worker_kills_on_timeout(
monkeypatch,
):
monkeypatch.setenv("CLOUD_RUN_TIMEOUT_SECONDS", "1")
monkeypatch.setenv("WORKERS", 2)
monkeypatch.setenv("THREADS", 1)
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
assert result.status_code == 500
@pytest.mark.skipif("platform.system() == 'Windows'")
@pytest.mark.skipif("platform.system() == 'Darwin'")
@pytest.mark.slow_integration_test
def test_timeout_sync_worker_does_not_kill_if_less_than_timeout(
monkeypatch,
):
monkeypatch.setenv("CLOUD_RUN_TIMEOUT_SECONDS", "2")
monkeypatch.setenv("WORKERS", 2)
monkeypatch.setenv("THREADS", 1)
source = TEST_FUNCTIONS_DIR / "timeout" / "main.py"
target = "function"
app = create_app(target, source)
options = {}
gunicorn_app = ff_gunicorn.GunicornApplication(
app, TEST_HOST, TEST_PORT, False, **options
)
gunicorn_p = Process(target=gunicorn_app.run)
gunicorn_p.start()
_wait_for_listen(TEST_HOST, TEST_PORT)
result = requests.get("http://{}:{}/".format(TEST_HOST, TEST_PORT))
gunicorn_p.terminate()
gunicorn_p.join()
assert result.status_code == 200
@pytest.mark.skip
def _wait_for_listen(host, port, timeout=10):
# Used in tests to make sure that the gunicorn app has booted and is
# listening before sending a test request
start_time = time.perf_counter()
while True:
try:
with socket.create_connection((host, port), timeout=timeout):
break
except OSError as ex:
time.sleep(0.01)
if time.perf_counter() - start_time >= timeout:
raise TimeoutError(
"Waited too long for port {} on host {} to start accepting "
"connections.".format(port, host)
) from ex
@pytest.mark.skip
def _wait_for_no_listen(host, port, timeout=10):
# Used in tests to make sure that the port is actually free after
# the process binding to it should have been killed
start_time = time.perf_counter()
while True:
try:
with socket.create_connection((host, port), timeout=timeout):
time.sleep(0.01)
if time.perf_counter() - start_time >= timeout:
raise TimeoutError(
"Waited too long for port {} on host {} to stop accepting "
"connections.".format(port, host)
)
except OSError as ex:
break