Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat: use backoff module instead of flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
nicain committed Oct 7, 2021
1 parent 33b943b commit 5b9f2aa
Showing 1 changed file with 15 additions and 5 deletions.
Expand Up @@ -17,6 +17,7 @@

from google.cloud import storage
import pytest
import backoff

import translate_v3_batch_translate_text_with_glossary

Expand All @@ -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()
Expand All @@ -36,16 +36,26 @@ def bucket():

bucket.delete(force=True)

@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())

@pytest.mark.flaky(max_runs=3, min_passes=1)

@backoff.on_exception(wait_gen=lambda : iter([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

0 comments on commit 5b9f2aa

Please sign in to comment.