1515import os
1616import uuid
1717
18+ import backoff
1819from google .cloud import storage
1920import pytest
2021
2526GLOSSARY_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 )
4156def 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