Skip to content

Commit

Permalink
Fix blob properties - set ContentLength properly and use `TimeProvi…
Browse files Browse the repository at this point in the history
…der` for `createdOn` and `lastModified` values (#16)
  • Loading branch information
tomas-pajurek authored Sep 13, 2024
1 parent f79885e commit 348fae1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Spotflow.InMemory.Azure.Storage.Blobs.Internals;

internal class InMemoryBlobContainer(string name, IDictionary<string, string>? metadata, InMemoryBlobService service)
{
private readonly TimeProvider _timeProvider = service.Account.Provider.TimeProvider;

private readonly object _lock = new();
private readonly Dictionary<string, BlobEntry> _blobEntries = [];
Expand Down Expand Up @@ -70,7 +71,7 @@ private BlobEntry GetBlobEntry(string blobName)
{
if (!_blobEntries.TryGetValue(blobName, out entry))
{
var blob = new InMemoryBlockBlob(blobName, this);
var blob = new InMemoryBlockBlob(blobName, this, _timeProvider);
entry = new(blob, new(1, 1));
_blobEntries.Add(blobName, entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Spotflow.InMemory.Azure.Storage.Blobs.Internals;

internal class InMemoryBlockBlob(string blobName, InMemoryBlobContainer container)
internal class InMemoryBlockBlob(string blobName, InMemoryBlobContainer container, TimeProvider timeProvider)
{
private Dictionary<string, Block>? _uncommittedBlocks = null;
private List<Block>? _committedBlocks = null;
Expand Down Expand Up @@ -336,19 +336,23 @@ private BinaryData GetContent()
[MemberNotNull(nameof(_committedBlocks))]
private void SetCommitedState(BlobHttpHeaders? headers, IDictionary<string, string>? metadata, List<Block> committedBlocks)
{
var newProperties = BlobsModelFactory.BlobProperties(
contentLength: _committedBlocks is null ? 0 : GetContent().ToMemory().Length,
_cachedContent = null;
_uncommittedBlocks = null;
_committedBlocks = committedBlocks;

var now = timeProvider.GetUtcNow();

var createdOn = _properties?.CreatedOn ?? now;

_properties = BlobsModelFactory.BlobProperties(
contentLength: GetContent().ToMemory().Length,
metadata: metadata ?? _properties?.Metadata,
eTag: new ETag(Guid.NewGuid().ToString()),
lastModified: DateTimeOffset.UtcNow,
lastModified: now,
contentType: headers?.ContentType ?? _properties?.ContentType,
contentEncoding: headers?.ContentEncoding ?? _properties?.ContentEncoding
contentEncoding: headers?.ContentEncoding ?? _properties?.ContentEncoding,
createdOn: createdOn
);

_properties = newProperties;
_cachedContent = null;
_uncommittedBlocks = null;
_committedBlocks = committedBlocks;
}

private void DeleteCore()
Expand Down
3 changes: 3 additions & 0 deletions tests/Tests/Storage/Blobs/BlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ public void GetProperties_For_Existing_Blob_Should_Succeed(BlobClientType client

var properties = blobClient.GetProperties().Value;

properties.ContentLength.Should().Be(13);
properties.CreatedOn.Should().BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromHours(1));
properties.LastModified.Should().Be(properties.CreatedOn);
properties.BlobType.Should().Be(BlobType.Block);
}

Expand Down

0 comments on commit 348fae1

Please sign in to comment.