Skip to content

Commit aaff0be

Browse files
author
Takashi Matsuo
committed
use backoff instead of flaky
1 parent 9fa6ea4 commit aaff0be

File tree

9 files changed

+243
-210
lines changed

9 files changed

+243
-210
lines changed

datalabeling/create_annotation_spec_set_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def cleaner():
3939

4040
def test_create_annotation_spec_set(cleaner, capsys):
4141

42-
@backoff.on_exception(backoff.expo, DeadlineExceeded, max_time=60)
42+
@backoff.on_exception(
43+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
4344
def run_sample():
4445
return create_annotation_spec_set.create_annotation_spec_set(PROJECT_ID)
4546

datalabeling/create_instruction_test.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,39 @@
1616

1717
import os
1818

19-
from google.api_core.client_options import ClientOptions
20-
from google.cloud import datalabeling_v1beta1 as datalabeling
19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
2121
import pytest
2222

2323
import create_instruction
24+
import testing_lib
25+
2426

2527
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
2628
INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling'
2729
'/instruction/test.pdf')
2830

2931

30-
@pytest.mark.flaky(max_runs=3)
31-
def test_create_instruction(capsys):
32-
result = create_instruction.create_instruction(
33-
PROJECT_ID,
34-
'IMAGE',
35-
INSTRUCTION_GCS_URI
36-
)
37-
out, _ = capsys.readouterr()
38-
assert 'The instruction resource name: ' in out
32+
@pytest.fixture(scope='module')
33+
def cleaner():
34+
resource_names = []
35+
36+
yield resource_names
37+
38+
for resource_name in resource_names:
39+
testing_lib.delete_instruction(resource_name)
3940

40-
# Delete the created instruction.
41-
instruction_name = result.name
42-
client = datalabeling.DataLabelingServiceClient()
4341

44-
# If provided, use a provided test endpoint - this will prevent tests on
45-
# this snippet from triggering any action by a real human
46-
if 'DATALABELING_ENDPOINT' in os.environ:
47-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
48-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
42+
def test_create_instruction(cleaner, capsys):
4943

50-
client.delete_instruction(instruction_name)
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: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,48 @@
1616

1717
import os
1818

19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
1921
import pytest
2022

2123
import import_data
2224
import manage_dataset
25+
import testing_lib
26+
2327

2428
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
2529
INPUT_GCS_URI = 'gs://cloud-samples-data/datalabeling/image/image_dataset.csv'
2630

2731

2832
@pytest.fixture(scope='function')
2933
def dataset():
30-
# create a temporary dataset
31-
dataset = manage_dataset.create_dataset(PROJECT_ID)
34+
35+
@backoff.on_exception(
36+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
37+
def create_dataset():
38+
# create a temporary dataset
39+
return manage_dataset.create_dataset(PROJECT_ID)
40+
41+
dataset = create_dataset()
3242

3343
yield dataset
3444

35-
# tear down
36-
manage_dataset.delete_dataset(dataset.name)
45+
@backoff.on_exception(
46+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
47+
def delete_dataset():
48+
# tear down
49+
manage_dataset.delete_dataset(dataset.name)
50+
51+
delete_dataset()
3752

3853

39-
@pytest.mark.flaky(max_runs=3)
4054
def test_import_data(capsys, dataset):
41-
import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI)
55+
56+
@backoff.on_exception(
57+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
58+
def run_sample():
59+
import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI)
60+
61+
run_sample()
4262
out, _ = capsys.readouterr()
4363
assert 'Dataset resource name: ' in out

datalabeling/label_image_test.py

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,101 +16,77 @@
1616

1717
import os
1818

19-
from google.api_core.client_options import ClientOptions
20-
from google.cloud import datalabeling_v1beta1 as datalabeling
19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
2121
import pytest
2222

23-
import create_annotation_spec_set
24-
import create_instruction
25-
import import_data
2623
import label_image
27-
import manage_dataset
24+
import testing_lib
25+
2826

2927
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
3028
INPUT_GCS_URI = 'gs://cloud-samples-data/datalabeling/image/image_dataset.csv'
29+
INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling'
30+
'/instruction/test.pdf')
3131

3232

3333
@pytest.fixture(scope='function')
3434
def dataset():
3535
# create a temporary dataset
36-
dataset = manage_dataset.create_dataset(PROJECT_ID)
37-
38-
# import some data to it
39-
import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI)
36+
dataset = testing_lib.create_dataset(PROJECT_ID)
4037

4138
yield dataset
4239

4340
# tear down
44-
manage_dataset.delete_dataset(dataset.name)
41+
testing_lib.delete_dataset(dataset.name)
4542

4643

4744
@pytest.fixture(scope='function')
4845
def annotation_spec_set():
4946
# create a temporary annotation_spec_set
50-
response = create_annotation_spec_set.create_annotation_spec_set(
51-
PROJECT_ID)
47+
response = testing_lib.create_annotation_spec_set(PROJECT_ID)
5248

5349
yield response
5450

55-
# tear down
56-
client = datalabeling.DataLabelingServiceClient()
57-
58-
# If provided, use a provided test endpoint - this will prevent tests on
59-
# this snippet from triggering any action by a real human
60-
if 'DATALABELING_ENDPOINT' in os.environ:
61-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
62-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
63-
64-
client.delete_annotation_spec_set(response.name)
51+
testing_lib.delete_annotation_spec_set(response.name)
6552

6653

6754
@pytest.fixture(scope='function')
6855
def instruction():
6956
# create a temporary instruction
70-
instruction = create_instruction.create_instruction(
71-
PROJECT_ID, 'IMAGE',
72-
'gs://cloud-samples-data/datalabeling/instruction/test.pdf')
57+
instruction = testing_lib.create_instruction(
58+
PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI)
7359

7460
yield instruction
7561

7662
# tear down
77-
client = datalabeling.DataLabelingServiceClient()
63+
testing_lib.delete_instruction(instruction.name)
64+
7865

79-
# If provided, use a provided test endpoint - this will prevent tests on
80-
# this snippet from triggering any action by a real human
81-
if 'DATALABELING_ENDPOINT' in os.environ:
82-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
83-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
66+
@pytest.fixture(scope='module')
67+
def cleaner():
68+
resource_names = []
8469

85-
client.delete_instruction(instruction.name)
70+
yield resource_names
71+
72+
for resource_name in resource_names:
73+
testing_lib.cancel_operation(resource_name)
8674

8775

8876
# Passing in dataset as the last argument in test_label_image since it needs
8977
# to be deleted before the annotation_spec_set can be deleted.
90-
@pytest.mark.flaky(max_runs=3)
91-
def test_label_image(capsys, annotation_spec_set, instruction, dataset):
92-
93-
# Start labeling.
94-
response = label_image.label_image(
95-
dataset.name,
96-
instruction.name,
97-
annotation_spec_set.name
98-
)
99-
out, _ = capsys.readouterr()
100-
assert 'Label_image operation name: ' in out
101-
operation_name = response.operation.name
78+
def test_label_image(
79+
capsys, annotation_spec_set, instruction, dataset, cleaner):
10280

103-
# Cancels the labeling operation.
104-
response.cancel()
105-
assert response.cancelled() is True
81+
@backoff.on_exception(
82+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
83+
def run_sample():
84+
# Start labeling.
85+
return label_image.label_image(
86+
dataset.name, instruction.name, annotation_spec_set.name)
10687

107-
client = datalabeling.DataLabelingServiceClient()
88+
response = run_sample()
89+
cleaner.append(response.operation.name)
10890

109-
# If provided, use a provided test endpoint - this will prevent tests on
110-
# this snippet from triggering any action by a real human
111-
if 'DATALABELING_ENDPOINT' in os.environ:
112-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
113-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
114-
115-
client.transport._operations_client.cancel_operation(
116-
operation_name)
91+
out, _ = capsys.readouterr()
92+
assert 'Label_image operation name: ' in out

datalabeling/label_text_test.py

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,101 +16,81 @@
1616

1717
import os
1818

19-
from google.api_core.client_options import ClientOptions
20-
from google.cloud import datalabeling_v1beta1 as datalabeling
19+
import backoff
20+
from google.api_core.exceptions import DeadlineExceeded
2121
import pytest
2222

23-
import create_annotation_spec_set
24-
import create_instruction
25-
import import_data
2623
import label_text
27-
import manage_dataset
24+
import testing_lib
2825

2926
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
3027
INPUT_GCS_URI = 'gs://cloud-samples-data/datalabeling/text/input.csv'
28+
INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling'
29+
'/instruction/test.pdf')
3130

3231

3332
@pytest.fixture(scope='function')
3433
def dataset():
3534
# create a temporary dataset
36-
dataset = manage_dataset.create_dataset(PROJECT_ID)
35+
dataset = testing_lib.create_dataset(PROJECT_ID)
3736

38-
# import some data to it
39-
import_data.import_data(dataset.name, 'TEXT', INPUT_GCS_URI)
37+
testing_lib.import_data(dataset.name, 'TEXT', INPUT_GCS_URI)
4038

4139
yield dataset
4240

4341
# tear down
44-
manage_dataset.delete_dataset(dataset.name)
42+
testing_lib.delete_dataset(dataset.name)
4543

4644

4745
@pytest.fixture(scope='function')
4846
def annotation_spec_set():
4947
# create a temporary annotation_spec_set
50-
response = create_annotation_spec_set.create_annotation_spec_set(
51-
PROJECT_ID)
48+
response = testing_lib.create_annotation_spec_set(PROJECT_ID)
5249

5350
yield response
5451

55-
# tear down
56-
client = datalabeling.DataLabelingServiceClient()
57-
58-
# If provided, use a provided test endpoint - this will prevent tests on
59-
# this snippet from triggering any action by a real human
60-
if 'DATALABELING_ENDPOINT' in os.environ:
61-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
62-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
63-
64-
client.delete_annotation_spec_set(response.name)
52+
testing_lib.delete_annotation_spec_set(response.name)
6553

6654

6755
@pytest.fixture(scope='function')
6856
def instruction():
6957
# create a temporary instruction
70-
instruction = create_instruction.create_instruction(
71-
PROJECT_ID, 'TEXT',
72-
'gs://cloud-samples-data/datalabeling/instruction/test.pdf')
58+
instruction = testing_lib.create_instruction(
59+
PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI)
7360

7461
yield instruction
7562

7663
# tear down
77-
client = datalabeling.DataLabelingServiceClient()
64+
testing_lib.delete_instruction(instruction.name)
7865

79-
# If provided, use a provided test endpoint - this will prevent tests on
80-
# this snippet from triggering any action by a real human
81-
if 'DATALABELING_ENDPOINT' in os.environ:
82-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
83-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
8466

85-
client.delete_instruction(instruction.name)
67+
@pytest.fixture(scope='module')
68+
def cleaner():
69+
resource_names = []
70+
71+
yield resource_names
72+
73+
for resource_name in resource_names:
74+
testing_lib.cancel_operation(resource_name)
8675

8776

8877
# Passing in dataset as the last argument in test_label_image since it needs
8978
# to be deleted before the annotation_spec_set can be deleted.
90-
@pytest.mark.flaky(max_runs=3)
91-
def test_label_text(capsys, annotation_spec_set, instruction, dataset):
92-
93-
# Start labeling.
94-
response = label_text.label_text(
95-
dataset.name,
96-
instruction.name,
97-
annotation_spec_set.name
98-
)
79+
def test_label_text(capsys, annotation_spec_set, instruction, dataset, cleaner):
80+
81+
@backoff.on_exception(
82+
backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE)
83+
def run_sample():
84+
# Start labeling.
85+
return label_text.label_text(
86+
dataset.name, instruction.name, annotation_spec_set.name)
87+
88+
response = run_sample()
89+
cleaner.append(response.operation.name)
90+
9991
out, _ = capsys.readouterr()
10092
assert 'Label_text operation name: ' in out
101-
operation_name = response.operation.name
10293

10394
# Cancels the labeling operation.
10495
response.cancel()
10596
assert response.cancelled() is True
106-
107-
client = datalabeling.DataLabelingServiceClient()
108-
109-
# If provided, use a provided test endpoint - this will prevent tests on
110-
# this snippet from triggering any action by a real human
111-
if 'DATALABELING_ENDPOINT' in os.environ:
112-
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
113-
client = datalabeling.DataLabelingServiceClient(client_options=opts)
114-
115-
client.transport._operations_client.cancel_operation(
116-
operation_name)

0 commit comments

Comments
 (0)