Skip to content

Commit c0af0d0

Browse files
committed
fix unit tests
1 parent 0e2d2a9 commit c0af0d0

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

google/cloud/bigquery/retry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ def _should_retry_get_job_conflict(exc):
9898
return isinstance(exc, exceptions.NotFound) or _should_retry(exc)
9999

100100

101+
# Pick a deadline smaller than our other deadlines since we want to timeout
102+
# before those expire.
103+
_DEFAULT_GET_JOB_CONFLICT_DEADLINE = _DEFAULT_RETRY_DEADLINE / 3.0
101104
_DEFAULT_GET_JOB_CONFLICT_RETRY = retry.Retry(
102-
predicate=_should_retry_get_job_conflict, deadline=_DEFAULT_RETRY_DEADLINE
105+
predicate=_should_retry_get_job_conflict,
106+
deadline=_DEFAULT_GET_JOB_CONFLICT_DEADLINE,
103107
)
104108
"""Private, may be removed in future."""
105109

tests/unit/test_client.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import pytest
3434
import requests
3535

36+
import google.api
37+
3638

3739
try:
3840
import opentelemetry
@@ -5355,7 +5357,7 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
53555357
conn = client._connection = make_connection(
53565358
# We're mocking QueryJob._begin, so this is only going to be
53575359
# jobs.get requests and responses.
5358-
ValueError("not normally retriable, but you said it was"),
5360+
google.api_core.exceptions.TooManyRequests("this is retriable by default"),
53595361
google.api_core.exceptions.NotFound("we lost your job"),
53605362
google.api_core.exceptions.NotFound("we lost your job again, sorry"),
53615363
{
@@ -5367,11 +5369,6 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
53675369
},
53685370
)
53695371

5370-
retry = google.cloud.bigquery.retry.DEFAULT_RETRY.with_predicate(
5371-
# We should respect the predicate the user provides,
5372-
# but amend it with allowing 404 in this particular situation.
5373-
lambda exc: isinstance(exc, ValueError)
5374-
)
53755372
job_create_error = google.api_core.exceptions.Conflict("Job already exists.")
53765373
job_begin_patcher = mock.patch.object(
53775374
bqjob.QueryJob, "_begin", side_effect=job_create_error
@@ -5386,7 +5383,7 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
53865383
# If get job request fails there does exist a job
53875384
# with this ID already, retry 404 until we get it (or fails for a
53885385
# non-retriable reason, see other tests).
5389-
result = client.query("SELECT 1;", job_id=None, retry=retry)
5386+
result = client.query("SELECT 1;", job_id=None)
53905387

53915388
jobs_get_path = mock.call(
53925389
method="GET",
@@ -5404,7 +5401,7 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404(self):
54045401
)
54055402
assert result.job_id == job_id
54065403

5407-
def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404_query_job(
5404+
def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404_and_query_job_insert(
54085405
self,
54095406
):
54105407
"""Regression test for https://github.com/googleapis/python-bigquery/issues/2134
@@ -5441,16 +5438,21 @@ def test_query_job_rpc_fail_w_conflict_random_id_job_fetch_retries_404_query_job
54415438
"make_job_id",
54425439
side_effect=[job_id_1, job_id_2],
54435440
)
5441+
retry_patcher = mock.patch.object(
5442+
google.cloud.bigquery.retry,
5443+
"_DEFAULT_GET_JOB_CONFLICT_RETRY",
5444+
retry,
5445+
)
54445446

54455447
with freezegun.freeze_time(
54465448
"2025-01-01 00:00:00",
54475449
# 10x the retry deadline to guarantee a timeout.
5448-
auto_tick_seconds=10,
5449-
), job_begin_patcher, job_id_patcher:
5450+
auto_tick_seconds=100,
5451+
), job_begin_patcher, job_id_patcher, retry_patcher:
54505452
# If get job request fails there does exist a job
54515453
# with this ID already, retry 404 until we get it (or fails for a
54525454
# non-retriable reason, see other tests).
5453-
result = client.query("SELECT 1;", job_id=None, retry=retry)
5455+
result = client.query("SELECT 1;", job_id=None)
54545456

54555457
jobs_get_path_1 = mock.call(
54565458
method="GET",

0 commit comments

Comments
 (0)