@@ -4972,129 +4972,6 @@ def test_pooled_connections_have_independent_encoding_settings(conn_str, reset_p
49724972 conn3 .close ()
49734973
49744974
4975- def test_encoding_settings_persist_across_pool_reuse (conn_str , reset_pooling_state ):
4976- """Test that encoding settings behavior when connection is reused from pool."""
4977- from mssql_python import pooling
4978-
4979- # Enable pooling with max_size=1 to force reuse
4980- pooling (max_size = 1 , idle_timeout = 30 )
4981-
4982- # First connection: set custom encoding
4983- conn1 = mssql_python .connect (conn_str )
4984- cursor1 = conn1 .cursor ()
4985- cursor1 .execute ("SELECT @@SPID" )
4986- spid1 = cursor1 .fetchone ()[0 ]
4987-
4988- conn1 .setencoding (encoding = "utf-16le" , ctype = mssql_python .SQL_WCHAR )
4989- conn1 .setdecoding (mssql_python .SQL_CHAR , encoding = "latin-1" )
4990-
4991- enc1 = conn1 .getencoding ()
4992- dec1 = conn1 .getdecoding (mssql_python .SQL_CHAR )
4993-
4994- assert enc1 ["encoding" ] == "utf-16le"
4995- assert dec1 ["encoding" ] == "latin-1"
4996-
4997- conn1 .close ()
4998-
4999- # Second connection: should get same SPID (pool reuse)
5000- conn2 = mssql_python .connect (conn_str )
5001- cursor2 = conn2 .cursor ()
5002- cursor2 .execute ("SELECT @@SPID" )
5003- spid2 = cursor2 .fetchone ()[0 ]
5004-
5005- # Should reuse same SPID (pool reuse)
5006- assert spid1 == spid2
5007-
5008- # Check if settings persist or reset
5009- enc2 = conn2 .getencoding ()
5010- # Encoding may persist or reset depending on implementation
5011- assert enc2 ["encoding" ] in ["utf-16le" , "utf-8" ]
5012-
5013- conn2 .close ()
5014-
5015-
5016- @timeout_test (60 ) # 60-second timeout for pooling test
5017- def test_connection_pool_with_threadpool_executor (conn_str , reset_pooling_state ):
5018- """Test connection pooling with ThreadPoolExecutor for realistic concurrent workload."""
5019- from mssql_python import pooling
5020- import concurrent .futures
5021- import sys
5022-
5023- # Enable pooling
5024- pooling (max_size = 10 , idle_timeout = 30 )
5025-
5026- # Platform-adjusted settings to prevent hangs
5027- max_workers = 8 if sys .platform .startswith (("linux" , "darwin" )) else 15
5028- num_tasks = 30 if sys .platform .startswith (("linux" , "darwin" )) else 50
5029-
5030- def execute_query_with_encoding (task_id ):
5031- """Execute a query with specific encoding."""
5032- conn = None
5033- cursor = None
5034- try :
5035- conn = mssql_python .connect (conn_str )
5036-
5037- # Set encoding based on task_id
5038- encoding = "utf-16le" if task_id % 2 == 0 else "utf-16be"
5039- conn .setencoding (encoding = encoding , ctype = mssql_python .SQL_WCHAR )
5040- conn .setdecoding (mssql_python .SQL_WCHAR , encoding = encoding )
5041-
5042- # Execute query
5043- cursor = conn .cursor ()
5044- cursor .execute ("SELECT CAST(N'Result' AS NVARCHAR(50))" )
5045- result = cursor .fetchone ()
5046-
5047- # Verify encoding is still correct
5048- enc = conn .getencoding ()
5049- assert enc ["encoding" ] == encoding
5050-
5051- return {
5052- "task_id" : task_id ,
5053- "encoding" : encoding ,
5054- "result" : result [0 ] if result else None ,
5055- "success" : True ,
5056- }
5057- except Exception as e :
5058- return {
5059- "task_id" : task_id ,
5060- "encoding" : "unknown" ,
5061- "result" : None ,
5062- "success" : False ,
5063- "error" : str (e ),
5064- }
5065- finally :
5066- try :
5067- if cursor :
5068- cursor .close ()
5069- except :
5070- pass
5071- try :
5072- if conn :
5073- conn .close ()
5074- except :
5075- pass
5076-
5077- # Use ThreadPoolExecutor with timeout protection
5078- with concurrent .futures .ThreadPoolExecutor (max_workers = max_workers ) as executor :
5079- futures = [executor .submit (execute_query_with_encoding , i ) for i in range (num_tasks )]
5080-
5081- # Collect results with timeout
5082- results = []
5083- for future in concurrent .futures .as_completed (futures , timeout = 50 ):
5084- try :
5085- result = future .result (timeout = 5 ) # 5 second timeout per task
5086- results .append (result )
5087- except concurrent .futures .TimeoutError :
5088- results .append ({"task_id" : - 1 , "success" : False , "error" : "Task timeout" })
5089- except Exception as e :
5090- results .append ({"task_id" : - 1 , "success" : False , "error" : str (e )})
5091-
5092- # Verify we got most results (allow some failures on slower platforms)
5093- success_count = sum (1 for r in results if r .get ("success" , False ))
5094- assert len (results ) >= num_tasks * 0.8 , f"Too few results: { len (results )} /{ num_tasks } "
5095- assert success_count >= num_tasks * 0.7 , f"Too few successful: { success_count } /{ num_tasks } "
5096-
5097-
50984975def test_pooling_disabled_encoding_still_works (conn_str , reset_pooling_state ):
50994976 """Test that encoding/decoding works correctly when pooling is disabled."""
51004977 from mssql_python import pooling
0 commit comments