From 6f26fc95fa19501a3d752c4ac87ec3b90d9b9978 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 13:18:55 +0900 Subject: [PATCH] =?UTF-8?q?SynchronizedCompositor=E3=81=AE=E3=83=A6?= =?UTF-8?q?=E3=83=8B=E3=83=83=E3=83=88=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SynchronizedCompositorTests.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 LuaScript.Tests/SynchronizedCompositorTests.cs diff --git a/LuaScript.Tests/SynchronizedCompositorTests.cs b/LuaScript.Tests/SynchronizedCompositorTests.cs new file mode 100644 index 0000000..d1d5be0 --- /dev/null +++ b/LuaScript.Tests/SynchronizedCompositorTests.cs @@ -0,0 +1,88 @@ +using System; +using System.Threading; + +namespace LuaScript.Tests +{ + public class SynchronizedCompositorTests + { + private sealed class GateObservingCompositor(SemaphoreSlim gate, bool result) : IBufferCompositor + { + public int Calls; + public int GateCountDuringCompose = -1; + + public bool TryCompose(byte[] dst, int dstW, int dstH, byte[] src, int srcW, int srcH, in DrawCommand command) + { + Calls++; + GateCountDuringCompose = gate.CurrentCount; + return result; + } + } + + private sealed class ThrowingCompositor : IBufferCompositor + { + public bool TryCompose(byte[] dst, int dstW, int dstH, byte[] src, int srcW, int srcH, in DrawCommand command) => + throw new InvalidOperationException("device lost"); + } + + private sealed class DisposableCompositor : IBufferCompositor, IDisposable + { + public bool Disposed; + + public bool TryCompose(byte[] dst, int dstW, int dstH, byte[] src, int srcW, int srcH, in DrawCommand command) => true; + + public void Dispose() => Disposed = true; + } + + private static readonly DrawCommand Command = new(0d, 0d, 0d, 1d, 1d, 0d); + + [Fact] + public void TryCompose_HoldsGateDuringComposeAndReleasesAfter() + { + using var gate = new SemaphoreSlim(1, 1); + var inner = new GateObservingCompositor(gate, result: true); + var compositor = new SynchronizedCompositor(inner, gate); + + Assert.True(compositor.TryCompose(new byte[4], 1, 1, new byte[4], 1, 1, Command)); + + Assert.Equal(1, inner.Calls); + Assert.Equal(0, inner.GateCountDuringCompose); + Assert.Equal(1, gate.CurrentCount); + } + + [Fact] + public void TryCompose_PassesRejectionThrough() + { + using var gate = new SemaphoreSlim(1, 1); + var inner = new GateObservingCompositor(gate, result: false); + var compositor = new SynchronizedCompositor(inner, gate); + + Assert.False(compositor.TryCompose(new byte[4], 1, 1, new byte[4], 1, 1, Command)); + Assert.Equal(1, gate.CurrentCount); + } + + [Fact] + public void TryCompose_ReleasesGateWhenInnerThrows() + { + using var gate = new SemaphoreSlim(1, 1); + var compositor = new SynchronizedCompositor(new ThrowingCompositor(), gate); + + Assert.Throws(() => + compositor.TryCompose(new byte[4], 1, 1, new byte[4], 1, 1, Command)); + Assert.Equal(1, gate.CurrentCount); + } + + [Fact] + public void Dispose_DisposesInnerButNotGate() + { + using var gate = new SemaphoreSlim(1, 1); + var inner = new DisposableCompositor(); + var compositor = new SynchronizedCompositor(inner, gate); + + compositor.Dispose(); + + Assert.True(inner.Disposed); + gate.Wait(); + gate.Release(); + } + } +}