|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import time |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +import pytest |
| 19 | +import requests.exceptions |
| 20 | +from google.api_core import exceptions as core_exceptions |
| 21 | +from google.cloud import bigquery |
| 22 | + |
| 23 | +def test_insert_rows_json_ssl_error_no_retry(bigquery_client, dataset_id): |
| 24 | + """ |
| 25 | + Verify that SSLError during insert_rows_json is NOT retried and |
| 26 | + propagates a descriptive error message immediately. |
| 27 | + """ |
| 28 | + table_id = f"{dataset_id}.test_ssl_retry_{int(time.time())}" |
| 29 | + schema = [bigquery.SchemaField("name", "STRING")] |
| 30 | + table = bigquery.Table(table_id, schema=schema) |
| 31 | + bigquery_client.create_table(table) |
| 32 | + |
| 33 | + # We mock the api_request to simulate the GFE abruptly closing the connection |
| 34 | + # which manifests as a requests.exceptions.SSLError. |
| 35 | + original_api_request = bigquery_client._connection.api_request |
| 36 | + call_count = 0 |
| 37 | + |
| 38 | + def mock_api_request(*args, **kwargs): |
| 39 | + nonlocal call_count |
| 40 | + call_count += 1 |
| 41 | + raise requests.exceptions.SSLError("EOF occurred in violation of protocol") |
| 42 | + |
| 43 | + with mock.patch.object(bigquery_client._connection, "api_request", side_effect=mock_api_request): |
| 44 | + # Use a reasonably short deadline for the test, although it should fail on the first attempt anyway. |
| 45 | + retry = bigquery.DEFAULT_RETRY.with_deadline(5.0) |
| 46 | + |
| 47 | + start_time = time.time() |
| 48 | + with pytest.raises(requests.exceptions.SSLError) as excinfo: |
| 49 | + bigquery_client.insert_rows_json( |
| 50 | + table, |
| 51 | + [{"name": "test"}], |
| 52 | + retry=retry |
| 53 | + ) |
| 54 | + duration = time.time() - start_time |
| 55 | + |
| 56 | + # Verification: |
| 57 | + # 1. It should NOT have retried (total calls should be 1) |
| 58 | + assert call_count == 1 |
| 59 | + |
| 60 | + # 2. It should have failed quickly (much less than the 5s deadline) |
| 61 | + assert duration < 2.0 |
| 62 | + |
| 63 | + # 3. The error message should contain our descriptive wrapping |
| 64 | + assert "invalid table schema" in str(excinfo.value) |
| 65 | + assert "SSL/Connection error occurred" in str(excinfo.value) |
| 66 | + |
| 67 | + # Cleanup |
| 68 | + bigquery_client.delete_table(table_id) |
0 commit comments