using System; using NUnit.Framework; using ShrinkEventBus; [TestFixture, NonParallelizable] public class EventBusAllocationTests { private readonly struct ProbeEvent : IShrinkEvent { } [Test] public void PublicDiagnosticsDoesNotAllocatePerPostAfterWarmup() { using var host = new ShrinkEventBusHost(); var bus = host.CreateBus(new ShrinkBusKey("allocation", "probe"), ShrinkBusOptions.Inline()); var calls = 0; using var observer = EventBus.ObserveDiagnostics(_ => calls++); Measure(bus); var allocated = Measure(bus); Assert.That(calls, Is.EqualTo(20000)); Assert.That(allocated, Is.Zero); } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] private static long Measure(IShrinkEventBus bus) { var before = GC.GetAllocatedBytesForCurrentThread(); for (var i = 0; i < 10000; i++) bus.Post(new ProbeEvent()); return GC.GetAllocatedBytesForCurrentThread() - before; } }