Skip to content

Commit f51d1e4

Browse files
committed
refactor fixtures with inner functions
1 parent 2756a9b commit f51d1e4

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

dataproc/snippets/submit_job_test.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,39 @@
3838

3939
@pytest.fixture(autouse=True)
4040
def setup_teardown():
41+
cluster_client = dataproc.ClusterControllerClient(
42+
client_options={
43+
"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)
44+
}
45+
)
46+
4147
# Retry on InvalidArgument subnetwork not ready error
4248
@backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3)
43-
def eventually_consistent_operation():
49+
def setup():
50+
# Create the cluster.
51+
operation = cluster_client.create_cluster(
52+
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
53+
)
54+
operation.result()
55+
56+
def teardown():
4457
try:
45-
cluster_client = dataproc.ClusterControllerClient(
46-
client_options={
47-
"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)
58+
operation = cluster_client.delete_cluster(
59+
request={
60+
"project_id": PROJECT_ID,
61+
"region": REGION,
62+
"cluster_name": CLUSTER_NAME,
4863
}
4964
)
50-
51-
# Create the cluster.
52-
operation = cluster_client.create_cluster(
53-
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
54-
)
55-
print("hi")
5665
operation.result()
57-
print("yes")
58-
yield
59-
60-
finally:
61-
try:
62-
operation = cluster_client.delete_cluster(
63-
request={
64-
"project_id": PROJECT_ID,
65-
"region": REGION,
66-
"cluster_name": CLUSTER_NAME,
67-
}
68-
)
69-
operation.result()
7066

71-
except NotFound:
72-
print("Cluster already deleted")
73-
eventually_consistent_operation()
67+
except NotFound:
68+
print("Cluster already deleted")
69+
try:
70+
setup()
71+
yield
72+
finally:
73+
teardown()
7474

7575

7676
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=5)

dataproc/snippets/update_cluster_test.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,30 @@ def cluster_client():
5353
def setup_teardown(cluster_client):
5454
# InvalidArgument is thrown when the subnetwork is not ready
5555
@backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3)
56-
def eventually_consistent_operation():
56+
def setup():
57+
# Create the cluster.
58+
operation = cluster_client.create_cluster(
59+
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
60+
)
61+
operation.result()
62+
63+
def teardown():
5764
try:
58-
# Create the cluster.
59-
operation = cluster_client.create_cluster(
60-
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
65+
operation = cluster_client.delete_cluster(
66+
request={
67+
"project_id": PROJECT_ID,
68+
"region": REGION,
69+
"cluster_name": CLUSTER_NAME,
70+
}
6171
)
6272
operation.result()
63-
64-
yield
65-
finally:
66-
try:
67-
operation = cluster_client.delete_cluster(
68-
request={
69-
"project_id": PROJECT_ID,
70-
"region": REGION,
71-
"cluster_name": CLUSTER_NAME,
72-
}
73-
)
74-
operation.result()
75-
except NotFound:
76-
print("Cluster already deleted")
77-
eventually_consistent_operation()
73+
except NotFound:
74+
print("Cluster already deleted")
75+
try:
76+
setup()
77+
yield
78+
finally:
79+
teardown()
7880

7981

8082
@backoff.on_exception(backoff.expo, (InternalServerError, ServiceUnavailable), max_tries=5)

0 commit comments

Comments
 (0)