From a64b75512c9ffcf83f00950d5e5691bf55936b23 Mon Sep 17 00:00:00 2001 From: Nick Cain Date: Wed, 6 Oct 2021 21:41:13 +0000 Subject: [PATCH 1/2] feat: use backoff module instead of flaky --- .github/CODEOWNERS | 2 +- ...batch_translate_text_with_glossary_test.py | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index cb787cdf..b3b89075 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -11,4 +11,4 @@ /samples/**/*.py @googleapis/cdpe-cloudai # The python-samples-owners team is the default owner for samples -/samples/**/*.py @telpirion @sirtorry @googleapis/python-samples-owners \ No newline at end of file +/samples/**/*.py @aribray @sirtorry @googleapis/python-samples-owners \ No newline at end of file diff --git a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py index d2b1f211..658ce571 100644 --- a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -15,6 +15,7 @@ import os import uuid +import backoff from google.cloud import storage import pytest @@ -25,8 +26,7 @@ GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" -@pytest.fixture(scope="function") -def bucket(): +def get_ephemeral_bucket(): """Create a temporary bucket to store annotation output.""" bucket_name = f"tmp-{uuid.uuid4().hex}" storage_client = storage.Client() @@ -37,15 +37,26 @@ def bucket(): bucket.delete(force=True) -@pytest.mark.flaky(max_runs=3, min_passes=1) +@pytest.fixture(scope="function") +def bucket(): + """Create a bucket feature for testing""" + return next(get_ephemeral_bucket()) + + +def on_backoff(invocation_dict): + """Backoff callback; create a testing bucket for each backoff run""" + invocation_dict['kwargs']['bucket'] = next(get_ephemeral_bucket()) + + +@backoff.on_exception(wait_gen=lambda : iter([100, 250, 300, 500]), exception=Exception, max_tries=5, on_backoff=on_backoff) def test_batch_translate_text_with_glossary(capsys, bucket): + translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( "gs://cloud-samples-data/translation/text_with_glossary.txt", "gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name), PROJECT_ID, GLOSSARY_ID, - 320, - ) + 500) out, _ = capsys.readouterr() assert "Total Characters: 9" in out From 6bbaa7d249b2ba7a589b14c9ff8c4ff916446da0 Mon Sep 17 00:00:00 2001 From: Nick Cain Date: Fri, 8 Oct 2021 19:03:11 +0000 Subject: [PATCH 2/2] Updates after PR review --- .github/CODEOWNERS | 2 +- ...ranslate_v3_batch_translate_text_with_glossary_test.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index b3b89075..cdf66106 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -11,4 +11,4 @@ /samples/**/*.py @googleapis/cdpe-cloudai # The python-samples-owners team is the default owner for samples -/samples/**/*.py @aribray @sirtorry @googleapis/python-samples-owners \ No newline at end of file +/samples/**/*.py @aribray @googleapis/python-samples-owners \ No newline at end of file diff --git a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py index 658ce571..088d8977 100644 --- a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -48,7 +48,11 @@ def on_backoff(invocation_dict): invocation_dict['kwargs']['bucket'] = next(get_ephemeral_bucket()) -@backoff.on_exception(wait_gen=lambda : iter([100, 250, 300, 500]), exception=Exception, max_tries=5, on_backoff=on_backoff) +# If necessary, retry test function while backing off the timeout sequentially +MAX_TIMEOUT = 500 + + +@backoff.on_exception(wait_gen=lambda : iter([100, 250, 300, MAX_TIMEOUT]), exception=Exception, max_tries=5, on_backoff=on_backoff) def test_batch_translate_text_with_glossary(capsys, bucket): translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( @@ -56,7 +60,7 @@ def test_batch_translate_text_with_glossary(capsys, bucket): "gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name), PROJECT_ID, GLOSSARY_ID, - 500) + MAX_TIMEOUT) out, _ = capsys.readouterr() assert "Total Characters: 9" in out