public void InvalidBoundSyncedFunc_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.Bind(() => { return(13); }, syncContext, false);
                    context.Reset();
                    int result = action();

                    Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
                }
        }
        public void BoundAsyncAction_Invoked_UsesSyncContext()
        {
            bool sawSync = 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)
                    {
                        OnPost = () => { sawSync = true; },
                        OnSend = () => { sawSync = true; }
                    };

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

                    Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
                }
        }
        public void BoundSyncedFunc_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.Bind(() => { return(13); }, syncContext, false);
                    int result = action();

                    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 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_IncrementsSyncContextOperationCount()
        {
            bool sawOperationStarted = 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)
                    {
                        OnOperationStarted = () => { sawOperationStarted = true; }
                    };

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

                    Assert.IsFalse(sawOperationStarted, "Context incremented operation count");
                }
        }
        public void InvalidBoundSyncedFunc_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.Bind(() => { return 13; }, syncContext, false);
                context.Reset();
                int result = action();

                Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
            }
        }
        public void BoundSyncedFunc_Invoked_IncrementsSyncContextOperationCount()
        {
            bool sawOperationStarted = 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)
                {
                    OnOperationStarted = () => { sawOperationStarted = true; }
                };

                var action = context.Bind(() => { return 13; }, syncContext, false);
                int result = action();

                Assert.IsFalse(sawOperationStarted, "Context incremented operation count");
            }
        }
        public void BoundSyncedAction_Invoked_UsesSyncContext()
        {
            bool sawSync = 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)
                {
                    OnPost = () => { sawSync = true; },
                    OnSend = () => { sawSync = true; }
                };

                var action = context.Bind(() => { }, syncContext, false);
                action();

                Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
            }
        }