Skip to content

Commit 0a88585

Browse files
authored
Add retries to Run service requests (GoogleCloudPlatform#6087)
## Description Fixes GoogleCloudPlatform#6086 GoogleCloudPlatform#6085 Note: It's a good idea to open an issue first for discussion. ## Checklist - [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] Please **merge** this PR for me once it is approved. - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample
1 parent 10128c5 commit 0a88585

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

run/idp-sql/test/e2e_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from firebase_admin import auth # noqa: F401
2626
import pytest
2727
import requests
28+
from requests.adapters import HTTPAdapter
29+
from requests.packages.urllib3.util.retry import Retry
2830

2931
default_app = firebase_admin.initialize_app()
3032

@@ -179,7 +181,24 @@ def test_end_to_end(jwt_token: str, deployed_service: str) -> None:
179181
token = jwt_token
180182
service_url = deployed_service
181183

184+
retry_strategy = Retry(
185+
total=3,
186+
status_forcelist=[400, 401, 403, 500, 502, 503, 504],
187+
allowed_methods=["GET", "POST"],
188+
backoff_factor=3
189+
)
190+
191+
retry_strategy_500 = Retry(
192+
total=3,
193+
status_forcelist=[500, 502, 503, 504],
194+
allowed_methods=["GET", "POST"],
195+
backoff_factor=3
196+
)
197+
198+
adapter = HTTPAdapter(max_retries=retry_strategy)
199+
182200
client = requests.session()
201+
client.mount("https://", adapter)
183202

184203
# Can successfully make a request
185204
response = client.get(service_url)
@@ -197,6 +216,8 @@ def test_end_to_end(jwt_token: str, deployed_service: str) -> None:
197216
assert response.status_code == 200
198217
assert "🐶" in response.content.decode("UTF-8")
199218

219+
adapter = HTTPAdapter(max_retries=retry_strategy_500)
220+
client.mount("https://", adapter)
200221
# Cannot make post with bad token
201222
response = client.post(
202223
service_url,

run/logging-manual/e2e_test.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import os
2121
import subprocess
2222
import time
23-
from urllib import request
2423
import uuid
2524

2625
from google.cloud import logging_v2
2726

2827
import pytest
2928

29+
import requests
30+
from requests.adapters import HTTPAdapter
31+
from requests.packages.urllib3.util.retry import Retry
32+
3033
# Unique suffix to create distinct service names
3134
SUFFIX = uuid.uuid4().hex[:10]
3235
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
@@ -148,18 +151,26 @@ def test_end_to_end(service_url_auth_token, deployed_service):
148151
service_url, auth_token = service_url_auth_token
149152

150153
# Test that the service is responding
151-
req = request.Request(
154+
retry_strategy = Retry(
155+
total=3,
156+
status_forcelist=[400, 401, 403, 500, 502, 503, 504],
157+
allowed_methods=["GET", "POST"],
158+
backoff_factor=3
159+
)
160+
adapter = HTTPAdapter(max_retries=retry_strategy)
161+
162+
client = requests.session()
163+
client.mount("https://", adapter)
164+
165+
response = client.get(
152166
service_url,
153167
headers={
154168
"Authorization": f"Bearer {auth_token}",
155169
"X-Cloud-Trace-Context": "foo/bar",
156170
},
157171
)
158-
response = request.urlopen(req)
159-
assert response.status == 200
160-
161-
body = response.read()
162-
assert body.decode() == "Hello Logger!"
172+
assert response.status_code == 200
173+
assert "Hello Logger!" in response.content.decode("UTF-8")
163174

164175
# Test that the logs are writing properly to stackdriver
165176
time.sleep(10) # Slight delay writing to stackdriver

0 commit comments

Comments
 (0)