1515import os
1616import uuid
1717
18+ import backoff
1819from google .cloud import storage
1920import 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
146156def 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
155171def 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
172200def 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
0 commit comments