Пример #1
0
 private static DistributedEvictionSettings WithMockDistributedLocationStore(MockDistributedLocationStore mock)
 {
     return(new DistributedEvictionSettings(
                (context, contentHashesWithInfo, cts, urgencyHint) => null,
                locationStoreBatchSize: 42,
                replicaCreditInMinutes: null,
                distributedStore: mock));
 }
Пример #2
0
        public async Task TestSelfCheckAsync()
        {
            var mockDistributedLocationStore = new MockDistributedLocationStore();

            using (var testDirectory = new DisposableDirectory(FileSystem))
            {
                var context = new Context(Logger);

                var store = CreateStore(testDirectory, Settings, WithMockDistributedLocationStore(mockDistributedLocationStore));

                await store.StartupAsync(context).ShouldBeSuccess();

                var result = await store.SelfCheckContentDirectoryAsync(context, CancellationToken.None).ShouldBeSuccess();

                result.Value.TotalProcessedFiles.Should().Be(0, "TotalProcessedFiles should be 0 for an empty store.");

                // Running self check, but it should be up-to-date
                result = await store.SelfCheckContentDirectoryAsync(context, CancellationToken.None).ShouldBeSuccess();

                result.Value.TotalProcessedFiles.Should().Be(-1, "SelfCheck should be skipped due to up-to-date check.");

                // Now we're going to mess up with the content by putting the file and changing it on disk.
                var putResult = await store.PutRandomAsync(context, ValueSize).ShouldBeSuccess();

                var currentSize = store.ContentDirectorySize();

                var pathInCache = store.GetPrimaryPathFor(putResult.ContentHash);
                FileSystem.WriteAllText(pathInCache, "Definitely wrong content");

                // Moving the time forward to make self check is not up-to-date.
                Clock.UtcNow = Clock.UtcNow + TimeSpan.FromDays(2);

                result = await store.SelfCheckContentDirectoryAsync(context, CancellationToken.None).ShouldBeSuccess();

                result.Value.InvalidFiles.Should().Be(1);

                // An invalid file should be removed from:

                // 1. File system
                FileSystem.FileExists(pathInCache).Should().BeFalse("The store should delete the file with invalid content.");

                // 2. Content directory
                store.ContentDirectorySize().Should().Be(currentSize - ValueSize);

                // 3. Quota Keeper
                store.QuotaKeeperSize().Should().Be(currentSize - ValueSize);

                // 4. The content should be removed from distributed store.
                mockDistributedLocationStore.UnregisteredHashes.Should().NotBeNullOrEmpty();
            }
        }