Skip to content

Commit 5d46a77

Browse files
author
Takashi Matsuo
committed
[vision] testing: retry upon errors
fixes GoogleCloudPlatform#3734 I only wrapped some of the tests. Potentially we can do it for everything.
1 parent ec30b6a commit 5d46a77

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

vision/cloud-client/detect/detect_test.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import uuid
1717

18+
import backoff
1819
from google.cloud import storage
1920
import pytest
2021

@@ -142,19 +143,40 @@ def test_detect_properties_uri(capsys):
142143
assert 'frac' in out
143144

144145

146+
def only_sample_error(e):
147+
"""A callback for giving up upon Exceptions.
148+
149+
Giving up upon any Exceptions other than the ones that sample code
150+
throws at the end of the function.
151+
"""
152+
return 'https://cloud.google.com/apis/design/errors' not in str(e)
153+
154+
145155
# Vision 1.1 tests
146156
def test_detect_web(capsys):
147157
file_name = os.path.join(
148158
os.path.dirname(__file__),
149159
'resources/landmark.jpg')
150-
detect.detect_web(file_name)
160+
161+
@backoff.on_exception(
162+
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
163+
def run_sample():
164+
detect.detect_web(file_name)
165+
166+
run_sample()
151167
out, _ = capsys.readouterr()
152168
assert 'best guess label: palace of fine arts' in out.lower()
153169

154170

155171
def test_detect_web_uri(capsys):
156172
file_name = 'gs://{}/vision/landmark/pofa.jpg'.format(ASSET_BUCKET)
157-
detect.detect_web_uri(file_name)
173+
174+
@backoff.on_exception(
175+
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
176+
def run_sample():
177+
detect.detect_web_uri(file_name)
178+
179+
run_sample()
158180
out, _ = capsys.readouterr()
159181
assert 'best guess label: palace of fine arts' in out.lower()
160182

@@ -163,15 +185,27 @@ def test_detect_web_with_geo(capsys):
163185
file_name = os.path.join(
164186
os.path.dirname(__file__),
165187
'resources/city.jpg')
166-
detect.web_entities_include_geo_results(file_name)
188+
189+
@backoff.on_exception(
190+
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
191+
def run_sample():
192+
detect.web_entities_include_geo_results(file_name)
193+
194+
run_sample()
167195
out, _ = capsys.readouterr()
168196
out = out.lower()
169197
assert 'description' in out
170198

171199

172200
def test_detect_web_with_geo_uri(capsys):
173201
file_name = 'gs://{}/vision/web/city.jpg'.format(ASSET_BUCKET)
174-
detect.web_entities_include_geo_results_uri(file_name)
202+
203+
@backoff.on_exception(
204+
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
205+
def run_sample():
206+
detect.web_entities_include_geo_results_uri(file_name)
207+
208+
run_sample()
175209
out, _ = capsys.readouterr()
176210
out = out.lower()
177211
assert 'description' in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
backoff==1.10.0
12
pytest==5.3.2
23
flaky==3.6.1

0 commit comments

Comments
 (0)