Skip to content

Commit be61e9a

Browse files
authored
FEAT: adding tests for connection pooling speed and basic functionality (#103)
This pull request introduces new functionality to test connection pooling in the `mssql_python` library, along with some minor updates to imports in the test file. The most significant changes include adding tests for connection pooling speed and basic functionality, as well as updating the import statement to include the `pooling` module. ### Connection pooling tests: * [`tests/test_003_connection.py`](diffhunk://#diff-ca315ffd463e0f93f3d45ace0869a4a3b1e572941da6723d7e9ce028cf9d2278R182-R225): Added `test_connection_pooling_speed` to measure the performance improvement when using connection pooling compared to no pooling. Includes assertions to validate that pooled connections are faster. * [`tests/test_003_connection.py`](diffhunk://#diff-ca315ffd463e0f93f3d45ace0869a4a3b1e572941da6723d7e9ce028cf9d2278R182-R225): Added `test_connection_pooling_basic` to verify the functionality of connection pooling with a small pool size, ensuring connections are reused and respecting the pool size limit. Includes assertions and exception handling for edge cases. ### Import updates: * [`tests/test_003_connection.py`](diffhunk://#diff-ca315ffd463e0f93f3d45ace0869a4a3b1e572941da6723d7e9ce028cf9d2278L14-R15): Updated import statement to include the `pooling` module, which is required for the new connection pooling tests.
1 parent 918b328 commit be61e9a

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

tests/test_003_connection.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"""
1212

1313
import pytest
14-
from mssql_python import Connection, connect
14+
import time
15+
from mssql_python import Connection, connect, pooling
1516

1617
def drop_table_if_exists(cursor, table_name):
1718
"""Drop the table if it exists"""
@@ -177,5 +178,49 @@ def test_connection_close(conn_str):
177178
# Create a separate connection just for this test
178179
temp_conn = connect(conn_str)
179180
# Check if the database connection can be closed
180-
temp_conn.close()
181+
temp_conn.close()
182+
183+
def test_connection_pooling_speed(conn_str):
184+
# No pooling
185+
start_no_pool = time.perf_counter()
186+
conn1 = connect(conn_str)
187+
conn1.close()
188+
end_no_pool = time.perf_counter()
189+
no_pool_duration = end_no_pool - start_no_pool
190+
191+
# Second connection
192+
start2 = time.perf_counter()
193+
conn2 = connect(conn_str)
194+
conn2.close()
195+
end2 = time.perf_counter()
196+
duration2 = end2 - start2
197+
198+
# Pooling enabled
199+
pooling(max_size=2, idle_timeout=10)
200+
connect(conn_str).close()
201+
202+
# Pooled connection (should be reused, hence faster)
203+
start_pool = time.perf_counter()
204+
conn2 = connect(conn_str)
205+
conn2.close()
206+
end_pool = time.perf_counter()
207+
pool_duration = end_pool - start_pool
208+
assert pool_duration < no_pool_duration, "Expected faster connection with pooling"
209+
210+
def test_connection_pooling_basic(conn_str):
211+
# Enable pooling with small pool size
212+
pooling(max_size=2, idle_timeout=5)
213+
conn1 = connect(conn_str)
214+
conn2 = connect(conn_str)
215+
assert conn1 is not None
216+
assert conn2 is not None
217+
try:
218+
conn3 = connect(conn_str)
219+
assert conn3 is not None, "Third connection failed — pooling is not working or limit is too strict"
220+
conn3.close()
221+
except Exception as e:
222+
print(f"Expected: Could not open third connection due to max_size=2: {e}")
223+
224+
conn1.close()
225+
conn2.close()
181226

0 commit comments

Comments
 (0)