public void DoerTest() { OcConfiguration.SaveOcDispatcherInvocationExecutionStackTrace = true; OcDispatcher dispatcher = new OcDispatcher(); ManualResetEventSlim mre = new ManualResetEventSlim(false); dispatcher.InvokeAsync(() => { mre.Wait(); }); Invocation doer = dispatcher.InvokeAsync(() => { dispatcher.Pass(); }); Invocation invocation = dispatcher.InvokeAsync(() => {}); mre.Set(); dispatcher.Pass(); Assert.AreEqual(invocation.Executor, doer); Assert.IsNotNull(invocation.ExecutionStackTrace); dispatcher.Dispose(); }
public void TestInvocationBehaviourIgnore() { OcDispatcher dispatcher = new OcDispatcher(); dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Cancel; bool called = false; dispatcher.Invoke(() => { called = true; }); dispatcher.Invoke(s => { called = true; }, new object()); dispatcher.InvokeAsync(() => { called = true; }); dispatcher.InvokeAsync(s => { called = true; }, new object()); dispatcher.Invoke(() => { called = true; return(0); }); dispatcher.Invoke(s => { called = true; return(0); }, new object()); dispatcher.InvokeAsync(() => { called = true; return(0); }); dispatcher.InvokeAsync(s => { called = true; return(0); }, new object()); dispatcher.InvokeAsyncAwaitable(() => { called = true; }); dispatcher.InvokeAsyncAwaitable(s => { called = true; }, new object()); dispatcher.InvokeAsyncAwaitable(() => { called = true; return(0); }); dispatcher.InvokeAsyncAwaitable(s => { called = true; return(0); }, new object()); dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Accept; dispatcher.Pass(); Assert.IsFalse(called); dispatcher.Dispose(); }
public void TestFuncAsync() { OcDispatcher dispatcher = new OcDispatcher(2); object returnObject = new object(); InvocationResult <object> invocationResult = dispatcher.InvokeAsync(() => returnObject); dispatcher.Pass(); Assert.AreEqual(invocationResult.Value, invocationResult.Value); Assert.AreEqual(invocationResult.ToString(), "(ObservableComputations.InvocationResult<Object> (Value = '(System.Object)'))"); dispatcher.Dispose(); }
public void TestFuncAsyncState() { OcDispatcher dispatcher = new OcDispatcher(2); TestValue returnObject = new TestValue(); InvocationResult <TestValue> invocationResult = dispatcher.InvokeAsync(s => returnObject, new object()); bool propertyChanged = false; invocationResult.PropertyChanged += (sender, args) => propertyChanged = true; dispatcher.Pass(); Assert.AreEqual(invocationResult.Value, returnObject); Assert.IsTrue(propertyChanged); Assert.AreEqual(invocationResult.ToString(), "(ObservableComputations.InvocationResult<TestValue> (Value = 'Mesasage'))"); Assert.AreEqual(invocationResult.Invocation.Status, InvocationStatus.Executed); dispatcher.Dispose(); }
public void TestInvocationBehaviourThrowException() { OcDispatcher dispatcher = new OcDispatcher(); dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.ThrowException; bool called = false; Exception exception; void invoke(Action action) { exception = null; try { action(); } catch (Exception e) { exception = e; } Assert.IsTrue(exception != null); } invoke(() => dispatcher.Invoke(() => { called = true; })); invoke(() => dispatcher.Invoke(s => { called = true; }, new object())); invoke(() => dispatcher.InvokeAsync(() => { called = true; })); invoke(() => dispatcher.InvokeAsync(s => { called = true; }, new object())); invoke(() => dispatcher.Invoke(() => { called = true; return(0); })); invoke(() => dispatcher.Invoke(s => { called = true; return(0); }, new object())); invoke(() => dispatcher.InvokeAsync(() => { called = true; return(0); })); invoke(() => dispatcher.InvokeAsync(s => { called = true; return(0); }, new object())); dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Accept; dispatcher.Pass(); Assert.IsFalse(called); dispatcher.Dispose(); }
public void TestDebugInfoState( [Values(true, false)] bool debug, [Values(true, false)] bool secondAsync) { OcConfiguration.SaveOcDispatcherInvocationInstantiationStackTrace = debug; object context = new object(); object state = new object(); OcDispatcher dispatcher = new OcDispatcher(2); Action <object> action = null; Invocation parentInvocation; action = s => { Assert.AreEqual(s, state); Assert.IsTrue(dispatcher.CheckAccess()); Assert.AreEqual(OcDispatcher.Current, dispatcher); parentInvocation = dispatcher.CurrentInvocation; Assert.AreEqual(parentInvocation.Parent, null); Assert.AreEqual(parentInvocation.State, state); Assert.AreEqual(parentInvocation.Action, null); Assert.AreEqual(parentInvocation.ActionWithState, action); if (debug) { Assert.IsTrue(parentInvocation.InstantiationStackTrace != null); } Assert.AreEqual(parentInvocation.Context, context); Assert.AreEqual(parentInvocation.OcDispatcher, dispatcher); Assert.AreEqual(parentInvocation.Priority, 1); object state1 = new object(); Func <object, int> func1 = null; func1 = s1 => { Assert.IsTrue(dispatcher.CheckAccess()); Assert.AreEqual(OcDispatcher.Current, dispatcher); Invocation invocation1 = dispatcher.CurrentInvocation; Assert.AreEqual(invocation1.Parent, parentInvocation); Assert.AreEqual(invocation1.State, state1); Assert.AreEqual(invocation1.Action, null); if (debug) { Assert.IsTrue(invocation1.InstantiationStackTrace != null); } Assert.AreEqual(invocation1.Context, null); Assert.AreEqual(invocation1.OcDispatcher, dispatcher); Assert.AreEqual(invocation1.Priority, 0); return(3); }; if (secondAsync) { dispatcher.InvokeAsync(func1, state1).PropertyChanged += (sender, args) => { if (args.PropertyName == "Value") { Assert.AreEqual(((InvocationResult <int>)sender).Value, 3); } }; } else { Assert.AreEqual(dispatcher.Invoke(func1, state1).Value, 3); } }; Assert.IsFalse(dispatcher.CheckAccess()); dispatcher.Invoke(action, state, 1, context); dispatcher.Pass(); dispatcher.Pass(); Assert.AreEqual(dispatcher, StaticInfo.OcDispatchers[dispatcher.ManagedThreadId]); Assert.IsTrue(dispatcher.CurrentInvocation == null); Assert.IsTrue(OcDispatcher.Current == null); dispatcher.Dispose(); }
public void TestExecuteOthers([Values(1, 2, 3, 4)] int mode) { OcDispatcher dispatcher = new OcDispatcher(2); ManualResetEventSlim mres = new ManualResetEventSlim(); bool done = false; object context = new object(); dispatcher.InvokeAsync(() => { mres.Wait(); Assert.AreEqual(done, false); switch (mode) { case 1: dispatcher.ExecuteOtherInvocations(); break; case 2: dispatcher.ExecuteOtherInvocations(1); break; case 3: dispatcher.ExecuteOtherInvocations(TimeSpan.FromSeconds(1)); break; case 4: dispatcher.ExecuteOtherInvocations((count, invocation) => invocation.Context == context); break; } Assert.AreEqual(done, true); }); dispatcher.InvokeAsync(() => { done = true; }, 0, context); mres.Set(); dispatcher.Pass(); Exception exception = null; try { switch (mode) { case 1: dispatcher.ExecuteOtherInvocations(); break; case 2: dispatcher.ExecuteOtherInvocations(1); break; case 3: dispatcher.ExecuteOtherInvocations(TimeSpan.FromSeconds(1)); break; case 4: dispatcher.ExecuteOtherInvocations((count, invocation) => invocation.Context == context); break; } } catch (Exception e) { exception = e; } Assert.IsNotNull(exception); dispatcher.Dispose(); }