public void TestCallAwaitable() { TestSynchronizationConext testSynchronizationConext = new TestSynchronizationConext(); SynchronizationContext.SetSynchronizationContext(testSynchronizationConext); OcDispatcher dispatcher = new OcDispatcher(2); int count = 1; ManualResetEventSlim mres = new ManualResetEventSlim(false); object context = new object(); Invocation parent; dispatcher.InvokeAsync(() => { parent = dispatcher.CurrentInvocation; dispatcher.Invoke(async() => { Assert.AreEqual(count, 1); count++; Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 1); Assert.AreEqual(dispatcher.CurrentInvocation.Context, context); Assert.AreEqual(dispatcher.CurrentInvocation.Parent, parent); OcDispatcherSynchronizationContext synchronizationContext = (OcDispatcherSynchronizationContext)SynchronizationContext.Current; Assert.AreEqual(synchronizationContext.Priority, 1); Assert.AreEqual(synchronizationContext.Context, context); Assert.AreEqual(synchronizationContext.ParentInvocation, parent); await Task.Run(() => count++); Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 1); Assert.AreEqual(dispatcher.CurrentInvocation.Context, context); Assert.AreEqual(dispatcher.CurrentInvocation.Parent, parent); count++; mres.Set(); return(1); }, 1, context, true); }); mres.Wait(); mres.Dispose(); Assert.AreEqual(count, 4); dispatcher.Dispose(); }
public void TestInvokeAsyncAwaitableFuncStateNonDispatcherThread() { TestSynchronizationConext testSynchronizationConext = new TestSynchronizationConext(); SynchronizationContext.SetSynchronizationContext(testSynchronizationConext); OcDispatcher dispatcher = new OcDispatcher(2); int count = 0; ManualResetEventSlim mres = new ManualResetEventSlim(false); object context = new object(); async void test() { count++; int count1 = await dispatcher.InvokeAsyncAwaitable(state => { Assert.AreEqual(count, 1); count++; Assert.AreEqual(dispatcher.CurrentInvocation.Priority, 1); Assert.AreEqual(dispatcher.CurrentInvocation.Context, context); return(count); }, new object(), 1, context); Assert.AreEqual(count1, 2); Assert.AreEqual(count, 2); count++; Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, dispatcher.ManagedThreadId); Assert.AreEqual(dispatcher.CurrentInvocation, null); mres.Set(); } test(); mres.Wait(); mres.Dispose(); Assert.AreEqual(count, 3); dispatcher.Dispose(); }