[datalabeling] testing: wrap rpcs with backoff (#3443) · code4ward/python-docs-samples@f877a9a · GitHub
Skip to content

Commit f877a9a

Browse files
author
Takashi Matsuo
authored
[datalabeling] testing: wrap rpcs with backoff (GoogleCloudPlatform#3443)
* wrap all the rpcs with backoff * add a shared testing lib * remove flaky
1 parent c71f978 commit f877a9a

10 files changed

Lines changed: 316 additions & 241 deletions

datalabeling/create_annotation_spec_set_test.py

Lines changed: 27 additions & 18 deletions

datalabeling/create_instruction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import argparse
1818
import os
19+
1920
from google.api_core.client_options import ClientOptions
2021

2122

datalabeling/create_instruction_test.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,39 @@
1616

1717
import os
1818

19-
import create_instruction
20-
from google.api_core.client_options import ClientOptions
21-
from google.cloud import datalabeling_v1beta1 as datalabeling
19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
2221
import pytest
2322

23+
import create_instruction
24+
import testing_lib
25+
26+
2427
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
2528
INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling'
2629
'/instruction/test.pdf')
2730

2831

29-
@pytest.mark.slow
30-
def test_create_instruction(capsys):
31-
result = create_instruction.create_instruction(
32-
PROJECT_ID,
33-
'IMAGE',
34-
INSTRUCTION_GCS_URI
35-
)
36-
out, _ = capsys.readouterr()
37-
assert 'The instruction resource name: ' in out
32+
@pytest.fixture(scope='module')
33+
def cleaner():
34+
resource_names = []
3835

39-
# Delete the created instruction.
40-
instruction_name = result.name
41-
client = datalabeling.DataLabelingServiceClient()
36+
yield resource_names
4237

43-
# If provided, use a provided test endpoint - this will prevent tests on
44-
# this snippet from triggering any action by a real human
45-
if 'DATALABELING_ENDPOINT' in os.environ:
46-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
47-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
38+
for resource_name in resource_names:
39+
testing_lib.delete_instruction(resource_name)
4840

49-
client.delete_instruction(instruction_name)
41+
42+
def test_create_instruction(cleaner, capsys):
43+
44+
@backoff.on_exception(
45+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
46+
def run_sample():
47+
return create_instruction.create_instruction(
48+
PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI)
49+
50+
instruction = run_sample()
51+
cleaner.append(instruction.name)
52+
53+
out, _ = capsys.readouterr()
54+
assert 'The instruction resource name: ' in out

datalabeling/import_data_test.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,36 @@
1616

1717
import os
1818

19-
import import_data
20-
import manage_dataset
19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
2121
import pytest
2222

23+
import import_data
24+
import testing_lib
25+
26+
2327
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
2428
INPUT_GCS_URI = 'gs://cloud-samples-data/datalabeling/image/image_dataset.csv'
2529

2630

27-
@pytest.fixture(scope='function')
31+
@pytest.fixture(scope='module')
2832
def dataset():
2933
# create a temporary dataset
30-
dataset = manage_dataset.create_dataset(PROJECT_ID)
34+
dataset = testing_lib.create_dataset(PROJECT_ID)
3135

3236
yield dataset
3337

3438
# tear down
35-
manage_dataset.delete_dataset(dataset.name)
39+
testing_lib.delete_dataset(dataset.name)
3640

3741

38-
@pytest.mark.slow
3942
def test_import_data(capsys, dataset):
40-
import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI)
43+
44+
@backoff.on_exception(
45+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
46+
def run_sample():
47+
import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI)
48+
49+
run_sample()
4150
out, _ = capsys.readouterr()
4251
assert 'Dataset resource name: ' in out

datalabeling/label_image_test.py

Lines changed: 39 additions & 61 deletions

0 commit comments

Comments
 (0)