-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(sqlalchemy-spanner): wire timeout execution option through to DBAPI Connection.timeout #18485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sakthivelmanii
wants to merge
1
commit into
main
Choose a base branch
from
feat/sqlalchemy-spanner-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # Copyright 2026 Google LLC All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest import mock | ||
|
|
||
| from sqlalchemy.testing import eq_ | ||
| from sqlalchemy.testing.plugin.plugin_base import fixtures | ||
|
|
||
| from google.cloud import spanner_dbapi | ||
| from google.cloud.sqlalchemy_spanner.sqlalchemy_spanner import ( | ||
| _UNSET, | ||
| SpannerDialect, | ||
| SpannerExecutionContext, | ||
| reset_connection, | ||
| ) | ||
|
|
||
|
|
||
| class SqlAlchemyTimeoutTest(fixtures.TestBase): | ||
| def test_reset_connection_clears_timeout(self): | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 30.0 | ||
| dbapi_conn.inside_transaction = False | ||
|
|
||
| reset_connection(dbapi_conn, None) | ||
|
|
||
| eq_(dbapi_conn.timeout, None) | ||
|
|
||
| def test_pre_exec_sets_timeout(self): | ||
| context = SpannerExecutionContext() | ||
| context.execution_options = {"timeout": 45.0} | ||
|
|
||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = None | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
|
|
||
| eq_(dbapi_conn.timeout, 45.0) | ||
|
|
||
| def test_query_without_timeout_does_not_alter_connection_timeout(self): | ||
| dialect = SpannerDialect() | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 25.0 | ||
|
|
||
| context = SpannerExecutionContext() | ||
| context.dialect = dialect | ||
| context.execution_options = {} | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
| eq_(dbapi_conn.timeout, 25.0) | ||
|
|
||
| context.post_exec() | ||
| eq_(dbapi_conn.timeout, 25.0) | ||
|
|
||
| def test_statement_level_timeout_restores_previous_timeout_in_post_exec(self): | ||
| dialect = SpannerDialect() | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 25.0 | ||
|
|
||
| context = SpannerExecutionContext() | ||
| context.dialect = dialect | ||
| context.execution_options = {"timeout": 5.0} | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
| eq_(dbapi_conn.timeout, 5.0) | ||
|
|
||
| context.post_exec() | ||
| eq_(dbapi_conn.timeout, 25.0) | ||
|
|
||
| def test_statement_level_timeout_restores_previous_timeout_on_dbapi_exception(self): | ||
| dialect = SpannerDialect() | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 25.0 | ||
|
|
||
| context = SpannerExecutionContext() | ||
| context.dialect = dialect | ||
| context.execution_options = {"timeout": 5.0} | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
| eq_(dbapi_conn.timeout, 5.0) | ||
|
|
||
| context.handle_dbapi_exception(Exception("Statement timeout/error")) | ||
| eq_(dbapi_conn.timeout, 25.0) | ||
|
|
||
| def test_statement_level_timeout_none_overrides_and_restores(self): | ||
| dialect = SpannerDialect() | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 25.0 | ||
|
|
||
| context = SpannerExecutionContext() | ||
| context.dialect = dialect | ||
| context.execution_options = {"timeout": None} | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
| eq_(dbapi_conn.timeout, None) | ||
|
|
||
| context.post_exec() | ||
| eq_(dbapi_conn.timeout, 25.0) | ||
|
|
||
| def test_statement_level_timeout_swallows_exception_on_broken_conn(self): | ||
| dialect = SpannerDialect() | ||
| dbapi_conn = mock.MagicMock(spec=spanner_dbapi.Connection) | ||
| dbapi_conn.timeout = 25.0 | ||
|
|
||
| context = SpannerExecutionContext() | ||
| context.dialect = dialect | ||
| context.execution_options = {"timeout": 5.0} | ||
| context._dbapi_connection = mock.MagicMock() | ||
| context._dbapi_connection.connection = dbapi_conn | ||
|
|
||
| context.pre_exec() | ||
| eq_(dbapi_conn.timeout, 5.0) | ||
|
|
||
| # Simulate broken connection raising on timeout assignment | ||
| type(dbapi_conn).timeout = mock.PropertyMock( | ||
| side_effect=Exception("Connection broken") | ||
| ) | ||
|
|
||
| # Should not raise exception | ||
| context.handle_dbapi_exception(Exception("Original DBAPI error")) | ||
| eq_(context._previous_timeout, _UNSET) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.