From 4873ea55cb811aed355d70556aafabb0bcd4aa44 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 13 Sep 2026 11:16:28 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7=E3=81=AE?= =?UTF-8?q?=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=81=BF=E3=81=AE=E8=A9=A6=E9=A8=93?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyForge.Tests/ProxyUpgradeLoaderTests.cs | 202 ++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 ProxyForge.Tests/ProxyUpgradeLoaderTests.cs diff --git a/ProxyForge.Tests/ProxyUpgradeLoaderTests.cs b/ProxyForge.Tests/ProxyUpgradeLoaderTests.cs new file mode 100644 index 0000000..19136ac --- /dev/null +++ b/ProxyForge.Tests/ProxyUpgradeLoaderTests.cs @@ -0,0 +1,202 @@ +using System.IO; +using ProxyForge.Cache; +using ProxyForge.Encoding; +using ProxyForge.Export; +using ProxyForge.Sources; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.Tests; + +public sealed class ProxyUpgradeLoaderTests : IDisposable +{ + readonly string root = Path.Combine(Path.GetTempPath(), "ProxyForgeTests", Guid.NewGuid().ToString("N")); + readonly GraphicsDevices devices = new(); + readonly IGraphicsDevicesAndContext context; + readonly ProxyCache cache; + readonly ProxyGenerationQueue queue; + readonly List created = []; + bool refuse; + + public ProxyUpgradeLoaderTests() + { + Directory.CreateDirectory(root); + context = devices.CreateContext(); + cache = new ProxyCache(Path.Combine(root, "cache")); + queue = new ProxyGenerationQueue(cache, Encode, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }) + { + CompletedRetention = TimeSpan.FromMilliseconds(50), + }; + } + + public void Dispose() + { + context.Dispose(); + devices.Dispose(); + try + { + Directory.Delete(root, true); + } + catch (IOException) + { + } + } + + static Task Encode(ProxyEncodeRequest request, IProgress? progress, CancellationToken cancellationToken) + { + File.WriteAllBytes(request.OutputPath, new byte[64]); + return Task.FromResult(new ProxyEncodeResult(new ProxyGeometry(960, 540), new DisplayBounds(-960f, -540f, 1920f, 1080f), new FrameRate(30, 1), TimeSpan.FromSeconds(2), 60, 64)); + } + + YukkuriMovieMaker.Plugin.FileSource.IVideoFileSource? Factory(IGraphicsDevicesAndContext devices, string path) + { + if (refuse) + return null; + + var source = TestVideoSource.Solid(devices, 960, 540, 30, 1, 60); + created.Add(source); + return source; + } + + SourceIdentity CreateSource(string name = "source.mp4") + { + var path = Path.Combine(root, name); + File.WriteAllBytes(path, new byte[100]); + return SourceIdentity.Of(path)!.Value; + } + + ProxyVideoSource CreateWrapper(out TestVideoSource original) + { + original = TestVideoSource.Solid(context, 1920, 1080, 30, 1, 60); + return ProxyVideoSource.FromOriginal(context, original); + } + + static async Task WaitUntilAsync(Func condition) + { + for (var attempt = 0; attempt < 300 && !condition(); attempt++) + await Task.Delay(10, TestContext.Current.CancellationToken); + Assert.True(condition()); + } + + [Fact] + public async Task AnExistingProxyIsLoadedAtOnce() + { + var source = CreateSource(); + queue.TryEnqueue(source, 50); + await queue.WhenIdleAsync(); + var wrapper = CreateWrapper(out var original); + + wrapper.AttachLoader(new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory)); + await WaitUntilAsync(() => created.Count == 1); + await WaitUntilAsync(() => + { + wrapper.Update(TimeSpan.Zero); + return wrapper.IsProxy; + }); + + Assert.True(original.IsDisposed); + wrapper.Dispose(); + Assert.True(created[0].IsDisposed); + } + + [Fact] + public async Task AProxyGeneratedLaterIsLoadedWhenTheQueueAnnouncesIt() + { + var source = CreateSource(); + var wrapper = CreateWrapper(out _); + wrapper.AttachLoader(new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory)); + await Task.Delay(50, TestContext.Current.CancellationToken); + Assert.Empty(created); + + queue.TryEnqueue(source, 50); + await queue.WhenIdleAsync(); + await WaitUntilAsync(() => + { + wrapper.Update(TimeSpan.Zero); + return wrapper.IsProxy; + }); + + wrapper.Dispose(); + } + + [Fact] + public async Task AnAnnouncementForAnotherScaleIsIgnored() + { + var source = CreateSource(); + var wrapper = CreateWrapper(out _); + wrapper.AttachLoader(new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory)); + + queue.TryEnqueue(source, 25); + await queue.WhenIdleAsync(); + await Task.Delay(100, TestContext.Current.CancellationToken); + wrapper.Update(TimeSpan.Zero); + + Assert.False(wrapper.IsProxy); + Assert.Empty(created); + wrapper.Dispose(); + } + + [Fact] + public async Task AnAnnouncementForAnotherFileIsIgnored() + { + var source = CreateSource(); + var other = CreateSource("other.mp4"); + var wrapper = CreateWrapper(out _); + wrapper.AttachLoader(new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory)); + + queue.TryEnqueue(other, 50); + await queue.WhenIdleAsync(); + await Task.Delay(100, TestContext.Current.CancellationToken); + wrapper.Update(TimeSpan.Zero); + + Assert.False(wrapper.IsProxy); + Assert.Empty(created); + wrapper.Dispose(); + } + + [Fact] + public async Task AProxyNobodyCanOpenIsRemovedFromTheCache() + { + var source = CreateSource(); + queue.TryEnqueue(source, 50); + await queue.WhenIdleAsync(); + refuse = true; + var wrapper = CreateWrapper(out _); + + wrapper.AttachLoader(new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory)); + await WaitUntilAsync(() => cache.Count == 0); + wrapper.Update(TimeSpan.Zero); + + Assert.False(wrapper.IsProxy); + wrapper.Dispose(); + } + + [Fact] + public async Task ADisposedWrapperNeverReceivesTheProxy() + { + var source = CreateSource(); + var wrapper = CreateWrapper(out _); + var loader = new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory); + wrapper.AttachLoader(loader); + wrapper.Dispose(); + + queue.TryEnqueue(source, 50); + await queue.WhenIdleAsync(); + await Task.Delay(100, TestContext.Current.CancellationToken); + + Assert.Empty(created); + } + + [Fact] + public async Task ALoaderDisposedWhileLoadingDisposesWhatItOpened() + { + var source = CreateSource(); + queue.TryEnqueue(source, 50); + await queue.WhenIdleAsync(); + var wrapper = CreateWrapper(out _); + var loader = new ProxyUpgradeLoader(wrapper, devices, source, 50, cache, queue, Factory); + wrapper.Dispose(); + + await WaitUntilAsync(() => created.Count == 1 && created[0].IsDisposed); + loader.Dispose(); + } +}