Skip to content

Fix sequential text reader bug and add covering test #3383

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

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override Task<int> ReadAsync(char[] buffer, int index, int count)
byte[] byteBuffer = PrepareByteBuffer(charsNeeded, out int byteBufferUsed);

// Permit a 0 byte read in order to advance the reader to the correct column
if ((byteBufferUsed < byteBuffer.Length) || (byteBuffer.Length == 0))
if (byteBufferUsed <= byteBuffer.Length || byteBuffer.Length == 0)
{
SqlDataReader reader = _reader;
if (reader != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,60 @@ integrated into a comprehensive development
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task CanReadSequentialDecreasingChunks()
{
// pattern repeat input allows you to more easily identify if chunks are incorrectly
// related to each other by seeing the start and end of sequential chunks and checking
// if they correctly move to the next char while debugging
// simply repeating a single char can't tell you where in the string it went wrong.
const string baseString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder inputBuilder = new StringBuilder();
while (inputBuilder.Length < (64 * 1024))
{
inputBuilder.Append(baseString);
inputBuilder.Append(' ');
}

string input = inputBuilder.ToString();

StringBuilder resultBuilder = new StringBuilder();
CancellationTokenSource cts = new CancellationTokenSource();
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
await connection.OpenAsync(cts.Token);

using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT CONVERT(varchar(max),@str) as a";
command.Parameters.AddWithValue("@str", input);

using (var reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cts.Token))
{
if (await reader.ReadAsync(cts.Token))
{
using (var textReader = reader.GetTextReader(0))
{
var buffer = new char[4096];
var charsReadCount = -1;
var start = 0;
while (charsReadCount != 0)
{
charsReadCount = await textReader.ReadAsync(buffer, start, buffer.Length - start);
resultBuilder.Append(buffer, start, charsReadCount);
start++;
}
}
}
}
}
}

string result = resultBuilder.ToString();

Assert.Equal(input, result);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task CanReadBinaryData()
{
Expand Down
Loading