15
15
import os
16
16
import uuid
17
17
18
+ import backoff
18
19
from google .cloud import storage
19
20
import pytest
20
21
@@ -142,19 +143,40 @@ def test_detect_properties_uri(capsys):
142
143
assert 'frac' in out
143
144
144
145
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
+
145
155
# Vision 1.1 tests
146
156
def test_detect_web (capsys ):
147
157
file_name = os .path .join (
148
158
os .path .dirname (__file__ ),
149
159
'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 ()
151
167
out , _ = capsys .readouterr ()
152
168
assert 'best guess label: palace of fine arts' in out .lower ()
153
169
154
170
155
171
def test_detect_web_uri (capsys ):
156
172
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 ()
158
180
out , _ = capsys .readouterr ()
159
181
assert 'best guess label: palace of fine arts' in out .lower ()
160
182
@@ -163,15 +185,27 @@ def test_detect_web_with_geo(capsys):
163
185
file_name = os .path .join (
164
186
os .path .dirname (__file__ ),
165
187
'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 ()
167
195
out , _ = capsys .readouterr ()
168
196
out = out .lower ()
169
197
assert 'description' in out
170
198
171
199
172
200
def test_detect_web_with_geo_uri (capsys ):
173
201
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 ()
175
209
out , _ = capsys .readouterr ()
176
210
out = out .lower ()
177
211
assert 'description' in out
0 commit comments