Skip to content

Conversation

@benrr101
Copy link
Contributor

@benrr101 benrr101 commented Dec 12, 2025

Description

As part of debugging some failing tests I've been working on, I tweaked some test code so I could more easily diagnose the issue. These changes aren't really worth mucking up the PRs I used them on, but I feel they are valuable enough to submit anyhow. Consider it a byproduct of other, more important work.

Here's a rundown of the changes:

  • Introduce SqlDataReaderExtension class that has some test-only extensions for commonly used behaviors that are just kinda ugly :)
    • FlushAllResults - Reads and discards all results/sets from the provided data reader
    • FlushResultSet - Reads and discards all results in the current set of the provided data reader
  • Rewrite the DataTestUtility from @paulmedynski
    • Original code relied on parsing the DisplayName property of the currently running ITest - unfortunately this property can differently structured depending on the value of methodDisplay, which may be overridden by IDE test runners (ie, my IDE's test runner).
    • New code traverses the object tree to find the class name and method name independently.
    • Replaced Asserts in the utility method with direct throws - since these are not tests, they should not be using test assertions to verify intermediate behavior.
  • Extract XEventScope from DataTestUtility to its own class. Some light cleanup has been done to use xmldocs where necessary, replace assertions with throws, reformat SQL strings.
  • Change AsyncCancelledConnectionTest from Fact that has two separate test runs inside it to a Theory with inline data for enabling/disabling MARS.
  • Rewrite AsyncCancelledConnectionTest to no longer capture all exceptions and check for an empty collection of exceptions at the end, but instead let the exceptions bubble to the top to cause the test to fail faster and better expose the exception in test results. (this was implemented by codex 🤖)
  • [Update] Rewrite AsyncCancelledConnectionTest such that all the tracking/status displaying code is removed. The logging was pretty meaningless and added a lot of complexity to the code.

Issues

N/A

Testing

Tests were ran locally and passed with their new changes. CI will validate.

@benrr101 benrr101 added this to the 7.0.0-preview4 milestone Dec 12, 2025
@benrr101 benrr101 requested a review from a team as a code owner December 12, 2025 00:08
Copilot AI review requested due to automatic review settings December 12, 2025 00:08
@benrr101 benrr101 added Area\Tests Issues that are targeted to tests or test projects Code Health 💊 Issues/PRs that are targeted to source code quality improvements. labels Dec 12, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves test code maintainability and diagnostics by extracting the XEventScope class from DataTestUtility into its own file, rewriting test name extraction logic to be more robust, introducing helper extensions for SqlDataReader, and refactoring the AsyncCancelledConnectionsTest to use modern async/await patterns with better exception handling.

Key Changes:

  • Extracted XEventScope from DataTestUtility into a standalone class with improved documentation and error handling (throws instead of asserts)
  • Rewrote DataTestUtility.CurrentTestName() to traverse the xUnit object tree directly rather than parsing DisplayName, making it resilient to IDE test runner configuration differences
  • Converted AsyncCancelledConnectionsTest from a Fact with internal loops to a Theory with InlineData, implementing fail-fast exception handling instead of collecting exceptions

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
SqlDataReaderExtensions.cs New helper class with FlushAllResults and FlushResultSet extension methods for discarding data reader results
XEventScope.cs Newly extracted class for managing XEvent sessions with improved XML docs, exception-based validation, and formatted SQL strings
DataTestUtility.cs Removed XEventScope nested class and rewrote CurrentTestName to use object tree traversal instead of regex parsing
AsyncCancelledConnectionsTest.cs Converted from Fact to Theory, refactored to use async/await properly, and implemented fail-fast exception handling with IsExpectedCancellation helper
XEventsTracingTest.cs Updated to use standalone XEventScope class
DataStreamTest.cs Updated to use standalone XEventScope class with target initializer syntax
Microsoft.Data.SqlClient.ManualTesting.Tests.csproj Added reference to new XEventScope.cs file

Copy link
Contributor

@paulmedynski paulmedynski left a comment

Choose a reason for hiding this comment

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

One question in the AsyncCancelledConnectionsTest.

}

private void RunCancelAsyncConnections(SqlConnectionStringBuilder connectionStringBuilder)
private async Task RunCancelAsyncConnections(SqlConnectionStringBuilder connectionStringBuilder)
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that you've made a single test theory, do we need a separate Run() function, or can this just be folded into the test body?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure why not - but be careful what you wish for. This unlocked my cowboy coder and I decided to strip out all the tracking code in the test :)

@paulmedynski paulmedynski self-assigned this Dec 12, 2025
@@ -0,0 +1,204 @@
using System;
Copy link
Contributor

Choose a reason for hiding this comment

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

missing license header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I fixed it in all the files

Copy link
Contributor

@mdaigle mdaigle left a comment

Choose a reason for hiding this comment

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

Accidentally clicked approve before

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

Labels

Area\Tests Issues that are targeted to tests or test projects Code Health 💊 Issues/PRs that are targeted to source code quality improvements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants