public void InvalidBoundAsyncAction_Invoked_DoesSync()
        {
            bool sawSync = false;

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

                    // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var syncContext = new Util.LoggingSynchronizationContext(actionThreadSyncContext)
                    {
                        OnPost = () => { sawSync = true; },
                        OnSend = () => { sawSync = true; }
                    };

                    var action = context.AsyncBind(() => { }, syncContext, false);
                    context.Reset();
                    action();
                    thread.Join();

                    Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
                }
        }
        public void BoundAsyncAction_Invoked_ExecutesSynchronized()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

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

                    // Capture the thread's SynchronizationContext
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var action = context.AsyncBind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; }, actionThreadSyncContext);
                    action();
                    thread.Join();

                    Assert.AreEqual(thread.ManagedThreadId, sawActionThread, "Bound action was not synchronized");
                }
        }
        public void InvalidBoundAsyncAction_Invoked_DoesNotExecute()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

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

                    // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var action = context.AsyncBind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; }, actionThreadSyncContext, false);
                    context.Reset();
                    action();
                    thread.Join();

                    Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, sawActionThread, "Invalid action should not run");
                }
        }
        public void BoundAsyncAction_Invoked_DecrementsSyncContextOperationCount()
        {
            bool sawOperationCompleted = false;

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

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnOperationCompleted = () => { sawOperationCompleted = true; }
                };

                var action = context.AsyncBind(() => { }, syncContext, false);
                action();
                thread.Join();

                Assert.IsFalse(sawOperationCompleted, "Context decremented operation count");
            }
        }
        public void BoundAsyncAction_Invoked_DecrementsSyncContextOperationCount()
        {
            bool sawOperationCompleted = false;

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

                    // Capture the thread's SynchronizationContext
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var syncContext = new Util.LoggingSynchronizationContext(actionThreadSyncContext)
                    {
                        OnOperationCompleted = () => { sawOperationCompleted = true; }
                    };

                    var action = context.AsyncBind(() => { }, syncContext, false);
                    action();
                    thread.Join();

                    Assert.IsFalse(sawOperationCompleted, "Context decremented operation count");
                }
        }
        public void BoundAsyncAction_Invoked_ExecutesSynchronized()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

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

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var action = context.AsyncBind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; }, actionThreadSyncContext);
                action();
                thread.Join();

                Assert.AreEqual(thread.ManagedThreadId, sawActionThread, "Bound action was not synchronized");
            }
        }
        public void InvalidBoundAsyncAction_Invoked_DoesSync()
        {
            bool sawSync = false;

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

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnPost = () => { sawSync = true; },
                    OnSend = () => { sawSync = true; }
                };

                var action = context.AsyncBind(() => { }, syncContext, false);
                context.Reset();
                action();
                thread.Join();

                Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
            }
        }
        public void InvalidBoundAsyncAction_Invoked_DoesNotExecute()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

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

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var action = context.AsyncBind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; }, actionThreadSyncContext, false);
                context.Reset();
                action();
                thread.Join();

                Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, sawActionThread, "Invalid action should not run");
            }
        }