Skip to content

Commit dc1b7d0

Browse files
nicainparthea
andauthored
Fix: #254 by increasing timeout, use backoff module instead of flaky (#271)
* feat: use backoff module instead of flaky * Updates after PR review Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent eedcab7 commit dc1b7d0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import uuid
1717

18+
import backoff
1819
from google.cloud import storage
1920
import pytest
2021

@@ -25,8 +26,7 @@
2526
GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY"
2627

2728

28-
@pytest.fixture(scope="function")
29-
def bucket():
29+
def get_ephemeral_bucket():
3030
"""Create a temporary bucket to store annotation output."""
3131
bucket_name = f"tmp-{uuid.uuid4().hex}"
3232
storage_client = storage.Client()
@@ -37,15 +37,30 @@ def bucket():
3737
bucket.delete(force=True)
3838

3939

40-
@pytest.mark.flaky(max_runs=3, min_passes=1)
40+
@pytest.fixture(scope="function")
41+
def bucket():
42+
"""Create a bucket feature for testing"""
43+
return next(get_ephemeral_bucket())
44+
45+
46+
def on_backoff(invocation_dict):
47+
"""Backoff callback; create a testing bucket for each backoff run"""
48+
invocation_dict['kwargs']['bucket'] = next(get_ephemeral_bucket())
49+
50+
51+
# If necessary, retry test function while backing off the timeout sequentially
52+
MAX_TIMEOUT = 500
53+
54+
55+
@backoff.on_exception(wait_gen=lambda : iter([100, 250, 300, MAX_TIMEOUT]), exception=Exception, max_tries=5, on_backoff=on_backoff)
4156
def test_batch_translate_text_with_glossary(capsys, bucket):
57+
4258
translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary(
4359
"gs://cloud-samples-data/translation/text_with_glossary.txt",
4460
"gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name),
4561
PROJECT_ID,
4662
GLOSSARY_ID,
47-
320,
48-
)
63+
MAX_TIMEOUT)
4964

5065
out, _ = capsys.readouterr()
5166
assert "Total Characters: 9" in out

0 commit comments

Comments
 (0)