44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
#if SHRINK_EVENTBUS_HAS_SHARED_RUNTIME
|
|
using System;
|
|
using NUnit.Framework;
|
|
using ShrinkSDK.Runtime;
|
|
|
|
namespace ShrinkEventBus.Integration.Tests
|
|
{
|
|
public sealed class SharedDispatcherTests
|
|
{
|
|
private sealed class Dispatcher : IShrinkMainThreadDispatcher
|
|
{
|
|
public bool IsMainThread => false;
|
|
public Action Pending;
|
|
public bool TryPost(Action action) { Pending = action; return true; }
|
|
}
|
|
|
|
[Test]
|
|
public void AdapterPreservesAffinityAndPostsThroughSharedDispatcher()
|
|
{
|
|
var original = ShrinkEventBusRuntime.MainThreadDispatcher;
|
|
try
|
|
{
|
|
var dispatcher = new Dispatcher();
|
|
var adapter = new ShrinkSharedMainThreadDispatcher(dispatcher);
|
|
ShrinkEventBusRuntime.ConfigureMainThreadDispatcher(adapter);
|
|
Assert.IsFalse(ShrinkEventBusRuntime.MainThreadDispatcher.IsMainThread);
|
|
var hits = 0;
|
|
Assert.IsTrue(ShrinkEventBusRuntime.MainThreadDispatcher.TryPost(() => hits++));
|
|
Assert.AreEqual(0, hits);
|
|
dispatcher.Pending();
|
|
Assert.AreEqual(1, hits);
|
|
}
|
|
finally { ShrinkEventBusRuntime.ConfigureMainThreadDispatcher(original); }
|
|
}
|
|
|
|
[Test]
|
|
public void AdapterRejectsNullDispatcher()
|
|
{
|
|
Assert.Throws<ArgumentNullException>(() => new ShrinkSharedMainThreadDispatcher(null));
|
|
}
|
|
}
|
|
}
|
|
#endif
|