-
Notifications
You must be signed in to change notification settings - Fork 318
Draft | Some more test cleanup #3860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
25d570a
95855ed
65c5407
3c10e3c
394e255
ea468ff
a8b7ee4
39d3839
72bf546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.Tests.Common | ||
| { | ||
| /// <summary> | ||
|
|
@@ -13,14 +16,55 @@ public static class SqlDataReaderExtensions | |
| /// Reads all result sets in the provided <paramref name="dataReader"/> and discards them. | ||
| /// </summary> | ||
| /// <param name="dataReader">Reader to flush results from.</param> | ||
| public static void FlushAllResults(this SqlDataReader dataReader) | ||
| /// <param name="flushResults"> | ||
| /// If <c>true</c>, the records in each result set will be flushed, too. If <c>false</c> | ||
| /// only <see cref="SqlDataReader.NextResult"/> will be the only method called. | ||
| /// </param> | ||
|
Comment on lines
+19
to
+22
|
||
| public static void FlushAllResults(this SqlDataReader dataReader, bool flushResults = true) | ||
| { | ||
| do | ||
| { | ||
| dataReader.FlushResultSet(); | ||
| if (flushResults) | ||
| { | ||
| dataReader.FlushResultSet(); | ||
| } | ||
| } while (dataReader.NextResult()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads all result sets in the provided <paramref name="dataReader"/> and discards them. | ||
| /// </summary> | ||
| /// <param name="dataReader">Reader to flush results from.</param> | ||
| /// <param name="flushResults"> | ||
| /// If <c>true</c>, the records in each result set will be flushed, too. If <c>false</c> | ||
| /// only <see cref="SqlDataReader.NextResultAsync()"/> will be the only method called. | ||
|
||
| /// </param> | ||
|
Comment on lines
+38
to
+41
|
||
| public static Task FlushAllResultsAsync(this SqlDataReader dataReader, bool flushResults = true) => | ||
| FlushAllResultsAsync(dataReader, CancellationToken.None, flushResults); | ||
|
|
||
| /// <summary> | ||
| /// Reads all result sets in the provided <paramref name="dataReader"/> and discards them. | ||
| /// </summary> | ||
| /// <param name="dataReader">Reader to flush results from.</param> | ||
| /// <param name="cancellationToken">Token to use for premature cancellation of the task.</param> | ||
| /// <param name="flushResults"> | ||
| /// If <c>true</c>, the records in each result set will be flushed, too. If <c>false</c> | ||
| /// only <see cref="SqlDataReader.NextResultAsync()"/> will be the only method called. | ||
|
||
| /// </param> | ||
|
Comment on lines
+50
to
+53
|
||
| public static async Task FlushAllResultsAsync( | ||
| this SqlDataReader dataReader, | ||
| CancellationToken cancellationToken, | ||
| bool flushResults = true) | ||
| { | ||
| do | ||
| { | ||
| if (flushResults) | ||
| { | ||
| await dataReader.FlushResultSetAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } while (await dataReader.NextResultAsync(cancellationToken).ConfigureAwait(false)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads all results in the current result set of the provided <paramref name="dataReader"/> | ||
| /// and discards them. | ||
|
|
@@ -33,5 +77,27 @@ public static void FlushResultSet(this SqlDataReader dataReader) | |
| // Discard results. | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads all results in the current result set of the provided <paramref name="dataReader"/> | ||
| /// and discards them. | ||
| /// </summary> | ||
| /// <param name="dataReader">Reader to flush results from.</param> | ||
| public static Task FlushResultSetAsync(this SqlDataReader dataReader) => | ||
| FlushResultSetAsync(dataReader, CancellationToken.None); | ||
|
|
||
| /// <summary> | ||
| /// Reads all results in the current result set of the provided <paramref name="dataReader"/> | ||
| /// and discards them. | ||
| /// </summary> | ||
| /// <param name="dataReader">Reader to flush results from.</param> | ||
| /// <param name="cancellationToken">Token to use for premature cancellation of the task.</param> | ||
| public static async Task FlushResultSetAsync(this SqlDataReader dataReader, CancellationToken cancellationToken) | ||
| { | ||
| while (await dataReader.ReadAsync(cancellationToken).ConfigureAwait(false)) | ||
| { | ||
| // Discard results. | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -53,7 +53,7 @@ public static void PassThroughSinglePacket(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -67,7 +67,7 @@ public static void PassThroughMultiplePacket(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -81,7 +81,7 @@ public static void PassThroughMultiplePacketWithShortEnd(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -96,7 +96,7 @@ public static void ReconstructSinglePacket(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -115,7 +115,7 @@ public static void Reconstruct2Packets_Part_PartFull(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -134,7 +134,7 @@ public static void Reconstruct2Packets_Full_FullPart_Part(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -154,7 +154,7 @@ public static void ReconstructMultiplePacketSequence(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -173,7 +173,7 @@ public static void ReconstructMultiplePacketSequenceWithShortEnd(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalTheory(nameof(IsUsingModernProcessSni)), MemberData(nameof(IsAsync))] | ||||||
|
|
@@ -189,7 +189,7 @@ public static void Reconstruct3Packets_PartPartPart(bool isAsync) | |||||
|
|
||||||
| var output = MultiplexPacketList(isAsync, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalFact(nameof(IsUsingModernProcessSni))] | ||||||
|
|
@@ -208,7 +208,7 @@ public static void TrailingPartialPacketInSnapshotNotDuplicated() | |||||
|
|
||||||
| var output = MultiplexPacketList(true, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ConditionalFact(nameof(IsUsingModernProcessSni))] | ||||||
|
|
@@ -252,7 +252,7 @@ public static void MultipleFullPacketsInRemainderAreSplitCorrectly() | |||||
|
|
||||||
| var output = MultiplexPacketList(false, dataSize, input); | ||||||
|
|
||||||
| ComparePacketLists(dataSize, expected, output); | ||||||
| ComparePacketLists(expected, output); | ||||||
| } | ||||||
|
|
||||||
| [ExcludeFromCodeCoverage] | ||||||
|
|
@@ -269,16 +269,14 @@ private static List<PacketData> MultiplexPacketList(bool isAsync, int dataSize, | |||||
|
|
||||||
| if (stateObject._inBytesRead > 0) | ||||||
| { | ||||||
| if ( | ||||||
| stateObject._inBytesRead < TdsEnums.HEADER_LEN | ||||||
| || | ||||||
| stateObject._inBytesRead != (TdsEnums.HEADER_LEN + | ||||||
| Packet.GetDataLengthFromHeader( | ||||||
| stateObject._inBuff.AsSpan(0, TdsEnums.HEADER_LEN))) | ||||||
| ) | ||||||
| { | ||||||
| Assert.Fail("incomplete packet exposed after call to ProcessSniPacket"); | ||||||
| } | ||||||
| // At least the header must be read | ||||||
| Assert.False(stateObject._inBytesRead < TdsEnums.HEADER_LEN); | ||||||
|
|
||||||
| // The full packet must be read, too | ||||||
| Span<byte> header = stateObject._inBuff.AsSpan(0, TdsEnums.HEADER_LEN); | ||||||
| int packetLength = Packet.GetDataLengthFromHeader(header); | ||||||
| int expectedLength = TdsEnums.HEADER_LEN + packetLength; | ||||||
| Assert.Equal(expectedLength, stateObject._inBytesRead); | ||||||
|
|
||||||
| if (!isAsync) | ||||||
| { | ||||||
|
|
@@ -299,17 +297,14 @@ private static List<PacketData> MultiplexPacketList(bool isAsync, int dataSize, | |||||
|
|
||||||
| if (stateObject._inBytesRead > 0) | ||||||
| { | ||||||
| if ( | ||||||
| stateObject._inBytesRead < TdsEnums.HEADER_LEN | ||||||
| || | ||||||
| stateObject._inBytesRead != (TdsEnums.HEADER_LEN + | ||||||
| Packet.GetDataLengthFromHeader( | ||||||
| stateObject._inBuff.AsSpan(0, TdsEnums.HEADER_LEN))) | ||||||
| ) | ||||||
| { | ||||||
| Assert.Fail( | ||||||
| "incomplete packet exposed after call to ProcessSniPacket with usePartialPacket"); | ||||||
| } | ||||||
| // Header must at least have been read | ||||||
| Assert.False(stateObject._inBytesRead < TdsEnums.HEADER_LEN); | ||||||
|
||||||
| Assert.False(stateObject._inBytesRead < TdsEnums.HEADER_LEN); | |
| Assert.True(stateObject._inBytesRead >= TdsEnums.HEADER_LEN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't have it both ways, my dude. Assert false is what you asked for above, so you will get both scenarios using Assert false.
Copilot
AI
Dec 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comparison logic using SequenceCompareTo has been changed to SequenceEqual. While both can validate equality, SequenceCompareTo returns an integer (0 for equal sequences), whereas SequenceEqual returns a boolean directly. The new implementation correctly uses SequenceEqual with Assert.True, which is more idiomatic and readable. However, the assertion message format has changed from using Assert.Fail with string interpolation to Assert.True with a message parameter. While both work, consider using Assert.Equal with custom comparer or Assert.True consistently across similar test patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phrase "will be the only method called" is redundant. Consider simplifying to "only will be called."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not doing it.