Skip to content

Commit c484713

Browse files
committed
chore: add logs to tests
1 parent 8688757 commit c484713

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

.github/workflows/integration_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
python-version: [ "3.11", "3.12", "3.13" ]
22-
environment: [ "mysql", "pg" ]
22+
environment: [ "pg" ]
2323

2424
steps:
2525
- name: 'Clone repository'
@@ -73,7 +73,7 @@ jobs:
7373
fail-fast: false
7474
matrix:
7575
python-version: [ "3.11", "3.12", "3.13" ]
76-
environment: [ "mysql", "pg" ]
76+
environment: [ "pg" ]
7777

7878
steps:
7979
- name: 'Clone repository'

aws_advanced_python_wrapper/read_write_splitting_plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from copy import deepcopy
1818
from typing import TYPE_CHECKING, Any, Callable, Optional, Protocol, Set, Tuple
1919

20+
from aws_advanced_python_wrapper.sqlalchemy_driver_dialect import \
21+
SqlAlchemyDriverDialect
22+
2023
if TYPE_CHECKING:
2124
from aws_advanced_python_wrapper.driver_dialect import DriverDialect
2225
from aws_advanced_python_wrapper.host_list_provider import HostListProviderService
@@ -402,13 +405,20 @@ def _close_connection_if_idle(self, internal_conn: Optional[Connection]):
402405
current_conn = self._plugin_service.current_connection
403406
driver_dialect = self._plugin_service.driver_dialect
404407

408+
logger.debug(f"current connection during close if idle: {current_conn}: {self._plugin_service.current_host_info}")
405409
if internal_conn == current_conn:
406410
# Connection is in use, do not close
411+
logger.debug("current connection is in use. do not close")
407412
return
408413

409414
try:
410415
if self._is_connection_usable(internal_conn, driver_dialect):
411-
driver_dialect.execute(DbApiMethod.CONNECTION_CLOSE.method_name, lambda: internal_conn.close())
416+
if isinstance(driver_dialect, SqlAlchemyDriverDialect) and hasattr(internal_conn, "invalidate"):
417+
logger.debug(f"Invalidating pooled connection {internal_conn}")
418+
driver_dialect.execute(DbApiMethod.CONNECTION_CLOSE.method_name, lambda: internal_conn.invalidate(soft=True))
419+
else:
420+
logger.debug(f"ReadWriteSplittingPlugin.ClosingIdleConnection: {internal_conn}")
421+
driver_dialect.execute(DbApiMethod.CONNECTION_CLOSE.method_name, lambda: internal_conn.close())
412422
except Exception:
413423
# Ignore exceptions during cleanup - connection might already be dead
414424
pass

tests/integration/container/test_custom_endpoint.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def rds_utils(self):
6161
return RdsTestUtility(region)
6262

6363
@pytest.fixture(scope='class')
64-
def props(self):
64+
def default_props(self):
6565
p: Properties = Properties(
66-
{"plugins": "custom_endpoint,read_write_splitting,failover", "connect_timeout": 10_000, "autocommit": True, "cluster_id": "cluster1"})
66+
{"connect_timeout": 10_000, "autocommit": True, "cluster_id": "cluster1"})
6767

6868
features = TestEnvironment.get_current().get_features()
6969
if TestEnvironmentFeatures.TELEMETRY_TRACES_ENABLED in features \
@@ -77,6 +77,18 @@ def props(self):
7777

7878
return p
7979

80+
@pytest.fixture(scope='class')
81+
def props_with_failover(self, default_props):
82+
p = default_props.copy()
83+
p["plugins"] = "custom_endpoint,read_write_splitting,failover"
84+
return p
85+
86+
@pytest.fixture(scope='class')
87+
def props(self, default_props):
88+
p = default_props.copy()
89+
p["plugins"] = "custom_endpoint,read_write_splitting"
90+
return p
91+
8092
@pytest.fixture(scope='class', autouse=True)
8193
def setup_and_teardown(self):
8294
env_info = TestEnvironment.get_current().get_info()
@@ -221,13 +233,13 @@ def wait_until_endpoint_has_members(self, rds_client, expected_members: Set[str]
221233
duration_sec = (perf_counter_ns() - start_ns) / 1_000_000_000
222234
self.logger.debug(f"wait_until_endpoint_has_specified_members took {duration_sec} seconds.")
223235

224-
def test_custom_endpoint_failover(self, test_driver: TestDriver, conn_utils, props, rds_utils):
225-
props["failover_mode"] = "reader_or_writer"
236+
def test_custom_endpoint_failover(self, test_driver: TestDriver, conn_utils, props_with_failover, rds_utils):
237+
props_with_failover["failover_mode"] = "reader_or_writer"
226238

227239
target_driver_connect = DriverHelper.get_connect_func(test_driver)
228240
kwargs = conn_utils.get_connect_params()
229241
kwargs["host"] = self.endpoint_info["Endpoint"]
230-
conn = AwsWrapperConnection.connect(target_driver_connect, **kwargs, **props)
242+
conn = AwsWrapperConnection.connect(target_driver_connect, **kwargs, **props_with_failover)
231243

232244
endpoint_members = self.endpoint_info["StaticMembers"]
233245
instance_id = rds_utils.query_instance_id(conn)
@@ -281,7 +293,7 @@ def _setup_custom_endpoint_role(self, target_driver_connect, conn_kwargs, rds_ut
281293
self.logger.debug("Custom endpoint instance successfully set to role: " + host_role.name)
282294

283295
def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__with_reader_as_init_conn(
284-
self, test_driver: TestDriver, conn_utils, props, rds_utils):
296+
self, test_driver: TestDriver, conn_utils, props_with_failover, rds_utils):
285297
'''
286298
Will test for the following scenario:
287299
1. Initially connect to a reader instance via the custom endpoint.
@@ -297,13 +309,13 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
297309
kwargs["host"] = self.endpoint_info["Endpoint"]
298310
# This setting is not required for the test, but it allows us to also test re-creation of expired monitors since
299311
# it takes more than 30 seconds to modify the cluster endpoint (usually around 140s).
300-
props["custom_endpoint_idle_monitor_expiration_ms"] = 30_000
301-
props["wait_for_custom_endpoint_info_timeout_ms"] = 30_000
312+
props_with_failover["custom_endpoint_idle_monitor_expiration_ms"] = 30_000
313+
props_with_failover["wait_for_custom_endpoint_info_timeout_ms"] = 30_000
302314

303315
# Ensure that we are starting with a reader connection
304316
self._setup_custom_endpoint_role(target_driver_connect, kwargs, rds_utils, HostRole.READER)
305317

306-
conn = AwsWrapperConnection.connect(target_driver_connect, **kwargs, **props)
318+
conn = AwsWrapperConnection.connect(target_driver_connect, **kwargs, **props_with_failover)
307319
endpoint_members = self.endpoint_info["StaticMembers"]
308320
original_reader_id = rds_utils.query_instance_id(conn)
309321
assert original_reader_id in endpoint_members
@@ -350,16 +362,16 @@ def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__wit
350362

351363
def test_custom_endpoint_read_write_splitting__with_custom_endpoint_changes__with_writer_as_init_conn(
352364
self, test_driver: TestDriver, conn_utils, props, rds_utils):
353-
'''
365+
"""
354366
Will test for the following scenario:
355-
1. Iniitially connect to the writer instance via the custom endpoint.
367+
1. Initially connect to the writer instance via the custom endpoint.
356368
2. Attempt to switch to reader instance - should succeed, but will still use writer instance as reader.
357369
3. Modify the custom endpoint to add a reader instance as a static member.
358370
4. Switch to reader instance - should succeed.
359371
5. Switch back to writer instance - should succeed.
360372
6. Modify the custom endpoint to remove the reader instance as a static member.
361373
7. Attempt to switch to reader instance - should fail since the custom endpoint no longer has the reader instance.
362-
'''
374+
"""
363375

364376
target_driver_connect = DriverHelper.get_connect_func(test_driver)
365377
kwargs = conn_utils.get_connect_params()

tests/integration/container/test_read_write_splitting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ def test_pooled_connection__cluster_url_failover(
832832
TestEnvironmentFeatures.NETWORK_OUTAGES_ENABLED,
833833
TestEnvironmentFeatures.ABORT_CONNECTION_SUPPORTED])
834834
@disable_on_engines([DatabaseEngine.MYSQL])
835+
@pytest.mark.repeat(10)
835836
def test_pooled_connection__failover_failed(
836837
self,
837838
test_environment: TestEnvironment,

0 commit comments

Comments
 (0)