From 5033fb0384950dc9712dd661c895dd5815755864 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 11 Jul 2025 22:45:57 +0000 Subject: [PATCH 01/24] chore: debug test flake in windows python 3.9 This is for testcase `test_Connector_close_called_multiple_times`. --- google/cloud/alloydbconnector/connector.py | 6 ++++++ tests/unit/test_connector.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/google/cloud/alloydbconnector/connector.py b/google/cloud/alloydbconnector/connector.py index 9a108164..c392e79e 100644 --- a/google/cloud/alloydbconnector/connector.py +++ b/google/cloud/alloydbconnector/connector.py @@ -384,13 +384,19 @@ def close(self) -> None: ) # Will attempt to gracefully shut down tasks for 3s close_future.result(timeout=3) + else: + print("RISHABH DEBUG: loop is not running 1") # if background thread exists for Connector, clean it up if self._thread.is_alive(): if self._loop.is_running(): # stop event loop running in background thread self._loop.call_soon_threadsafe(self._loop.stop) + else: + print("RISHABH DEBUG: loop is not running 2") # wait for thread to finish closing (i.e. loop to stop) self._thread.join() + else: + print("RISHABH DEBUG: thread is not alive") self._closed = True async def close_async(self) -> None: diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 11f6b237..6748fcc5 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -241,6 +241,8 @@ def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> # check that connector thread is no longer running assert connector._thread.is_alive() is False # call connector.close a second time + assert connector._loop.is_running() is False + print("RISHABH DEBUG: before calling close() a second time") connector.close() From e34288258cc5c69d6388797ab20bb55e7a6d8fd2 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 11 Jul 2025 22:50:31 +0000 Subject: [PATCH 02/24] add -s pytest flag for stdout output --- noxfile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/noxfile.py b/noxfile.py index ef368c27..c8a03896 100644 --- a/noxfile.py +++ b/noxfile.py @@ -140,6 +140,7 @@ def default(session, path): "--include=*/google/cloud/alloydbconnector/*.py", "-m", "pytest", + "-s", "-v", path, *session.posargs, From b1ab75d9e1ddac8a064b0a627ed5b2d172abd31c Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 11 Jul 2025 23:11:11 +0000 Subject: [PATCH 03/24] add more logging for cases before close() is called --- google/cloud/alloydbconnector/connector.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/google/cloud/alloydbconnector/connector.py b/google/cloud/alloydbconnector/connector.py index c392e79e..d736e945 100644 --- a/google/cloud/alloydbconnector/connector.py +++ b/google/cloud/alloydbconnector/connector.py @@ -383,6 +383,7 @@ def close(self) -> None: self.close_async(), loop=self._loop ) # Will attempt to gracefully shut down tasks for 3s + print("RISHABH DEBUG: before calling close_async()") close_future.result(timeout=3) else: print("RISHABH DEBUG: loop is not running 1") @@ -390,10 +391,12 @@ def close(self) -> None: if self._thread.is_alive(): if self._loop.is_running(): # stop event loop running in background thread + print("RISHABH DEBUG: before calling call_soon_threadsafe()") self._loop.call_soon_threadsafe(self._loop.stop) else: print("RISHABH DEBUG: loop is not running 2") # wait for thread to finish closing (i.e. loop to stop) + print("RISHABH DEBUG: before calling thread.join()") self._thread.join() else: print("RISHABH DEBUG: thread is not alive") From 5b0f22eb12bc59ae09322db7dd7ec231f8d4b049 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 01:27:39 +0000 Subject: [PATCH 04/24] check if new pytest-asyncio version is causing the flake --- google/cloud/alloydbconnector/connector.py | 9 --------- requirements-test.txt | 5 ++++- tests/unit/test_connector.py | 2 -- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/google/cloud/alloydbconnector/connector.py b/google/cloud/alloydbconnector/connector.py index d736e945..9a108164 100644 --- a/google/cloud/alloydbconnector/connector.py +++ b/google/cloud/alloydbconnector/connector.py @@ -383,23 +383,14 @@ def close(self) -> None: self.close_async(), loop=self._loop ) # Will attempt to gracefully shut down tasks for 3s - print("RISHABH DEBUG: before calling close_async()") close_future.result(timeout=3) - else: - print("RISHABH DEBUG: loop is not running 1") # if background thread exists for Connector, clean it up if self._thread.is_alive(): if self._loop.is_running(): # stop event loop running in background thread - print("RISHABH DEBUG: before calling call_soon_threadsafe()") self._loop.call_soon_threadsafe(self._loop.stop) - else: - print("RISHABH DEBUG: loop is not running 2") # wait for thread to finish closing (i.e. loop to stop) - print("RISHABH DEBUG: before calling thread.join()") self._thread.join() - else: - print("RISHABH DEBUG: thread is not alive") self._closed = True async def close_async(self) -> None: diff --git a/requirements-test.txt b/requirements-test.txt index 7d425d66..4dd3c076 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -11,7 +11,10 @@ psycopg2-binary==2.9.9; python_version == "3.9" and sys_platform == "darwin" psycopg2-binary==2.9.10; python_version != "3.9" or sys_platform != "darwin" pytest==8.4.0 -pytest-asyncio==1.0.0 + +pytest-asyncio==0.26.0; python_version == "3.9" and sys_platform == "win32" +pytest-asyncio==1.0.0; python_version != "3.9" or sys_platform != "win32" + pytest-cov==6.1.1 pytest-aiohttp==1.1.0 SQLAlchemy[asyncio]==2.0.41 diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 6748fcc5..11f6b237 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -241,8 +241,6 @@ def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> # check that connector thread is no longer running assert connector._thread.is_alive() is False # call connector.close a second time - assert connector._loop.is_running() is False - print("RISHABH DEBUG: before calling close() a second time") connector.close() From 59dd94fb419381bbfc22d894de238d0823162b7b Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 03:06:12 +0000 Subject: [PATCH 05/24] check if previous cryptography version does not cause flake --- requirements-test.txt | 5 +---- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/requirements-test.txt b/requirements-test.txt index 19e71bf5..b5113af3 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -11,10 +11,7 @@ psycopg2-binary<2.9.10; python_version == "3.9" and sys_platform == "darwin" psycopg2-binary==2.9.10; python_version != "3.9" or sys_platform != "darwin" pytest==8.4.1 - -pytest-asyncio==0.26.0; python_version == "3.9" and sys_platform == "win32" -pytest-asyncio==1.0.0; python_version != "3.9" or sys_platform != "win32" - +pytest-asyncio==1.0.0 pytest-cov==6.2.1 pytest-aiohttp==1.1.0 SQLAlchemy[asyncio]==2.0.41 diff --git a/requirements.txt b/requirements.txt index a6f4a82e..882874cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ aiofiles==24.1.0 aiohttp==3.12.13 -cryptography==45.0.5 +cryptography==44.0.3 google-auth==2.40.3 requests==2.32.4 protobuf==6.31.1 From 54acc4ad62b64d60b5ef0b8f932bfffa27a94ece Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 20:05:03 +0000 Subject: [PATCH 06/24] Add pytest timeout to add timeout to tests --- pyproject.toml | 3 +++ requirements-test.txt | 1 + requirements.txt | 2 +- tests/unit/test_connector.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b308797f..4d7da302 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,3 +82,6 @@ exclude = ['docs/*'] [tool.pytest.ini_options] asyncio_mode = "auto" +timeout = 5 +log_cli=true +log_level=INFO diff --git a/requirements-test.txt b/requirements-test.txt index b5113af3..2a1af23a 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -16,3 +16,4 @@ pytest-cov==6.2.1 pytest-aiohttp==1.1.0 SQLAlchemy[asyncio]==2.0.41 aioresponses==0.7.8 +pytest-timeout==2.4.0 diff --git a/requirements.txt b/requirements.txt index 882874cb..a6f4a82e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ aiofiles==24.1.0 aiohttp==3.12.13 -cryptography==44.0.3 +cryptography==45.0.5 google-auth==2.40.3 requests==2.32.4 protobuf==6.31.1 diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 11f6b237..fb0d377d 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -38,6 +38,7 @@ def test_Connector_init(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets default attributes. """ + print("RISHABH DEBUG: started test") connector = Connector(credentials) assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" @@ -45,10 +46,12 @@ def test_Connector_init(credentials: FakeCredentials) -> None: assert connector._credentials == credentials assert connector._closed is False connector.close() + print("RISHABH DEBUG: completed test") def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: """Test that Connector errors due to bad ip_type str.""" + print("RISHABH DEBUG: started test") bad_ip_type = "BAD-IP-TYPE" with pytest.raises(ValueError) as exc_info: Connector(ip_type=bad_ip_type, credentials=credentials) @@ -56,6 +59,7 @@ def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) + print("RISHABH DEBUG: completed test") @pytest.mark.parametrize( @@ -106,9 +110,11 @@ def test_Connector_init_ip_type( Test to check whether the __init__ method of Connector properly sets ip_type. """ + print("RISHABH DEBUG: started test") connector = Connector(credentials=credentials, ip_type=ip_type) assert connector._ip_type == expected connector.close() + print("RISHABH DEBUG: completed test") def test_Connector_init_alloydb_api_endpoint_with_http_prefix( @@ -118,11 +124,13 @@ def test_Connector_init_alloydb_api_endpoint_with_http_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'http://' prefix. """ + print("RISHABH DEBUG: started test") connector = Connector( alloydb_api_endpoint="http://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() + print("RISHABH DEBUG: completed test") def test_Connector_init_alloydb_api_endpoint_with_https_prefix( @@ -132,11 +140,13 @@ def test_Connector_init_alloydb_api_endpoint_with_https_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'https://' prefix. """ + print("RISHABH DEBUG: started test") connector = Connector( alloydb_api_endpoint="https://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() + print("RISHABH DEBUG: completed test") def test_Connector_context_manager(credentials: FakeCredentials) -> None: @@ -144,11 +154,13 @@ def test_Connector_context_manager(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets defaults as context manager. """ + print("RISHABH DEBUG: started test") with Connector(credentials) as connector: assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" assert connector._client is None assert connector._credentials == credentials + print("RISHABH DEBUG: completed test") def test_Connector_close(credentials: FakeCredentials) -> None: @@ -156,6 +168,7 @@ def test_Connector_close(credentials: FakeCredentials) -> None: Test that Connector's close method stops event loop and background thread, and sets the connector as closed. """ + print("RISHABH DEBUG: started test") with Connector(credentials) as connector: loop: asyncio.AbstractEventLoop = connector._loop thread: Thread = connector._thread @@ -165,6 +178,7 @@ def test_Connector_close(credentials: FakeCredentials) -> None: assert loop.is_running() is False assert thread.is_alive() is False assert connector._closed is True + print("RISHABH DEBUG: completed test") @pytest.mark.usefixtures("proxy_server") @@ -172,6 +186,7 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - """ Test that connector.connect returns connection object. """ + print("RISHABH DEBUG: started test") client = fake_client with Connector(credentials) as connector: connector._client = client @@ -187,12 +202,14 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - ) # check connection is returned assert connection is True + print("RISHABH DEBUG: completed test") def test_connect_bad_ip_type( credentials: FakeCredentials, fake_client: FakeAlloyDBClient ) -> None: """Test that Connector.connect errors due to bad ip_type str.""" + print("RISHABH DEBUG: started test") with Connector(credentials=credentials) as connector: connector._client = fake_client bad_ip_type = "BAD-IP-TYPE" @@ -209,12 +226,14 @@ def test_connect_bad_ip_type( exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) + print("RISHABH DEBUG: completed test") def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: """ Test that connector.connect errors with unsupported database driver. """ + print("RISHABH DEBUG: started test") client = FakeAlloyDBClient() with Connector(credentials) as connector: connector._client = client @@ -229,10 +248,12 @@ def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Driver 'bad_driver' is not a supported database driver." ) + print("RISHABH DEBUG: completed test") def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> None: """Test that Connector.close can be called multiple times.""" + print("RISHABH DEBUG: started test") # open and close Connector object connector = Connector(credentials=credentials) # verify background thread exists @@ -242,6 +263,7 @@ def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> assert connector._thread.is_alive() is False # call connector.close a second time connector.close() + print("RISHABH DEBUG: completed test") def test_Connector_remove_cached_bad_instance( @@ -252,6 +274,7 @@ def test_Connector_remove_cached_bad_instance( the cache and ensure no background refresh happens (which would be wasted cycles). """ + print("RISHABH DEBUG: started test") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance" with Connector(credentials) as connector: # The timeout of AlloyDB API methods is set to 60s by default. @@ -270,6 +293,7 @@ def test_Connector_remove_cached_bad_instance( with pytest.raises(RetryError): connector.connect(instance_uri, "pg8000") assert instance_uri not in connector._cache + print("RISHABH DEBUG: completed test") async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) -> None: @@ -277,6 +301,7 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) it should delete the instance from the cache and ensure no background refresh happens (which would be wasted cycles). """ + print("RISHABH DEBUG: started test") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance" # set instance to only have Public IP fake_client = FakeAlloyDBClient() @@ -296,6 +321,7 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) await connector.connect_async(instance_uri, "pg8000", ip_type="private") # check that cache has been removed from dict assert instance_uri not in connector._cache + print("RISHABH DEBUG: completed test") @pytest.mark.usefixtures("proxy_server") @@ -306,6 +332,7 @@ def test_Connector_static_connection_info( Test that Connector.__init__() can specify a static connection info to connect to an instance. """ + print("RISHABH DEBUG: started test") static_info = write_static_info(fake_client.instance) with Connector(credentials=credentials, static_conn_info=static_info) as connector: connector._client = fake_client @@ -321,12 +348,14 @@ def test_Connector_static_connection_info( ) # check connection is returned assert connection is True + print("RISHABH DEBUG: completed test") def test_connect_when_closed(credentials: FakeCredentials) -> None: """ Test that connector.connect errors when the connection is closed. """ + print("RISHABH DEBUG: started test") connector = Connector(credentials=credentials) connector.close() with pytest.raises(ClosedConnectorError) as exc_info: @@ -335,3 +364,4 @@ def test_connect_when_closed(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Connection attempt failed because the connector has already been closed." ) + print("RISHABH DEBUG: completed test") From 70ccd0313fbc62a8d9e776353a04b29e0a5e6337 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 20:07:42 +0000 Subject: [PATCH 07/24] fix adding of pytest timeout --- pyproject.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4d7da302..160b6b02 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,5 +83,4 @@ exclude = ['docs/*'] [tool.pytest.ini_options] asyncio_mode = "auto" timeout = 5 -log_cli=true -log_level=INFO +log_cli=true From 110e5548667d771121bf3fd1d2f60cdacdb06df2 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 20:25:55 +0000 Subject: [PATCH 08/24] increase timeout to 30 sec --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 160b6b02..ea624d25 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,5 +82,5 @@ exclude = ['docs/*'] [tool.pytest.ini_options] asyncio_mode = "auto" -timeout = 5 +timeout = 30 log_cli=true From 7ca3d7f0d8a1ca8f24c998b4f3e8fec495003da2 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 21:47:59 +0000 Subject: [PATCH 09/24] set timeout to 2min --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ea624d25..32bec0ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,5 +82,5 @@ exclude = ['docs/*'] [tool.pytest.ini_options] asyncio_mode = "auto" -timeout = 30 +timeout = 120 log_cli=true From fa81e1242d43477c7036af3389dc1149a3ac870f Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 15 Jul 2025 22:06:51 +0000 Subject: [PATCH 10/24] add timestamps to log statements --- tests/unit/test_connector.py | 61 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index fb0d377d..f0cb48b1 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -14,6 +14,7 @@ import asyncio from threading import Thread +import time from typing import Union from google.api_core.exceptions import RetryError @@ -38,7 +39,7 @@ def test_Connector_init(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets default attributes. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials) assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" @@ -46,12 +47,12 @@ def test_Connector_init(credentials: FakeCredentials) -> None: assert connector._credentials == credentials assert connector._closed is False connector.close() - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: """Test that Connector errors due to bad ip_type str.""" - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") bad_ip_type = "BAD-IP-TYPE" with pytest.raises(ValueError) as exc_info: Connector(ip_type=bad_ip_type, credentials=credentials) @@ -59,7 +60,7 @@ def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.parametrize( @@ -110,11 +111,11 @@ def test_Connector_init_ip_type( Test to check whether the __init__ method of Connector properly sets ip_type. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials=credentials, ip_type=ip_type) assert connector._ip_type == expected connector.close() - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_alloydb_api_endpoint_with_http_prefix( @@ -124,13 +125,13 @@ def test_Connector_init_alloydb_api_endpoint_with_http_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'http://' prefix. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector( alloydb_api_endpoint="http://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_alloydb_api_endpoint_with_https_prefix( @@ -140,13 +141,13 @@ def test_Connector_init_alloydb_api_endpoint_with_https_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'https://' prefix. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector( alloydb_api_endpoint="https://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_context_manager(credentials: FakeCredentials) -> None: @@ -154,13 +155,13 @@ def test_Connector_context_manager(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets defaults as context manager. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials) as connector: assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" assert connector._client is None assert connector._credentials == credentials - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_close(credentials: FakeCredentials) -> None: @@ -168,7 +169,7 @@ def test_Connector_close(credentials: FakeCredentials) -> None: Test that Connector's close method stops event loop and background thread, and sets the connector as closed. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials) as connector: loop: asyncio.AbstractEventLoop = connector._loop thread: Thread = connector._thread @@ -178,7 +179,7 @@ def test_Connector_close(credentials: FakeCredentials) -> None: assert loop.is_running() is False assert thread.is_alive() is False assert connector._closed is True - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.usefixtures("proxy_server") @@ -186,7 +187,7 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - """ Test that connector.connect returns connection object. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") client = fake_client with Connector(credentials) as connector: connector._client = client @@ -202,14 +203,14 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - ) # check connection is returned assert connection is True - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_bad_ip_type( credentials: FakeCredentials, fake_client: FakeAlloyDBClient ) -> None: """Test that Connector.connect errors due to bad ip_type str.""" - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials=credentials) as connector: connector._client = fake_client bad_ip_type = "BAD-IP-TYPE" @@ -226,14 +227,14 @@ def test_connect_bad_ip_type( exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: """ Test that connector.connect errors with unsupported database driver. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") client = FakeAlloyDBClient() with Connector(credentials) as connector: connector._client = client @@ -248,12 +249,12 @@ def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Driver 'bad_driver' is not a supported database driver." ) - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> None: """Test that Connector.close can be called multiple times.""" - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") # open and close Connector object connector = Connector(credentials=credentials) # verify background thread exists @@ -263,7 +264,7 @@ def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> assert connector._thread.is_alive() is False # call connector.close a second time connector.close() - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_remove_cached_bad_instance( @@ -274,7 +275,7 @@ def test_Connector_remove_cached_bad_instance( the cache and ensure no background refresh happens (which would be wasted cycles). """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance" with Connector(credentials) as connector: # The timeout of AlloyDB API methods is set to 60s by default. @@ -293,7 +294,7 @@ def test_Connector_remove_cached_bad_instance( with pytest.raises(RetryError): connector.connect(instance_uri, "pg8000") assert instance_uri not in connector._cache - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) -> None: @@ -301,7 +302,7 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) it should delete the instance from the cache and ensure no background refresh happens (which would be wasted cycles). """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance" # set instance to only have Public IP fake_client = FakeAlloyDBClient() @@ -321,7 +322,7 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) await connector.connect_async(instance_uri, "pg8000", ip_type="private") # check that cache has been removed from dict assert instance_uri not in connector._cache - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.usefixtures("proxy_server") @@ -332,7 +333,7 @@ def test_Connector_static_connection_info( Test that Connector.__init__() can specify a static connection info to connect to an instance. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") static_info = write_static_info(fake_client.instance) with Connector(credentials=credentials, static_conn_info=static_info) as connector: connector._client = fake_client @@ -348,14 +349,14 @@ def test_Connector_static_connection_info( ) # check connection is returned assert connection is True - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_when_closed(credentials: FakeCredentials) -> None: """ Test that connector.connect errors when the connection is closed. """ - print("RISHABH DEBUG: started test") + print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials=credentials) connector.close() with pytest.raises(ClosedConnectorError) as exc_info: @@ -364,4 +365,4 @@ def test_connect_when_closed(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Connection attempt failed because the connector has already been closed." ) - print("RISHABH DEBUG: completed test") + print(f"RISHABH DEBUG: completed test: {time.time()}") From b700bf8ee632f468c7c447bb6799bd338539b669 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 20:23:28 +0000 Subject: [PATCH 11/24] remove pytest-timeout --- pyproject.toml | 2 -- requirements-test.txt | 1 - 2 files changed, 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 32bec0ea..b308797f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,5 +82,3 @@ exclude = ['docs/*'] [tool.pytest.ini_options] asyncio_mode = "auto" -timeout = 120 -log_cli=true diff --git a/requirements-test.txt b/requirements-test.txt index 2a1af23a..b5113af3 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -16,4 +16,3 @@ pytest-cov==6.2.1 pytest-aiohttp==1.1.0 SQLAlchemy[asyncio]==2.0.41 aioresponses==0.7.8 -pytest-timeout==2.4.0 From 369fc6a6e63766564e7bde48738b865223518b99 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 20:32:36 +0000 Subject: [PATCH 12/24] remove print statements in test_Connector_close_called_multiple_times --- tests/unit/test_connector.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index f0cb48b1..9cfff9c3 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -254,7 +254,6 @@ def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> None: """Test that Connector.close can be called multiple times.""" - print(f"RISHABH DEBUG: started test: {time.time()}") # open and close Connector object connector = Connector(credentials=credentials) # verify background thread exists @@ -264,7 +263,6 @@ def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> assert connector._thread.is_alive() is False # call connector.close a second time connector.close() - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_remove_cached_bad_instance( From ef6c848feb400fdb100ece8953e43643af18ca6f Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 20:48:27 +0000 Subject: [PATCH 13/24] delete all print statements --- tests/unit/test_connector.py | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 9cfff9c3..7badb3eb 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -39,7 +39,6 @@ def test_Connector_init(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets default attributes. """ - print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials) assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" @@ -47,12 +46,10 @@ def test_Connector_init(credentials: FakeCredentials) -> None: assert connector._credentials == credentials assert connector._closed is False connector.close() - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: """Test that Connector errors due to bad ip_type str.""" - print(f"RISHABH DEBUG: started test: {time.time()}") bad_ip_type = "BAD-IP-TYPE" with pytest.raises(ValueError) as exc_info: Connector(ip_type=bad_ip_type, credentials=credentials) @@ -60,7 +57,6 @@ def test_Connector_init_bad_ip_type(credentials: FakeCredentials) -> None: exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) - print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.parametrize( @@ -111,11 +107,9 @@ def test_Connector_init_ip_type( Test to check whether the __init__ method of Connector properly sets ip_type. """ - print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials=credentials, ip_type=ip_type) assert connector._ip_type == expected connector.close() - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_alloydb_api_endpoint_with_http_prefix( @@ -125,13 +119,11 @@ def test_Connector_init_alloydb_api_endpoint_with_http_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'http://' prefix. """ - print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector( alloydb_api_endpoint="http://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_init_alloydb_api_endpoint_with_https_prefix( @@ -141,13 +133,11 @@ def test_Connector_init_alloydb_api_endpoint_with_https_prefix( Test to check whether the __init__ method of Connector properly sets alloydb_api_endpoint when its URL has an 'https://' prefix. """ - print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector( alloydb_api_endpoint="https://alloydb.googleapis.com", credentials=credentials ) assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" connector.close() - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_context_manager(credentials: FakeCredentials) -> None: @@ -155,13 +145,11 @@ def test_Connector_context_manager(credentials: FakeCredentials) -> None: Test to check whether the __init__ method of Connector properly sets defaults as context manager. """ - print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials) as connector: assert connector._quota_project is None assert connector._alloydb_api_endpoint == "alloydb.googleapis.com" assert connector._client is None assert connector._credentials == credentials - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_close(credentials: FakeCredentials) -> None: @@ -169,7 +157,6 @@ def test_Connector_close(credentials: FakeCredentials) -> None: Test that Connector's close method stops event loop and background thread, and sets the connector as closed. """ - print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials) as connector: loop: asyncio.AbstractEventLoop = connector._loop thread: Thread = connector._thread @@ -179,7 +166,6 @@ def test_Connector_close(credentials: FakeCredentials) -> None: assert loop.is_running() is False assert thread.is_alive() is False assert connector._closed is True - print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.usefixtures("proxy_server") @@ -187,7 +173,6 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - """ Test that connector.connect returns connection object. """ - print(f"RISHABH DEBUG: started test: {time.time()}") client = fake_client with Connector(credentials) as connector: connector._client = client @@ -203,14 +188,12 @@ def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) - ) # check connection is returned assert connection is True - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_bad_ip_type( credentials: FakeCredentials, fake_client: FakeAlloyDBClient ) -> None: """Test that Connector.connect errors due to bad ip_type str.""" - print(f"RISHABH DEBUG: started test: {time.time()}") with Connector(credentials=credentials) as connector: connector._client = fake_client bad_ip_type = "BAD-IP-TYPE" @@ -227,14 +210,12 @@ def test_connect_bad_ip_type( exc_info.value.args[0] == f"Incorrect value for ip_type, got '{bad_ip_type}'. Want one of: 'PUBLIC', 'PRIVATE', 'PSC'." ) - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: """ Test that connector.connect errors with unsupported database driver. """ - print(f"RISHABH DEBUG: started test: {time.time()}") client = FakeAlloyDBClient() with Connector(credentials) as connector: connector._client = client @@ -249,7 +230,6 @@ def test_connect_unsupported_driver(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Driver 'bad_driver' is not a supported database driver." ) - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_Connector_close_called_multiple_times(credentials: FakeCredentials) -> None: @@ -273,7 +253,6 @@ def test_Connector_remove_cached_bad_instance( the cache and ensure no background refresh happens (which would be wasted cycles). """ - print(f"RISHABH DEBUG: started test: {time.time()}") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance" with Connector(credentials) as connector: # The timeout of AlloyDB API methods is set to 60s by default. @@ -292,7 +271,6 @@ def test_Connector_remove_cached_bad_instance( with pytest.raises(RetryError): connector.connect(instance_uri, "pg8000") assert instance_uri not in connector._cache - print(f"RISHABH DEBUG: completed test: {time.time()}") async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) -> None: @@ -300,7 +278,6 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) it should delete the instance from the cache and ensure no background refresh happens (which would be wasted cycles). """ - print(f"RISHABH DEBUG: started test: {time.time()}") instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance" # set instance to only have Public IP fake_client = FakeAlloyDBClient() @@ -320,7 +297,6 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials) await connector.connect_async(instance_uri, "pg8000", ip_type="private") # check that cache has been removed from dict assert instance_uri not in connector._cache - print(f"RISHABH DEBUG: completed test: {time.time()}") @pytest.mark.usefixtures("proxy_server") @@ -331,7 +307,6 @@ def test_Connector_static_connection_info( Test that Connector.__init__() can specify a static connection info to connect to an instance. """ - print(f"RISHABH DEBUG: started test: {time.time()}") static_info = write_static_info(fake_client.instance) with Connector(credentials=credentials, static_conn_info=static_info) as connector: connector._client = fake_client @@ -347,14 +322,12 @@ def test_Connector_static_connection_info( ) # check connection is returned assert connection is True - print(f"RISHABH DEBUG: completed test: {time.time()}") def test_connect_when_closed(credentials: FakeCredentials) -> None: """ Test that connector.connect errors when the connection is closed. """ - print(f"RISHABH DEBUG: started test: {time.time()}") connector = Connector(credentials=credentials) connector.close() with pytest.raises(ClosedConnectorError) as exc_info: @@ -363,4 +336,3 @@ def test_connect_when_closed(credentials: FakeCredentials) -> None: exc_info.value.args[0] == "Connection attempt failed because the connector has already been closed." ) - print(f"RISHABH DEBUG: completed test: {time.time()}") From bd62f170234aab54e7a23b1d56c41cdcf22bcb88 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 21:17:16 +0000 Subject: [PATCH 14/24] comment out test_connect --- tests/unit/test_connector.py | 40 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 7badb3eb..014cc3b6 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -168,26 +168,26 @@ def test_Connector_close(credentials: FakeCredentials) -> None: assert connector._closed is True -@pytest.mark.usefixtures("proxy_server") -def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) -> None: - """ - Test that connector.connect returns connection object. - """ - client = fake_client - with Connector(credentials) as connector: - connector._client = client - # patch db connection creation - with patch("google.cloud.alloydbconnector.pg8000.connect") as mock_connect: - mock_connect.return_value = True - connection = connector.connect( - "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance", - "pg8000", - user="test-user", - password="test-password", - db="test-db", - ) - # check connection is returned - assert connection is True +# @pytest.mark.usefixtures("proxy_server") +# def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) -> None: +# """ +# Test that connector.connect returns connection object. +# """ +# client = fake_client +# with Connector(credentials) as connector: +# connector._client = client +# # patch db connection creation +# with patch("google.cloud.alloydbconnector.pg8000.connect") as mock_connect: +# mock_connect.return_value = True +# connection = connector.connect( +# "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance", +# "pg8000", +# user="test-user", +# password="test-password", +# db="test-db", +# ) +# # check connection is returned +# assert connection is True def test_connect_bad_ip_type( From 42fe6dc159f0384922337452e0ca0caec82ea160 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 22:54:53 +0000 Subject: [PATCH 15/24] change proxy_server to not run in test session scope --- tests/unit/conftest.py | 2 +- tests/unit/test_connector.py | 40 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index cca09a06..7b4fd870 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -75,7 +75,7 @@ async def start_proxy_server(instance: FakeInstance) -> None: conn.close() -@pytest.fixture(scope="session") +@pytest.fixture def proxy_server(fake_instance: FakeInstance) -> None: """Run local proxy server capable of performing metadata exchange""" thread = Thread( diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 014cc3b6..7badb3eb 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -168,26 +168,26 @@ def test_Connector_close(credentials: FakeCredentials) -> None: assert connector._closed is True -# @pytest.mark.usefixtures("proxy_server") -# def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) -> None: -# """ -# Test that connector.connect returns connection object. -# """ -# client = fake_client -# with Connector(credentials) as connector: -# connector._client = client -# # patch db connection creation -# with patch("google.cloud.alloydbconnector.pg8000.connect") as mock_connect: -# mock_connect.return_value = True -# connection = connector.connect( -# "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance", -# "pg8000", -# user="test-user", -# password="test-password", -# db="test-db", -# ) -# # check connection is returned -# assert connection is True +@pytest.mark.usefixtures("proxy_server") +def test_connect(credentials: FakeCredentials, fake_client: FakeAlloyDBClient) -> None: + """ + Test that connector.connect returns connection object. + """ + client = fake_client + with Connector(credentials) as connector: + connector._client = client + # patch db connection creation + with patch("google.cloud.alloydbconnector.pg8000.connect") as mock_connect: + mock_connect.return_value = True + connection = connector.connect( + "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance", + "pg8000", + user="test-user", + password="test-password", + db="test-db", + ) + # check connection is returned + assert connection is True def test_connect_bad_ip_type( From 8ecbd777f822a98a62bb3b3439e7195936e8ab06 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 23:17:09 +0000 Subject: [PATCH 16/24] revert this change: https://github.com/GoogleCloudPlatform/alloydb-python-connector/pull/406/files#r1946932831 --- tests/unit/conftest.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 7b4fd870..02c6589a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -16,6 +16,7 @@ import socket import ssl from threading import Thread +from typing import Generator from aiofiles.tempfile import TemporaryDirectory from mocks import FakeAlloyDBClient @@ -26,8 +27,6 @@ from google.cloud.alloydbconnector.utils import _write_to_file -DELAY = 1.0 - @pytest.fixture def credentials() -> FakeCredentials: @@ -67,8 +66,8 @@ async def start_proxy_server(instance: FakeInstance) -> None: # listen for incoming connections sock.listen(5) - with context.wrap_socket(sock, server_side=True) as ssock: - while True: + while True: + with context.wrap_socket(sock, server_side=True) as ssock: conn, _ = ssock.accept() metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) @@ -76,7 +75,7 @@ async def start_proxy_server(instance: FakeInstance) -> None: @pytest.fixture -def proxy_server(fake_instance: FakeInstance) -> None: +def proxy_server(fake_instance: FakeInstance) -> Generator: """Run local proxy server capable of performing metadata exchange""" thread = Thread( target=asyncio.run, @@ -88,4 +87,5 @@ def proxy_server(fake_instance: FakeInstance) -> None: daemon=True, ) thread.start() - thread.join(DELAY) # add a delay to allow the proxy server to start + yield thread + thread.join() From deae4df5a89aa3939eac66aa7f907d4c0a50eab1 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Fri, 18 Jul 2025 23:46:42 +0000 Subject: [PATCH 17/24] Revert "revert this change: https://github.com/GoogleCloudPlatform/alloydb-python-connector/pull/406/files#r1946932831" This reverts commit 8ecbd777f822a98a62bb3b3439e7195936e8ab06. --- tests/unit/conftest.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 02c6589a..7b4fd870 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -16,7 +16,6 @@ import socket import ssl from threading import Thread -from typing import Generator from aiofiles.tempfile import TemporaryDirectory from mocks import FakeAlloyDBClient @@ -27,6 +26,8 @@ from google.cloud.alloydbconnector.utils import _write_to_file +DELAY = 1.0 + @pytest.fixture def credentials() -> FakeCredentials: @@ -66,8 +67,8 @@ async def start_proxy_server(instance: FakeInstance) -> None: # listen for incoming connections sock.listen(5) - while True: - with context.wrap_socket(sock, server_side=True) as ssock: + with context.wrap_socket(sock, server_side=True) as ssock: + while True: conn, _ = ssock.accept() metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) @@ -75,7 +76,7 @@ async def start_proxy_server(instance: FakeInstance) -> None: @pytest.fixture -def proxy_server(fake_instance: FakeInstance) -> Generator: +def proxy_server(fake_instance: FakeInstance) -> None: """Run local proxy server capable of performing metadata exchange""" thread = Thread( target=asyncio.run, @@ -87,5 +88,4 @@ def proxy_server(fake_instance: FakeInstance) -> Generator: daemon=True, ) thread.start() - yield thread - thread.join() + thread.join(DELAY) # add a delay to allow the proxy server to start From 37213a684634e6fc2469022b054417b3c28dd4d4 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Sat, 19 Jul 2025 00:13:03 +0000 Subject: [PATCH 18/24] remove loop in start_proxy_server and have pytest return a generator --- tests/unit/conftest.py | 17 ++++++++--------- tests/unit/test_connector.py | 1 - 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 7b4fd870..bc2eb519 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -16,6 +16,7 @@ import socket import ssl from threading import Thread +from typing import Generator from aiofiles.tempfile import TemporaryDirectory from mocks import FakeAlloyDBClient @@ -26,8 +27,6 @@ from google.cloud.alloydbconnector.utils import _write_to_file -DELAY = 1.0 - @pytest.fixture def credentials() -> FakeCredentials: @@ -68,15 +67,14 @@ async def start_proxy_server(instance: FakeInstance) -> None: sock.listen(5) with context.wrap_socket(sock, server_side=True) as ssock: - while True: - conn, _ = ssock.accept() - metadata_exchange(conn) - conn.sendall(instance.name.encode("utf-8")) - conn.close() + conn, _ = ssock.accept() + metadata_exchange(conn) + conn.sendall(instance.name.encode("utf-8")) + conn.close() @pytest.fixture -def proxy_server(fake_instance: FakeInstance) -> None: +def proxy_server(fake_instance: FakeInstance) -> Generator: """Run local proxy server capable of performing metadata exchange""" thread = Thread( target=asyncio.run, @@ -88,4 +86,5 @@ def proxy_server(fake_instance: FakeInstance) -> None: daemon=True, ) thread.start() - thread.join(DELAY) # add a delay to allow the proxy server to start + yield thread + thread.join() diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 7badb3eb..11f6b237 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -14,7 +14,6 @@ import asyncio from threading import Thread -import time from typing import Union from google.api_core.exceptions import RetryError From 7c55851a25f531521ab4923b99483429f64c7bf8 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Sat, 19 Jul 2025 00:21:41 +0000 Subject: [PATCH 19/24] don't return generator in pytest --- tests/unit/conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index bc2eb519..dcd7c54d 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -16,7 +16,6 @@ import socket import ssl from threading import Thread -from typing import Generator from aiofiles.tempfile import TemporaryDirectory from mocks import FakeAlloyDBClient @@ -27,6 +26,8 @@ from google.cloud.alloydbconnector.utils import _write_to_file +DELAY = 1.0 + @pytest.fixture def credentials() -> FakeCredentials: @@ -74,7 +75,7 @@ async def start_proxy_server(instance: FakeInstance) -> None: @pytest.fixture -def proxy_server(fake_instance: FakeInstance) -> Generator: +def proxy_server(fake_instance: FakeInstance) -> None: """Run local proxy server capable of performing metadata exchange""" thread = Thread( target=asyncio.run, @@ -86,5 +87,4 @@ def proxy_server(fake_instance: FakeInstance) -> Generator: daemon=True, ) thread.start() - yield thread - thread.join() + thread.join(DELAY) From 7acc0aae41ad382e5fa638403e6abdabc1df3a27 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Sat, 19 Jul 2025 00:46:20 +0000 Subject: [PATCH 20/24] Put listen() inside loop --- tests/unit/conftest.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index dcd7c54d..6dec46a7 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -64,17 +64,18 @@ async def start_proxy_server(instance: FakeInstance) -> None: context.load_cert_chain(cert_chain_filename, key_filename) # bind socket to AlloyDB proxy server port on localhost sock.bind((ip_address, port)) - # listen for incoming connections - sock.listen(5) with context.wrap_socket(sock, server_side=True) as ssock: - conn, _ = ssock.accept() - metadata_exchange(conn) - conn.sendall(instance.name.encode("utf-8")) - conn.close() + while True: + # listen for incoming connections + ssock.listen(5) + conn, _ = ssock.accept() + metadata_exchange(conn) + conn.sendall(instance.name.encode("utf-8")) + conn.close() -@pytest.fixture +@pytest.fixture(scope="session") def proxy_server(fake_instance: FakeInstance) -> None: """Run local proxy server capable of performing metadata exchange""" thread = Thread( @@ -87,4 +88,4 @@ def proxy_server(fake_instance: FakeInstance) -> None: daemon=True, ) thread.start() - thread.join(DELAY) + thread.join(DELAY) # add a delay to allow the proxy server to start From 92d040bea184ecd6e3f23d47a3c1c7ca52815a72 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Sat, 19 Jul 2025 01:00:47 +0000 Subject: [PATCH 21/24] Put in server socket --- tests/unit/conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 6dec46a7..287e50d9 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -62,13 +62,13 @@ async def start_proxy_server(instance: FakeInstance) -> None: tmpdir, server, [server, root], instance.server_key ) context.load_cert_chain(cert_chain_filename, key_filename) - # bind socket to AlloyDB proxy server port on localhost - sock.bind((ip_address, port)) with context.wrap_socket(sock, server_side=True) as ssock: + # bind socket to AlloyDB proxy server port on localhost + ssock.bind((ip_address, port)) + # listen for incoming connections + ssock.listen(5) while True: - # listen for incoming connections - ssock.listen(5) conn, _ = ssock.accept() metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) From e8ad387fcf572ec619e8a94d845cf49bdc00596f Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Sat, 19 Jul 2025 01:14:53 +0000 Subject: [PATCH 22/24] yield thread --- tests/unit/conftest.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 287e50d9..8691d614 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -16,6 +16,7 @@ import socket import ssl from threading import Thread +from typing import Generator from aiofiles.tempfile import TemporaryDirectory from mocks import FakeAlloyDBClient @@ -62,13 +63,13 @@ async def start_proxy_server(instance: FakeInstance) -> None: tmpdir, server, [server, root], instance.server_key ) context.load_cert_chain(cert_chain_filename, key_filename) + # bind socket to AlloyDB proxy server port on localhost + sock.bind((ip_address, port)) with context.wrap_socket(sock, server_side=True) as ssock: - # bind socket to AlloyDB proxy server port on localhost - ssock.bind((ip_address, port)) - # listen for incoming connections - ssock.listen(5) while True: + # listen for incoming connections + ssock.listen(5) conn, _ = ssock.accept() metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) @@ -76,7 +77,7 @@ async def start_proxy_server(instance: FakeInstance) -> None: @pytest.fixture(scope="session") -def proxy_server(fake_instance: FakeInstance) -> None: +def proxy_server(fake_instance: FakeInstance) -> Generator: """Run local proxy server capable of performing metadata exchange""" thread = Thread( target=asyncio.run, @@ -88,4 +89,4 @@ def proxy_server(fake_instance: FakeInstance) -> None: daemon=True, ) thread.start() - thread.join(DELAY) # add a delay to allow the proxy server to start + yield thread From bc67f67e07369ef4f9ccb5351a3faa53876d56b4 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 22 Jul 2025 00:20:29 +0000 Subject: [PATCH 23/24] add logs in proxy_server function --- tests/unit/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 8691d614..a6f7a05b 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -69,11 +69,13 @@ async def start_proxy_server(instance: FakeInstance) -> None: with context.wrap_socket(sock, server_side=True) as ssock: while True: # listen for incoming connections + print(f"RISHABH DEBUG: listening for connection") ssock.listen(5) conn, _ = ssock.accept() metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) conn.close() + print(f"RISHABH DEBUG: finished processing a connection") @pytest.fixture(scope="session") From 973b5676a964f67fc3072a7fe8e36a840e45e160 Mon Sep 17 00:00:00 2001 From: rhatgadkar-goog Date: Tue, 22 Jul 2025 17:03:14 +0000 Subject: [PATCH 24/24] add more debug logs in start_proxy_server --- tests/unit/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index a6f7a05b..b97b4647 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -71,7 +71,9 @@ async def start_proxy_server(instance: FakeInstance) -> None: # listen for incoming connections print(f"RISHABH DEBUG: listening for connection") ssock.listen(5) + print(f"RISHABH DEBUG: accepting a connection") conn, _ = ssock.accept() + print(f"RISHABH DEBUG: doing metadata exchange") metadata_exchange(conn) conn.sendall(instance.name.encode("utf-8")) conn.close()