Skip to content

Commit

Permalink
Fix BlobWriteStream: Commit block list on each flush (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-pajurek authored Oct 11, 2024
1 parent 39448f7 commit 264d2e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ private async Task FlushUnsafeAsync(CancellationToken cancellationToken)

_blocks.Add(blockId);

var commitResult = await blobClient.CommitBlockListAsync(_blocks, options: _commitOptions, cancellationToken);

var newETag = commitResult.Value.ETag;

_commitOptions.Conditions.IfMatch = newETag;
_stageOptions.Conditions.IfMatch = newETag;

_bufferPosition = 0;
}


foreach (var interceptor in _interceptors)
{
await interceptor.FlushAsync();
Expand Down
37 changes: 37 additions & 0 deletions tests/Tests/Storage/Blobs/BlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;

using Spotflow.InMemory.Azure.Storage.FluentAssertions;

using Tests.Utils;

namespace Tests.Storage.Blobs;
Expand Down Expand Up @@ -326,6 +328,41 @@ public void OpenWrite_For_Existing_Blob_With_Conditions_Should_Fail(BlobClientTy

}

[TestMethod]
[TestCategory(TestCategory.AzureInfra)]
[DataRow(BlobClientType.Generic)]
[DataRow(BlobClientType.Block)]
public void OpenWrite_Stream_Should_Create_Block_And_Commit_On_Each_Flush(BlobClientType clientType)
{
var containerClient = ImplementationProvider.GetBlobContainerClient();

containerClient.CreateIfNotExists();

var blobName = Guid.NewGuid().ToString();

var blobClient = containerClient.GetBlobBaseClient(blobName, clientType);

using (var stream = OpenWrite(blobClient, true))
{
blobClient.Should().HaveNoCommittedBlocks();

stream.WriteByte(1);
stream.Flush();

blobClient.Should().HaveCommittedBlocks(1);

stream.WriteByte(2);
stream.Flush();

blobClient.Should().HaveCommittedBlocks(2);

stream.WriteByte(2);

}

blobClient.Should().HaveCommittedBlocks(3);
}


[TestMethod]
[TestCategory(TestCategory.AzureInfra)]
Expand Down

0 comments on commit 264d2e6

Please sign in to comment.