using System;
using System.ComponentModel;
using System.IO;
using NewLife.XCoder.Tools.Folder;
using Xunit;
namespace XCoder.App.Tests;
/// <summary>目录缓存读写测试</summary>
public class DiskCacheTests : IDisposable
{
private readonly String _root = Path.Combine(Path.GetTempPath(), "xcoder-cache-test-" + Guid.NewGuid().ToString("N"));
public DiskCacheTests() => Directory.CreateDirectory(_root);
public void Dispose()
{
try { if (Directory.Exists(_root)) Directory.Delete(_root, true); } catch { }
}
[Fact]
[DisplayName("缓存保存后可以完整读回")]
public void SaveLoad_RoundTrips()
{
var root = Path.Combine(_root, "volume");
var a = Path.Combine(root, "a");
var b = Path.Combine(a, "b");
Directory.CreateDirectory(b);
var file = Path.Combine(_root, "index.xcdx");
var cache = new DiskCache(root, file);
cache.Set(root, 100, 1, 1, [10, 20, 0, 0, 0, 0, 0, 0], true);
cache.Set(a, 80, 1, 1, [10, 0, 0, 0, 0, 0, 0, 0], true);
cache.Set(b, 40, 1, 0, [0, 0, 0, 0, 0, 0, 0, 0], false);
cache.Save();
var loaded = new DiskCache(root, file);
Assert.True(loaded.Read());
Assert.Equal(3, loaded.Count);
Assert.True(loaded.TryGet(b, out var entry));
Assert.Equal(40, entry.Size);
Assert.Equal(1, entry.FileCount);
Assert.False(entry.Complete);
Assert.True(loaded.TryGet(a, out var aEntry));
Assert.Equal(10, aEntry.Categories[0]);
Assert.True(loaded.TryGet(root, out var rootEntry));
Assert.Equal(20, rootEntry.Categories[1]);
}
[Fact]
[DisplayName("重复保存原子替换为最新快照")]
public void Save_ReplacesExisting()
{
var root = Path.Combine(_root, "volume");
Directory.CreateDirectory(root);
var file = Path.Combine(_root, "index.xcdx");
var first = new DiskCache(root, file);
first.Set(root, 10, 1, 0, null, false);
first.Save();
var second = new DiskCache(root, file);
second.Set(root, 20, 2, 0, null, true);
second.Save();
var loaded = new DiskCache(root, file);
Assert.True(loaded.Read());
Assert.True(loaded.TryGet(root, out var entry));
Assert.Equal(20, entry.Size);
Assert.True(entry.Complete);
}
[Fact]
[DisplayName("损坏的缓存文件按空缓存处理")]
public void Read_Corrupt_ReturnsFalse()
{
var root = Path.Combine(_root, "volume");
Directory.CreateDirectory(root);
var file = Path.Combine(_root, "index.xcdx");
File.WriteAllText(file, "not a cache");
var cache = new DiskCache(root, file);
Assert.False(cache.Read());
Assert.Equal(0, cache.Count);
}
[Fact]
[DisplayName("删除子树只移除该目录及后代")]
public void RemoveSubtree_RemovesDescendants()
{
var root = Path.Combine(_root, "volume");
var a = Path.Combine(root, "a");
var b = Path.Combine(a, "b");
var c = Path.Combine(root, "c");
Directory.CreateDirectory(b);
Directory.CreateDirectory(c);
var cache = new DiskCache(root, Path.Combine(_root, "index.xcdx"));
cache.Set(root, 100, 0, 0, null, false);
cache.Set(a, 60, 0, 0, null, false);
cache.Set(b, 30, 0, 0, null, false);
cache.Set(c, 40, 0, 0, null, false);
cache.RemoveSubtree(a);
Assert.True(cache.TryGet(root, out _));
Assert.False(cache.TryGet(a, out _));
Assert.False(cache.TryGet(b, out _));
Assert.True(cache.TryGet(c, out _));
}
[Fact]
[DisplayName("祖先聚合按增量同步")]
public void AdjustAncestors_UpdatesSizes()
{
var root = Path.Combine(_root, "volume");
var a = Path.Combine(root, "a");
var b = Path.Combine(a, "b");
Directory.CreateDirectory(b);
var cache = new DiskCache(root, Path.Combine(_root, "index.xcdx"));
cache.Set(root, 100, 10, 2, null, false);
cache.Set(a, 60, 5, 1, null, false);
cache.Set(b, 30, 2, 0, null, false);
cache.AdjustAncestors(b, -10, -1, 0, null);
Assert.True(cache.TryGet(root, out var rootEntry));
Assert.True(cache.TryGet(a, out var aEntry));
Assert.True(cache.TryGet(b, out var bEntry));
Assert.Equal(90, rootEntry.Size);
Assert.Equal(9, rootEntry.FileCount);
Assert.Equal(50, aEntry.Size);
Assert.Equal(4, aEntry.FileCount);
Assert.Equal(30, bEntry.Size);
}
[Fact]
[DisplayName("不同卷使用不同缓存文件")]
public void GetFilePath_SeparatesRoots()
{
var one = DiskCache.GetFilePath(Path.Combine(_root, "one"), _root);
var two = DiskCache.GetFilePath(Path.Combine(_root, "two"), _root);
Assert.NotEqual(one, two);
Assert.StartsWith(_root, one);
Assert.EndsWith(".xcdx", one);
}
[Fact]
[DisplayName("路径归属判断覆盖根与后代")]
public void IsUnder_ChecksRootAndChildren()
{
var root = Path.Combine(_root, "volume");
var child = Path.Combine(root, "a", "b");
Assert.True(DiskCache.IsUnder(root, root));
Assert.True(DiskCache.IsUnder(root, child));
Assert.False(DiskCache.IsUnder(child, root));
}
}
|