Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a44b67f

Browse files
author
Takashi Matsuo
authored
[jobs] testing: reduce test flakiness (GoogleCloudPlatform#3278)
* [jobs] testing: reduce test flakiness fixes GoogleCloudPlatform#2945 fixes GoogleCloudPlatform#2818 There were 8 tests which has `time.sleep(10)` for waiting data propagation or whatever. I chose to use `@eventually_consistent.call` instead of sleep in those tests. Bonus point is now the tests are significantly faster because it doesn't always wait for 10 seconds. The downside is it will take a long time on failure. We may want to specify the retry count on the eventualy consistent decorator once the following issue is fixed: GoogleCloudPlatform/python-repo-tools#25 * style fix
1 parent 5b5599f commit a44b67f

17 files changed

+319
-138
lines changed

jobs/v3/api_client/auto_complete_sample.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ def auto_complete_default(client_service, query, company_name):
5050
# [END auto_complete_default]
5151

5252

53-
def run_sample():
53+
def set_up():
5454
import base_company_sample
5555
import base_job_sample
56-
5756
company_to_be_created = base_company_sample.generate_company()
5857
company_created = base_company_sample.create_company(
5958
client_service, company_to_be_created)
@@ -64,16 +63,25 @@ def run_sample():
6463
job_to_be_created.update({'title': 'Software engineer'})
6564
job_name = base_job_sample.create_job(client_service,
6665
job_to_be_created).get('name')
66+
return company_name, job_name
6767

68-
# Wait several seconds for post processing
69-
time.sleep(10)
70-
auto_complete_default(client_service, 'goo', company_name)
71-
auto_complete_default(client_service, 'sof', company_name)
72-
job_title_auto_complete(client_service, 'sof', company_name)
7368

69+
def tear_down(company_name, job_name):
70+
import base_company_sample
71+
import base_job_sample
7472
base_job_sample.delete_job(client_service, job_name)
7573
base_company_sample.delete_company(client_service, company_name)
7674

7775

76+
def run_sample(company_name):
77+
auto_complete_default(client_service, 'goo', company_name)
78+
auto_complete_default(client_service, 'sof', company_name)
79+
job_title_auto_complete(client_service, 'sof', company_name)
80+
81+
7882
if __name__ == '__main__':
79-
run_sample()
83+
company_name, job_name = set_up()
84+
# Wait several seconds for post processing
85+
time.sleep(10)
86+
run_sample(company_name)
87+
tear_down(company_name, job_name)

jobs/v3/api_client/auto_complete_sample_test.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
import re
1517

16-
def test_auto_complete_sample(capsys):
17-
import auto_complete_sample
18-
import re
18+
from gcp_devrel.testing import eventually_consistent
1919

20-
auto_complete_sample.run_sample()
21-
out, _ = capsys.readouterr()
22-
expected = (
23-
'.*completionResults.*'
24-
'suggestion.*Google.*type.*COMPANY_NAME.*\n'
25-
'.*completionResults.*'
26-
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
27-
'.*completionResults.*'
28-
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
29-
)
30-
assert re.search(expected, out)
20+
import auto_complete_sample
21+
22+
23+
@pytest.fixture(scope="module")
24+
def company_name():
25+
company_name, job_name = auto_complete_sample.set_up()
26+
yield company_name
27+
auto_complete_sample.tear_down(company_name, job_name)
28+
29+
30+
def test_auto_complete_sample(company_name, capsys):
31+
@eventually_consistent.call
32+
def _():
33+
auto_complete_sample.run_sample(company_name)
34+
out, _ = capsys.readouterr()
35+
expected = (
36+
'.*completionResults.*'
37+
'suggestion.*Google.*type.*COMPANY_NAME.*\n'
38+
'.*completionResults.*'
39+
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
40+
'.*completionResults.*'
41+
'suggestion.*Software Engineer.*type.*JOB_TITLE.*\n'
42+
)
43+
assert re.search(expected, out)

jobs/v3/api_client/commute_search_sample.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import time
2020

2121
from googleapiclient.discovery import build
22-
2322
client_service = build('jobs', 'v3')
2423
parent = 'projects/' + os.environ['GOOGLE_CLOUD_PROJECT']
2524
# [END instantiate]
@@ -54,7 +53,7 @@ def commute_search(client_service, company_name):
5453
# [END commute_search]
5554

5655

57-
def run_sample():
56+
def set_up():
5857
import base_company_sample
5958
import base_job_sample
6059

@@ -70,14 +69,24 @@ def run_sample():
7069
})
7170
job_name = base_job_sample.create_job(client_service,
7271
job_to_be_created).get('name')
72+
return company_name, job_name
7373

74-
# Wait several seconds for post processing
75-
time.sleep(10)
76-
commute_search(client_service, company_name)
74+
75+
def tear_down(company_name, job_name):
76+
import base_company_sample
77+
import base_job_sample
7778

7879
base_job_sample.delete_job(client_service, job_name)
7980
base_company_sample.delete_company(client_service, company_name)
8081

8182

83+
def run_sample(company_name):
84+
commute_search(client_service, company_name)
85+
86+
8287
if __name__ == '__main__':
83-
run_sample()
88+
company_name, job_name = set_up()
89+
# Wait several seconds for post processing
90+
time.sleep(10)
91+
run_sample(company_name)
92+
tear_down(company_name, job_name)

jobs/v3/api_client/commute_search_sample_test.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
import re
1517

16-
def test_commute_search_sample(capsys):
17-
import commute_search_sample
18-
import re
18+
from gcp_devrel.testing import eventually_consistent
1919

20-
commute_search_sample.run_sample()
21-
out, _ = capsys.readouterr()
22-
expected = ('.*matchingJobs.*1600 Amphitheatre Pkwy.*')
23-
assert re.search(expected, out)
20+
import commute_search_sample
21+
22+
23+
@pytest.fixture(scope="module")
24+
def company_name():
25+
company_name, job_name = commute_search_sample.set_up()
26+
yield company_name
27+
commute_search_sample.tear_down(company_name, job_name)
28+
29+
30+
def test_commute_search_sample(company_name, capsys):
31+
@eventually_consistent.call
32+
def _():
33+
commute_search_sample.run_sample(company_name)
34+
out, _ = capsys.readouterr()
35+
expected = ('.*matchingJobs.*1600 Amphitheatre Pkwy.*')
36+
assert re.search(expected, out)

jobs/v3/api_client/custom_attribute_sample.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def custom_attribute_filter_multi_attributes(client_service):
133133
# [END custom_attribute_filter_multi_attributes]
134134

135135

136-
def run_sample():
136+
def set_up():
137137
import base_company_sample
138138
import base_job_sample
139139

@@ -145,16 +145,24 @@ def run_sample():
145145
job_to_be_created = generate_job_with_custom_attributes(company_name)
146146
job_name = base_job_sample.create_job(client_service,
147147
job_to_be_created).get('name')
148+
return company_name, job_name
148149

149-
# Wait several seconds for post processing
150-
time.sleep(10)
151-
custom_attribute_filter_string_value(client_service)
152-
custom_attribute_filter_long_value(client_service)
153-
custom_attribute_filter_multi_attributes(client_service)
154150

151+
def tear_down(company_name, job_name):
152+
import base_company_sample
153+
import base_job_sample
155154
base_job_sample.delete_job(client_service, job_name)
156155
base_company_sample.delete_company(client_service, company_name)
157156

158157

158+
def run_sample():
159+
custom_attribute_filter_string_value(client_service)
160+
custom_attribute_filter_long_value(client_service)
161+
custom_attribute_filter_multi_attributes(client_service)
162+
163+
159164
if __name__ == '__main__':
160-
run_sample()
165+
company_name, job_name = set_up()
166+
# Wait several seconds for post processing
167+
time.sleep(10)
168+
run_sample(company_name, job_name)

jobs/v3/api_client/custom_attribute_sample_test.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
import re
1517

16-
def test_custom_attribute_sample(capsys):
17-
import custom_attribute_sample
18-
import re
18+
from gcp_devrel.testing import eventually_consistent
1919

20-
custom_attribute_sample.run_sample()
21-
out, _ = capsys.readouterr()
22-
expected = ('.*Job created:.*job_with_custom_attributes.*\n'
23-
'.*matchingJobs.*job_with_custom_attributes.*\n'
24-
'.*matchingJobs.*job_with_custom_attributes.*\n'
25-
'.*matchingJobs.*job_with_custom_attributes.*\n')
26-
assert re.search(expected, out, re.DOTALL)
20+
import custom_attribute_sample
21+
22+
23+
@pytest.fixture(scope="module")
24+
def create_data():
25+
company_name, job_name = custom_attribute_sample.set_up()
26+
yield
27+
custom_attribute_sample.tear_down(company_name, job_name)
28+
29+
30+
def test_custom_attribute_sample(create_data, capsys):
31+
@eventually_consistent.call
32+
def _():
33+
custom_attribute_sample.run_sample()
34+
out, _ = capsys.readouterr()
35+
expected = ('.*matchingJobs.*job_with_custom_attributes.*\n'
36+
'.*matchingJobs.*job_with_custom_attributes.*\n'
37+
'.*matchingJobs.*job_with_custom_attributes.*\n')
38+
assert re.search(expected, out, re.DOTALL)

jobs/v3/api_client/email_alert_search_sample.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def search_for_alerts(client_service, company_name):
4444
# [END search_for_alerts]
4545

4646

47-
def run_sample():
47+
def set_up():
4848
import base_company_sample
4949
import base_job_sample
5050

@@ -58,13 +58,23 @@ def run_sample():
5858
job_name = base_job_sample.create_job(client_service,
5959
job_to_be_created).get('name')
6060

61-
# Wait several seconds for post processing
62-
time.sleep(10)
63-
search_for_alerts(client_service, company_name)
61+
return company_name, job_name
6462

63+
64+
def tear_down(company_name, job_name):
65+
import base_company_sample
66+
import base_job_sample
6567
base_job_sample.delete_job(client_service, job_name)
6668
base_company_sample.delete_company(client_service, company_name)
6769

6870

71+
def run_sample(company_name):
72+
search_for_alerts(client_service, company_name)
73+
74+
6975
if __name__ == '__main__':
70-
run_sample()
76+
company_name, job_name = set_up()
77+
# Wait several seconds for post processing
78+
time.sleep(10)
79+
run_sample(company_name)
80+
tear_down(company_name, job_name)

jobs/v3/api_client/email_alert_search_sample_test.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
import re
1517

16-
def test_email_alert_search_sample(capsys):
17-
import email_alert_search_sample
18-
import re
18+
from gcp_devrel.testing import eventually_consistent
1919

20-
email_alert_search_sample.run_sample()
21-
out, _ = capsys.readouterr()
22-
expected = ('.*matchingJobs.*')
23-
assert re.search(expected, out)
20+
import email_alert_search_sample
21+
22+
23+
@pytest.fixture(scope="module")
24+
def company_name():
25+
company_name, job_name = email_alert_search_sample.set_up()
26+
yield company_name
27+
email_alert_search_sample.tear_down(company_name, job_name)
28+
29+
30+
def test_email_alert_search_sample(company_name, capsys):
31+
@eventually_consistent.call
32+
def _():
33+
email_alert_search_sample.run_sample(company_name)
34+
out, _ = capsys.readouterr()
35+
expected = ('.*matchingJobs.*')
36+
assert re.search(expected, out)

jobs/v3/api_client/featured_job_search_sample.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def search_featured_job(client_service, company_name):
7474
# [END search_featured_job]
7575

7676

77-
def run_sample():
77+
def set_up():
7878
import base_company_sample
7979
import base_job_sample
8080

@@ -86,14 +86,24 @@ def run_sample():
8686
job_to_be_created = generate_featured_job(company_name)
8787
job_name = base_job_sample.create_job(client_service,
8888
job_to_be_created).get('name')
89+
return company_name, job_name
8990

90-
# Wait several seconds for post processing
91-
time.sleep(10)
92-
search_featured_job(client_service, company_name)
91+
92+
def tear_down(company_name, job_name):
93+
import base_company_sample
94+
import base_job_sample
9395

9496
base_job_sample.delete_job(client_service, job_name)
9597
base_company_sample.delete_company(client_service, company_name)
9698

9799

100+
def run_sample(company_name):
101+
search_featured_job(client_service, company_name)
102+
103+
98104
if __name__ == '__main__':
99-
run_sample()
105+
company_name, job_name = set_up()
106+
# Wait several seconds for post processing
107+
time.sleep(10)
108+
run_sample(company_name)
109+
tear_down(company_name, job_name)

jobs/v3/api_client/featured_job_search_sample_test.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
import re
1517

16-
def test_featured_job_search_sample(capsys):
17-
import featured_job_search_sample
18-
import re
18+
from gcp_devrel.testing import eventually_consistent
1919

20-
featured_job_search_sample.run_sample()
21-
out, _ = capsys.readouterr()
22-
expected = ('.*matchingJobs.*')
23-
assert re.search(expected, out)
20+
import featured_job_search_sample
21+
22+
23+
@pytest.fixture(scope="module")
24+
def company_name():
25+
company_name, job_name = featured_job_search_sample.set_up()
26+
yield company_name
27+
featured_job_search_sample.tear_down(company_name, job_name)
28+
29+
30+
def test_featured_job_search_sample(company_name, capsys):
31+
@eventually_consistent.call
32+
def _():
33+
featured_job_search_sample.run_sample(company_name)
34+
out, _ = capsys.readouterr()
35+
expected = ('.*matchingJobs.*')
36+
assert re.search(expected, out)

0 commit comments

Comments
 (0)