Skip to content

Commit bb011e1

Browse files
authored
samples: increase timeout, catch concurrent.futures.TimeoutError (#266)
* samples: catch concurrent.futures.TimeoutError * fix: use default timeout in samples file
1 parent 26aea2f commit bb011e1

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

dlp/snippets/risk.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def numerical_risk_analysis(
4848
Returns:
4949
None; the response from the API is printed to the terminal.
5050
"""
51+
import concurrent.futures
5152

5253
# Import the client library.
5354
import google.cloud.dlp
@@ -116,7 +117,7 @@ def callback(message):
116117

117118
try:
118119
subscription.result(timeout=timeout)
119-
except TimeoutError:
120+
except concurrent.futures.TimeoutError:
120121
print(
121122
"No event received before the timeout. Please verify that the "
122123
"subscription provided is subscribed to the topic provided."
@@ -156,6 +157,7 @@ def categorical_risk_analysis(
156157
Returns:
157158
None; the response from the API is printed to the terminal.
158159
"""
160+
import concurrent.futures
159161

160162
# Import the client library.
161163
import google.cloud.dlp
@@ -237,7 +239,7 @@ def callback(message):
237239

238240
try:
239241
subscription.result(timeout=timeout)
240-
except TimeoutError:
242+
except concurrent.futures.TimeoutError:
241243
print(
242244
"No event received before the timeout. Please verify that the "
243245
"subscription provided is subscribed to the topic provided."
@@ -277,6 +279,7 @@ def k_anonymity_analysis(
277279
Returns:
278280
None; the response from the API is printed to the terminal.
279281
"""
282+
import concurrent.futures
280283

281284
# Import the client library.
282285
import google.cloud.dlp
@@ -367,7 +370,7 @@ def callback(message):
367370

368371
try:
369372
subscription.result(timeout=timeout)
370-
except TimeoutError:
373+
except concurrent.futures.TimeoutError:
371374
print(
372375
"No event received before the timeout. Please verify that the "
373376
"subscription provided is subscribed to the topic provided."
@@ -409,6 +412,7 @@ def l_diversity_analysis(
409412
Returns:
410413
None; the response from the API is printed to the terminal.
411414
"""
415+
import concurrent.futures
412416

413417
# Import the client library.
414418
import google.cloud.dlp
@@ -509,7 +513,7 @@ def callback(message):
509513

510514
try:
511515
subscription.result(timeout=timeout)
512-
except TimeoutError:
516+
except concurrent.futures.TimeoutError:
513517
print(
514518
"No event received before the timeout. Please verify that the "
515519
"subscription provided is subscribed to the topic provided."
@@ -558,6 +562,7 @@ def k_map_estimate_analysis(
558562
Returns:
559563
None; the response from the API is printed to the terminal.
560564
"""
565+
import concurrent.futures
561566

562567
# Import the client library.
563568
import google.cloud.dlp
@@ -659,7 +664,7 @@ def callback(message):
659664

660665
try:
661666
subscription.result(timeout=timeout)
662-
except TimeoutError:
667+
except concurrent.futures.TimeoutError:
663668
print(
664669
"No event received before the timeout. Please verify that the "
665670
"subscription provided is subscribed to the topic provided."

dlp/snippets/risk_test.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
BIGQUERY_TABLE_ID = "dlp_test_table" + UNIQUE_STRING
3737
BIGQUERY_HARMFUL_TABLE_ID = "harmful" + UNIQUE_STRING
3838

39-
TIMEOUT = 120 # 2 minutes
40-
4139

4240
# Create new custom topic/subscription
4341
# We observe sometimes all the tests in this file fail. In a
@@ -173,7 +171,6 @@ def test_numerical_risk_analysis(topic_id, subscription_id, bigquery_project, ca
173171
NUMERIC_FIELD,
174172
topic_id,
175173
subscription_id,
176-
timeout=TIMEOUT,
177174
)
178175

179176
out, _ = capsys.readouterr()
@@ -192,7 +189,6 @@ def test_categorical_risk_analysis_on_string_field(
192189
UNIQUE_FIELD,
193190
topic_id,
194191
subscription_id,
195-
timeout=TIMEOUT,
196192
)
197193

198194
out, _ = capsys.readouterr()
@@ -211,7 +207,6 @@ def test_categorical_risk_analysis_on_number_field(
211207
NUMERIC_FIELD,
212208
topic_id,
213209
subscription_id,
214-
timeout=TIMEOUT,
215210
)
216211

217212
out, _ = capsys.readouterr()
@@ -230,7 +225,6 @@ def test_k_anonymity_analysis_single_field(
230225
topic_id,
231226
subscription_id,
232227
[NUMERIC_FIELD],
233-
timeout=TIMEOUT,
234228
)
235229

236230
out, _ = capsys.readouterr()
@@ -250,7 +244,6 @@ def test_k_anonymity_analysis_multiple_fields(
250244
topic_id,
251245
subscription_id,
252246
[NUMERIC_FIELD, REPEATED_FIELD],
253-
timeout=TIMEOUT,
254247
)
255248

256249
out, _ = capsys.readouterr()
@@ -271,7 +264,6 @@ def test_l_diversity_analysis_single_field(
271264
subscription_id,
272265
UNIQUE_FIELD,
273266
[NUMERIC_FIELD],
274-
timeout=TIMEOUT,
275267
)
276268

277269
out, _ = capsys.readouterr()
@@ -293,7 +285,6 @@ def test_l_diversity_analysis_multiple_field(
293285
subscription_id,
294286
UNIQUE_FIELD,
295287
[NUMERIC_FIELD, REPEATED_FIELD],
296-
timeout=TIMEOUT,
297288
)
298289

299290
out, _ = capsys.readouterr()
@@ -315,7 +306,6 @@ def test_k_map_estimate_analysis_single_field(
315306
subscription_id,
316307
[NUMERIC_FIELD],
317308
["AGE"],
318-
timeout=TIMEOUT,
319309
)
320310

321311
out, _ = capsys.readouterr()
@@ -337,7 +327,6 @@ def test_k_map_estimate_analysis_multiple_field(
337327
subscription_id,
338328
[NUMERIC_FIELD, STRING_BOOLEAN_FIELD],
339329
["AGE", "GENDER"],
340-
timeout=TIMEOUT,
341330
)
342331

343332
out, _ = capsys.readouterr()
@@ -360,5 +349,4 @@ def test_k_map_estimate_analysis_quasi_ids_info_types_equal(
360349
subscription_id,
361350
[NUMERIC_FIELD, STRING_BOOLEAN_FIELD],
362351
["AGE"],
363-
timeout=TIMEOUT,
364352
)

0 commit comments

Comments
 (0)