using XFE.SeAgent.Plugin.Graphics; namespace XFE.SeAgent.Tests; internal static partial class Program { private sealed class SnapshotFixture { public int Value; public int[] Data = new int[64]; } private static async Task TestSnapshotExchangeAsync() { var exchange=new SnapshotExchange(); var first=exchange.AcquireWritable(); first.Value=7; exchange.Publish(first); var held=exchange.AcquireRead(); var objects=new HashSet { first }; for(int i=0;i<100;i++) { var write=exchange.AcquireWritable(); objects.Add(write); write.Value=i; exchange.Publish(write); } Check(held.Value==7 && objects.Count==3,"triple snapshot buffers never overwrite a held render frame and remain bounded"); exchange.Clear(); Check(exchange.Current==null && held.Value==7,"world reset drops published data while an outstanding render lease stays valid"); exchange.ReleaseRead(held); int finished=0,reads=0; using var started=new ManualResetEventSlim(); var reader=Task.Run(()=> { started.Set(); do { var snapshot=exchange.AcquireRead(); try { if(snapshot!=null) { int expected=snapshot.Value; foreach(int value in snapshot.Data) if(value!=expected) throw new Exception("Torn scene snapshot."); Thread.SpinWait(100); reads++; if(snapshot.Value!=expected) throw new Exception("Leased scene was mutated."); } } finally { exchange.ReleaseRead(snapshot); } } while(Volatile.Read(ref finished)==0); }); started.Wait(); for(int i=1;i<=10000;i++) { var write=exchange.AcquireWritable();write.Value=i;Array.Fill(write.Data,i);exchange.Publish(write); if(i%64==0) Thread.Yield(); } Volatile.Write(ref finished,1); await reader.WaitAsync(TimeSpan.FromSeconds(5)); Check(reads>0 && exchange.Current.Value==10000,"concurrent scene publication preserves coherent data and the latest snapshot"); } }