15
15
import os
16
16
import uuid
17
17
18
+ import backoff
18
19
from google .cloud import storage
19
20
import pytest
20
21
25
26
GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY"
26
27
27
28
28
- @pytest .fixture (scope = "function" )
29
- def bucket ():
29
+ def get_ephemeral_bucket ():
30
30
"""Create a temporary bucket to store annotation output."""
31
31
bucket_name = f"tmp-{ uuid .uuid4 ().hex } "
32
32
storage_client = storage .Client ()
@@ -37,15 +37,30 @@ def bucket():
37
37
bucket .delete (force = True )
38
38
39
39
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 )
41
56
def test_batch_translate_text_with_glossary (capsys , bucket ):
57
+
42
58
translate_v3_batch_translate_text_with_glossary .batch_translate_text_with_glossary (
43
59
"gs://cloud-samples-data/translation/text_with_glossary.txt" ,
44
60
"gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/" .format (bucket .name ),
45
61
PROJECT_ID ,
46
62
GLOSSARY_ID ,
47
- 320 ,
48
- )
63
+ MAX_TIMEOUT )
49
64
50
65
out , _ = capsys .readouterr ()
51
66
assert "Total Characters: 9" in out
0 commit comments