public void FailingAction_BeginInvokedThroughActionThreadGSO_ThrowsTargetInvocationException()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

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

                IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { throw new Exception(); }), null);
                test.EndInvoke(result);
            }
        }
        public void Action_BeginInvokeThroughActionThreadGSO_RunsOnTheActionThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

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

                int          actionThreadId = Thread.CurrentThread.ManagedThreadId;
                IAsyncResult result         = test.BeginInvoke((MethodInvoker)(() => { actionThreadId = Thread.CurrentThread.ManagedThreadId; }), null);
                test.EndInvoke(result);

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

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

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

                IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                test.EndInvoke(result);

                Assert.IsTrue(sawAction, "GenericSynchronizingObject.BeginInvoke did not execute action");
            }
        }
        public void Action_BeginInvokeThroughGSO_ReceivesParameters()
        {
            object parameter = new object();
            object argument  = null;

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

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

                IAsyncResult result = test.BeginInvoke((Action <object>)((arg) => { argument = arg; }), new [] { parameter });
                test.EndInvoke(result);

                Assert.AreSame(parameter, argument, "GenericSynchronizingObject.BeginInvoke did not pass parameter");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }
        public void FailingAction_BeginInvokedThroughActionThreadGSO_PreservesExceptionAsInnerException()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

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

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

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

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                    {
                        using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                        {
                            GenericSynchronizingObject test = new GenericSynchronizingObject();
                            IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                            test.EndInvoke(result);
                        }
                    });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }