Skip to content

Commit 4cae698

Browse files
committed
Initialize ChunkStream with the data to read.
This will let us test if the blob is stored and retrieved exactly as-is. Signed-off-by: Bradley Grainger <[email protected]>
1 parent 140f572 commit 4cae698

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

tests/IntegrationTests/ChunkStream.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ namespace IntegrationTests;
22

33
internal sealed class ChunkStream : Stream
44
{
5-
public ChunkStream(int dataLength, int chunkLength)
5+
public ChunkStream(byte[] data, int chunkLength)
66
{
7-
m_dataLength = dataLength;
7+
if (data is null)
8+
throw new ArgumentNullException(nameof(data));
9+
if (chunkLength <= 0)
10+
throw new ArgumentOutOfRangeException(nameof(chunkLength));
11+
12+
m_data = data;
813
m_chunkLength = chunkLength;
914
m_position = 0;
1015
}
1116

1217
public override bool CanRead => true;
1318
public override bool CanSeek => false;
1419
public override bool CanWrite => false;
15-
public override long Length => m_dataLength;
20+
public override long Length => m_data.Length;
1621
public override long Position
1722
{
1823
get => m_position;
@@ -37,17 +42,14 @@ public override int Read(byte[] buffer, int offset, int count)
3742
#endif
3843
int Read(Span<byte> buffer)
3944
{
40-
if (m_position >= m_dataLength)
45+
if (m_position >= m_data.Length)
4146
return 0;
4247

4348
// Read at most chunkLength bytes
44-
var bytesToRead = Math.Min(buffer.Length, Math.Min(m_chunkLength, m_dataLength - m_position));
49+
var bytesToRead = Math.Min(buffer.Length, Math.Min(m_chunkLength, m_data.Length - m_position));
4550

46-
// Fill with dummy data (repeating pattern based on position)
47-
for (var i = 0; i < bytesToRead; i++)
48-
{
49-
buffer[i] = (byte) ((m_position + i) % 256);
50-
}
51+
// Copy data from the actual data array
52+
m_data.AsSpan(m_position, bytesToRead).CopyTo(buffer);
5153

5254
m_position += bytesToRead;
5355
return bytesToRead;
@@ -122,7 +124,7 @@ public override void Flush() =>
122124
public override Task FlushAsync(CancellationToken cancellationToken) =>
123125
throw new NotSupportedException();
124126

125-
private readonly int m_dataLength;
127+
private readonly byte[] m_data;
126128
private readonly int m_chunkLength;
127129
private int m_position;
128130
}

tests/IntegrationTests/InsertTests.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ public async Task SendLongData(int dataLength, int chunkLength, bool isAsync)
515515
create table insert_mysql_long_data(rowid integer not null primary key auto_increment, value longblob);
516516
""");
517517

518-
using var chunkStream = new ChunkStream(dataLength, chunkLength);
518+
var random = new Random(dataLength);
519+
var data = new byte[dataLength];
520+
random.NextBytes(data);
521+
522+
using var chunkStream = new ChunkStream(data, chunkLength);
519523

520524
using var writeCommand = new MySqlCommand("insert into insert_mysql_long_data(value) values(@value);", connection);
521525
writeCommand.Parameters.AddWithValue("@value", chunkStream);
@@ -525,10 +529,20 @@ public async Task SendLongData(int dataLength, int chunkLength, bool isAsync)
525529
else
526530
writeCommand.ExecuteNonQuery();
527531

528-
using var readLengthCommand = new MySqlCommand("select length(value) from insert_mysql_long_data order by rowid;", connection);
529-
using var reader = readLengthCommand.ExecuteReader();
530-
Assert.True(reader.Read());
531-
Assert.Equal(chunkStream.Length, reader.GetInt32(0));
532+
using var readCommand = new MySqlCommand("select length(value) from insert_mysql_long_data order by rowid;", connection);
533+
using (var reader = readCommand.ExecuteReader())
534+
{
535+
Assert.True(reader.Read());
536+
Assert.Equal(chunkStream.Length, reader.GetInt32(0));
537+
}
538+
539+
readCommand.CommandText = "select value from insert_mysql_long_data order by rowid;";
540+
using (var reader = readCommand.ExecuteReader())
541+
{
542+
Assert.True(reader.Read());
543+
var readData = (byte[]) reader.GetValue(0);
544+
Assert.True(data.AsSpan().SequenceEqual(readData)); // much faster than Assert.Equal
545+
}
532546
}
533547

534548
[Theory]

0 commit comments

Comments
 (0)