Skip to content

Commit

Permalink
[vision] testing: retry upon errors
Browse files Browse the repository at this point in the history
fixes GoogleCloudPlatform#3734

I only wrapped some of the tests. Potentially we can do it for
everything.
  • Loading branch information
Takashi Matsuo committed May 14, 2020
1 parent ec30b6a commit 5d46a77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
42 changes: 38 additions & 4 deletions vision/cloud-client/detect/detect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import uuid

import backoff
from google.cloud import storage
import pytest

Expand Down Expand Up @@ -142,19 +143,40 @@ def test_detect_properties_uri(capsys):
assert 'frac' in out


def only_sample_error(e):
"""A callback for giving up upon Exceptions.
Giving up upon any Exceptions other than the ones that sample code
throws at the end of the function.
"""
return 'https://cloud.google.com/apis/design/errors' not in str(e)


# Vision 1.1 tests
def test_detect_web(capsys):
file_name = os.path.join(
os.path.dirname(__file__),
'resources/landmark.jpg')
detect.detect_web(file_name)

@backoff.on_exception(
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
def run_sample():
detect.detect_web(file_name)

run_sample()
out, _ = capsys.readouterr()
assert 'best guess label: palace of fine arts' in out.lower()


def test_detect_web_uri(capsys):
file_name = 'gs://{}/vision/landmark/pofa.jpg'.format(ASSET_BUCKET)
detect.detect_web_uri(file_name)

@backoff.on_exception(
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
def run_sample():
detect.detect_web_uri(file_name)

run_sample()
out, _ = capsys.readouterr()
assert 'best guess label: palace of fine arts' in out.lower()

Expand All @@ -163,15 +185,27 @@ def test_detect_web_with_geo(capsys):
file_name = os.path.join(
os.path.dirname(__file__),
'resources/city.jpg')
detect.web_entities_include_geo_results(file_name)

@backoff.on_exception(
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
def run_sample():
detect.web_entities_include_geo_results(file_name)

run_sample()
out, _ = capsys.readouterr()
out = out.lower()
assert 'description' in out


def test_detect_web_with_geo_uri(capsys):
file_name = 'gs://{}/vision/web/city.jpg'.format(ASSET_BUCKET)
detect.web_entities_include_geo_results_uri(file_name)

@backoff.on_exception(
backoff.expo, Exception, max_time=60, giveup=only_sample_error)
def run_sample():
detect.web_entities_include_geo_results_uri(file_name)

run_sample()
out, _ = capsys.readouterr()
out = out.lower()
assert 'description' in out
Expand Down
1 change: 1 addition & 0 deletions vision/cloud-client/detect/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
backoff==1.10.0
pytest==5.3.2
flaky==3.6.1

0 comments on commit 5d46a77

Please sign in to comment.