using System; using System.Threading; using System.Threading.Tasks; using ShrinkEventBus; internal readonly struct PingEvent : IShrinkEvent { public PingEvent(int value) => Value = value; public int Value { get; } } [ShrinkEventSubscriber] internal sealed partial class SmokeSubscriber { public int Sum { get; private set; } public int ThreadId { get; private set; } [ShrinkSubscribe] private void OnPing(PingEvent evt) { Sum += evt.Value; ThreadId = Thread.CurrentThread.ManagedThreadId; } } [ShrinkEventSubscriber(DefaultBus = "game")] internal static partial class StaticSmokeSubscriber { public static int Sum { get; set; } [ShrinkSubscribe] private static void OnPing(PingEvent evt) => Sum += evt.Value; } internal static class Program { private static async Task Main() { using var host = new ShrinkEventBusHost(); var game = host.CreateBus(ShrinkBusKey.Game, ShrinkBusOptions.Inline()); var workerKey = new ShrinkBusKey("worker", "smoke"); var worker = host.CreateBus(workerKey, ShrinkBusOptions.DedicatedThread()); var gameSubscriber = new SmokeSubscriber(); var workerSubscriber = new SmokeSubscriber(); var callerThread = Thread.CurrentThread.ManagedThreadId; using var gameBinding = host.Attach(gameSubscriber, ShrinkBusKey.Game); using var workerBinding = host.Attach(workerSubscriber, workerKey); game.Post(new PingEvent(2)); await worker.PostAsync(new PingEvent(3)); if (gameSubscriber.Sum != 2 || workerSubscriber.Sum != 3 || StaticSmokeSubscriber.Sum != 2 || workerSubscriber.ThreadId == callerThread) { Console.Error.WriteLine("FAIL"); return 1; } Console.WriteLine("PASS"); return 0; } }