Skip to content

Commit 714a842

Browse files
author
TRybina132
committed
fix tests
1 parent 8ffc6cc commit 714a842

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

ManagedCode.Storage.Azure/BlobStream.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace ManagedCode.Storage.Azure;
88

99
public class BlobStream : Stream
1010
{
11-
// TODO: add same thing to google and aws
1211
private const string MetadataLengthKey = "STREAM_LENGTH";
1312
private const int PageSizeInBytes = 512;
1413
public const int DefaultBufferSize = 1024 * 1024 * 4;
@@ -38,17 +37,16 @@ public override long Length
3837
{
3938
get
4039
{
41-
var realLength = 0L;
4240
var metadata = _pageBlob.GetProperties().Value.Metadata;
43-
var contentLenght = _pageBlob.GetProperties().Value.ContentLength;
4441
if (metadata.TryGetValue(MetadataLengthKey, out var length))
4542
{
46-
if (long.TryParse(length, out realLength))
43+
if (long.TryParse(length, out var realLength))
4744
{
4845
return realLength;
4946
}
5047
}
51-
48+
49+
var contentLenght = _pageBlob.GetProperties().Value.ContentLength;
5250
SetLengthInternal(contentLenght);
5351
return contentLenght;
5452
}

ManagedCode.Storage.Tests/Storages/Azure/AzureBlobStreamTests.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,60 @@ protected override ServiceProvider ConfigureServices()
2626
}
2727

2828
[Fact]
29-
public async Task ReadStream_WhenFileExists_ReturnData()
29+
public async Task ReadStreamWithStreamReader_WhenFileExists_ReturnData()
3030
{
3131
// Arrange
3232
var directory = "test-directory";
33-
var fileName = Guid.NewGuid().ToString();
3433
await using var localFile = LocalFile.FromRandomNameWithExtension(".txt");
3534
FileHelper.GenerateLocalFileWithData(localFile, 10);
3635
var storage = (IAzureStorage) Storage;
3736

3837
UploadOptions options = new() { FileName = localFile.Name, Directory = directory };
39-
var result = await storage.UploadAsync(localFile.FileInfo.OpenRead(), options);
38+
await using var localFileStream = localFile.FileInfo.OpenRead();
39+
var result = await storage.UploadAsync(localFileStream, options);
4040

4141
await using var blobStream = storage.GetBlobStream(result.Value.FullName);
42-
43-
var chunkSize = localFile.FileInfo.Length;
44-
byte[] fileChunk1 = new byte[chunkSize];
45-
byte[] fileChunk2 = new byte[chunkSize];
4642

4743
// Act
4844
using var streamReader = new StreamReader(blobStream);
4945
var content = await streamReader.ReadToEndAsync();
5046

5147
// Assert
52-
content.Should().NotBeNullOrEmpty();
53-
using var fileReader = new StreamReader(localFile.FileStream);
48+
await using var fileStream = localFile.FileInfo.OpenRead();
49+
using var fileReader = new StreamReader(fileStream);
5450
var fileContent = await fileReader.ReadToEndAsync();
51+
content.Should().NotBeNullOrEmpty();
5552
fileContent.Should().NotBeNullOrEmpty();
5653
content.Should().Be(fileContent);
5754
}
55+
56+
[Fact]
57+
public async Task ReadStream_WhenFileExists_ReturnData()
58+
{
59+
// Arrange
60+
var directory = "test-directory";
61+
var localFile = LocalFile.FromRandomNameWithExtension(".txt");
62+
FileHelper.GenerateLocalFileWithData(localFile, 10);
63+
var storage = (IAzureStorage) Storage;
64+
65+
UploadOptions options = new() { FileName = localFile.Name, Directory = directory };
66+
await using var fileStream = localFile.FileInfo.OpenRead();
67+
var result = await storage.UploadAsync(fileStream, options);
68+
69+
await using var blobStream = storage.GetBlobStream(result.Value.FullName);
70+
71+
var chunkSize = (int) blobStream.Length / 2;
72+
var chunk1 = new byte[chunkSize];
73+
var chunk2 = new byte[chunkSize];
74+
75+
// Act
76+
var bytesReadForChunk1 = await blobStream.ReadAsync(chunk1, 0, chunkSize);
77+
var bytesReadForChunk2 = await blobStream.ReadAsync(chunk2, 0, chunkSize);
78+
79+
// Assert
80+
bytesReadForChunk1.Should().Be(chunkSize);
81+
bytesReadForChunk2.Should().Be(chunkSize);
82+
chunk1.Should().NotBeNullOrEmpty().And.HaveCount(chunkSize);
83+
chunk2.Should().NotBeNullOrEmpty().And.HaveCount(chunkSize);
84+
}
5885
}

0 commit comments

Comments
 (0)