Skip to content

test(mssql): isolate test tables and fix resource leaks in integration tests - #4499

Open
lusosa-googlexwf wants to merge 12 commits into
mainfrom
fix/mssql-test-isolation2
Open

lusosa-googlexwf wants to merge 12 commits into
mainfrom
fix/mssql-test-isolation2

Conversation

@lusosa-googlexwf

@lusosa-googlexwf lusosa-googlexwf commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Refactors MSSQL integration tests and shared test utilities to ensure complete isolation and prevent resource leaks during concurrent CI runs or on shared test databases.

Key Changes

  • MSSQL Table Cleanup: Avoid unconditional global table drops (sp_MSforeachtable). Only drop tables scoped to unique test IDs, or prune orphaned test tables older than 2 hours using server local time (GETDATE()).
  • Escaped DDL Identifiers: Safely escape table names with brackets and doubled brackets (]]) to prevent SQL syntax errors or injection.
  • Dynamic Table Names: Replace static table name t in RunExecuteSqlToolInvokeTest with unique UUID-based tables and register t.Cleanup hooks.
  • Connection & Resource Safety:
    • Switch from QueryContext to ExecContext in SetupMsSQLTable and trigger teardown on insert failure without shadowing errors.
    • Properly handle rows.Scan errors and check rows.Err() across result set iterations.
    • Ensure SQL Server connection pools and server commands (cmd.Close()) are cleaned up.

Tests

  • Tested locally with go test ./tests/mssql/...

PR Checklist

  • Make sure you reviewed
    CONTRIBUTING.md
  • Make sure to open an issue as a
    bug/issue
    before writing your code! That way we can discuss the change, evaluate
    designs, and agree on the general idea
  • Ensure you have manually reviewed the entire diff before requesting a
    review
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)
  • Make sure to add ! if this involve a breaking change

@lusosa-googlexwf
lusosa-googlexwf requested a review from a team as a code owner November 13, 2026 23:11

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves the robustness and concurrency of MSSQL integration tests. Key changes include transitioning to t.Cleanup for resource teardown, using ExecContext instead of QueryContext for DDL/DML statements, introducing dynamic table names for SQL execution tests to prevent conflicts, and refining CleanupMSSQLTables to target specific unique IDs or orphaned tables older than 2 hours. The review feedback highlights two important improvements: escaping the underscore wildcard in SQL LIKE patterns (e.g., t[_]% instead of t_%) to prevent matching unintended tables, and simplifying the string replacement logic for SQL table names to make it more robust.

Comment thread tests/common.go Resolved
Comment thread tests/tool.go Resolved
@lusosa-googlexwf

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves the robustness and concurrency safety of MSSQL integration tests. Key changes include refactoring CleanupMSSQLTables to target specific test tables by unique IDs or clean up orphaned test tables older than 2 hours (preventing concurrent CI runs from interfering with each other), replacing defer statements with t.Cleanup for reliable resource cleanup, and dynamically generating and cleaning up table names in RunExecuteSqlToolInvokeTest and RunToolInvokeWithTemplateParameters instead of using a hardcoded table name t. Additionally, SetupMsSQLTable was updated to use ExecContext and properly escape table names during teardown. I have no further feedback to provide as there are no review comments.

@lusosa-googlexwf

Copy link
Copy Markdown
Contributor Author

All MSSQL and Cloud SQL MSSQL integration tests are now passing successfully (cloud-sql-mssql: SUCCESS, mssql: SUCCESS).

@lusosa-googlexwf lusosa-googlexwf changed the title Fix/mssql test isolation test(mssql): isolate test tables and fix resource leaks in integration tests Nov 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants