public void FailingAction_InvokedThroughThreadPoolGSO_ThrowsTargetInvocationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         test.Invoke((MethodInvoker)(() => { throw new MyException(); }), null);
     }
 }
        public void ThreadPoolGSOFromNullSyncContext_Invoked_ExecutesSynchronously()
        {
            int threadId = ~Thread.CurrentThread.ManagedThreadId;

            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();
                test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null);
            }

            Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously");
        }
        public void FailingAction_InvokedThroughActionThreadGSO_ThrowsTargetInvocationException()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); });

                test.Invoke((MethodInvoker)(() => { throw new Exception(); }), null);
            }
        }
        public void Action_InvokedThroughActionThreadGSO_RunsOnTheActionThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); });

                int actionThreadId = Thread.CurrentThread.ManagedThreadId;
                test.Invoke((MethodInvoker)(() => { actionThreadId = Thread.CurrentThread.ManagedThreadId; }), null);

                Assert.AreEqual(thread.ManagedThreadId, actionThreadId, "GenericSynchronizingObject.Invoke did not synchronize");
            }
        }
        public void Action_InvokedThroughActionThreadGSO_Runs()
        {
            bool sawAction = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); });

                test.Invoke((MethodInvoker)(() => { sawAction = true; }), null);

                Assert.AreEqual(true, sawAction, "GenericSynchronizingObject.Invoke did not execute action");
            }
        }
        public void Action_InvokedThroughActionThreadGSO_ReceivesParameters()
        {
            object parameter = new object();
            object argument  = null;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); });

                test.Invoke((Action <object>)((arg) => { argument = arg; }), new [] { parameter });

                Assert.AreSame(parameter, argument, "GenericSynchronizingObject.Invoke did not pass parameter");
            }
        }
        public void FailingAction_InvokedThroughThreadPoolGSO_PreservesExceptionAsInnerException()
        {
            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();

                Exception errorToThrow     = new MyException();
                Exception innerErrorCaught = null;
                try
                {
                    test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null);
                }
                catch (TargetInvocationException ex)
                {
                    innerErrorCaught = ex.InnerException;
                }

                Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved");
            }
        }
        public void FailingAction_InvokedThroughActionThreadGSO_PreservesExceptionAsInnerException()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); });

                Exception errorToThrow     = new MyException();
                Exception innerErrorCaught = null;
                try
                {
                    test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null);
                }
                catch (TargetInvocationException ex)
                {
                    innerErrorCaught = ex.InnerException;
                }

                Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved");
            }
        }
        public void ThreadPoolGSO_Invoked_ExecutesSynchronously()
        {
            int threadId = ~Thread.CurrentThread.ManagedThreadId;

            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();
                test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null);
            }

            Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously");
        }
 public void FailingAction_InvokedThroughThreadPoolGSO_ThrowsTargetInvocationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         test.Invoke((MethodInvoker)(() => { throw new MyException(); }), null);
     }
 }
        public void FailingAction_InvokedThroughThreadPoolGSO_PreservesExceptionAsInnerException()
        {
            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();

                Exception errorToThrow = new MyException();
                Exception innerErrorCaught = null;
                try
                {
                    test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null);
                }
                catch (TargetInvocationException ex)
                {
                    innerErrorCaught = ex.InnerException;
                }

                Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved");
            }
        }