-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileSystemTestContext.cs
More file actions
164 lines (142 loc) · 5.37 KB
/
VirtualFileSystemTestContext.cs
File metadata and controls
164 lines (142 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections.Concurrent;
using ManagedCode.Storage.Core;
using ManagedCode.Storage.Core.Models;
using ManagedCode.Storage.VirtualFileSystem.Metadata;
using ManagedCode.Storage.VirtualFileSystem.Options;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using VfsImplementation = ManagedCode.Storage.VirtualFileSystem.Implementations.VirtualFileSystem;
namespace MediaLibrary;
public sealed class VirtualFileSystemTestContext : IAsyncDisposable
{
private readonly bool _ownsStorage;
private readonly IServiceProvider? _serviceProvider;
private readonly Func<ValueTask>? _cleanup;
private readonly MemoryCache _cache;
private VirtualFileSystemTestContext(
IStorage storage,
TestMetadataManager metadataManager,
VfsImplementation fileSystem,
MemoryCache cache,
bool ownsStorage,
IServiceProvider? serviceProvider,
string containerName,
Func<ValueTask>? cleanup)
{
Storage = storage;
MetadataManager = metadataManager;
FileSystem = fileSystem;
ContainerName = containerName;
_cache = cache;
_ownsStorage = ownsStorage;
_serviceProvider = serviceProvider;
_cleanup = cleanup;
}
public IStorage Storage { get; }
public TestMetadataManager MetadataManager { get; }
public VfsImplementation FileSystem { get; }
public string ContainerName { get; }
public static async Task<VirtualFileSystemTestContext> CreateAsync(
IStorage storage,
string containerName,
bool ownsStorage,
IServiceProvider? serviceProvider,
Func<ValueTask>? cleanup = null)
{
var metadataManager = new TestMetadataManager(storage);
var cache = new MemoryCache(new MemoryCacheOptions());
var options = Options.Create(new VfsOptions
{
DefaultContainer = containerName,
DirectoryStrategy = DirectoryStrategy.Virtual,
EnableCache = true
});
var vfs = new VfsImplementation(
storage,
metadataManager,
options,
cache,
NullLogger<VfsImplementation>.Instance);
var createResult = await storage.CreateContainerAsync();
if (!createResult.IsSuccess)
{
throw new InvalidOperationException($"Failed to create container '{containerName}'.");
}
return new VirtualFileSystemTestContext(storage, metadataManager, vfs, cache, ownsStorage, serviceProvider, containerName, cleanup);
}
public async ValueTask DisposeAsync()
{
await FileSystem.DisposeAsync();
if (_cleanup is not null)
{
await _cleanup();
}
_cache.Dispose();
if (_ownsStorage)
{
switch (Storage)
{
case IAsyncDisposable asyncDisposable:
await asyncDisposable.DisposeAsync();
break;
case IDisposable disposable:
disposable.Dispose();
break;
}
}
if (_serviceProvider is IAsyncDisposable asyncProvider)
{
await asyncProvider.DisposeAsync();
}
else if (_serviceProvider is IDisposable disposableProvider)
{
disposableProvider.Dispose();
}
}
}
public sealed class TestMetadataManager : IMetadataManager
{
private readonly IStorage _storage;
private readonly ConcurrentDictionary<string, VfsMetadata> _metadata = new();
private readonly ConcurrentDictionary<string, IReadOnlyDictionary<string, string>> _customMetadata = new();
public TestMetadataManager(IStorage storage)
{
_storage = storage;
}
public int BlobInfoRequests { get; private set; }
public int CustomMetadataRequests { get; private set; }
public void ResetCounters()
{
BlobInfoRequests = 0;
CustomMetadataRequests = 0;
}
public Task SetVfsMetadataAsync(string blobName, VfsMetadata metadata, IDictionary<string, string>? customMetadata = null, string? expectedETag = null, CancellationToken cancellationToken = default)
{
_metadata[blobName] = metadata;
_customMetadata[blobName] = customMetadata is null
? new Dictionary<string, string>()
: new Dictionary<string, string>(customMetadata);
return Task.CompletedTask;
}
public Task<VfsMetadata?> GetVfsMetadataAsync(string blobName, CancellationToken cancellationToken = default)
{
_metadata.TryGetValue(blobName, out var metadata);
return Task.FromResult(metadata);
}
public Task<IReadOnlyDictionary<string, string>> GetCustomMetadataAsync(string blobName, CancellationToken cancellationToken = default)
{
CustomMetadataRequests++;
if (_customMetadata.TryGetValue(blobName, out var metadata))
{
return Task.FromResult(metadata);
}
return Task.FromResult<IReadOnlyDictionary<string, string>>(new Dictionary<string, string>());
}
public async Task<BlobMetadata?> GetBlobInfoAsync(string blobName, CancellationToken cancellationToken = default)
{
BlobInfoRequests++;
var result = await _storage.GetBlobMetadataAsync(blobName, cancellationToken);
return result.IsSuccess ? result.Value : null;
}
}