示例#1
0
        public void TaskEnv_invoke_with_custom_dispatchqueue_sets_task_state()
        {
            var dispatchQueue = new TestDispatchQueue();

            _log.Debug("setting up envs");
            var   currentState = new State();
            State newState     = null;

            TaskEnv.Current.SetState(currentState);
            var  copiedEnv  = TaskEnv.Clone(dispatchQueue);
            var  newEnv     = TaskEnv.New(dispatchQueue);
            var  resetEvent = new AutoResetEvent(false);
            bool?hasState   = null;

            // Note: have to over acquire otherwise env is wiped after invokenow
            copiedEnv.Acquire();
            copiedEnv.Acquire();
            copiedEnv.Invoke(() => {
                _log.Debug("copied env invoke");
                hasState = currentState == TaskEnv.Current.GetState <State>();
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsTrue(hasState.Value);
            hasState = null;

            // Note: have to over acquire otherwise env is wiped after invokenow
            newEnv.Acquire();
            newEnv.Acquire();
            newEnv.Acquire();
            newEnv.Invoke(() => {
                _log.Debug("new env invoke");
                hasState = currentState == TaskEnv.Current.GetState <State>();
                newState = new State();
                TaskEnv.Current.SetState(newState);
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsFalse(hasState.Value);
            Assert.IsNotNull(newState);
            Assert.AreEqual(currentState, TaskEnv.Current.GetState <State>());
            Assert.AreNotEqual(currentState, newState);
            hasState = null;
            newEnv.Invoke(() => {
                _log.Debug("new env invoke 2");
                hasState = newState == TaskEnv.Current.GetState <State>();
                newEnv   = TaskEnv.Current;
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsTrue(hasState.Value);
        }
示例#2
0
文件: TaskTests.cs 项目: bjorg/DReAM
        public void TaskEnv_invoke_with_custom_dispatchqueue_sets_task_state()
        {
            var dispatchQueue = new TestDispatchQueue();
            _log.Debug("setting up envs");
            var currentState = new State();
            State newState = null;
            TaskEnv.Current.SetState(currentState);
            var copiedEnv = TaskEnv.Clone(dispatchQueue);
            var newEnv = TaskEnv.New(dispatchQueue);
            var resetEvent = new AutoResetEvent(false);
            bool? hasState = null;

            // Note: have to over acquire otherwise env is wiped after invokenow
            copiedEnv.Acquire();
            copiedEnv.Acquire();
            copiedEnv.Invoke(() => {
                _log.Debug("copied env invoke");
                hasState = currentState == TaskEnv.Current.GetState<State>();
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsTrue(hasState.Value);
            hasState = null;

            // Note: have to over acquire otherwise env is wiped after invokenow
            newEnv.Acquire();
            newEnv.Acquire();
            newEnv.Acquire();
            newEnv.Invoke(() => {
                _log.Debug("new env invoke");
                hasState = currentState == TaskEnv.Current.GetState<State>();
                newState = new State();
                TaskEnv.Current.SetState(newState);
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsFalse(hasState.Value);
            Assert.IsNotNull(newState);
            Assert.AreEqual(currentState, TaskEnv.Current.GetState<State>());
            Assert.AreNotEqual(currentState, newState);
            hasState = null;
            newEnv.Invoke(() => {
                _log.Debug("new env invoke 2");
                hasState = newState == TaskEnv.Current.GetState<State>();
                newEnv = TaskEnv.Current;
                resetEvent.Set();
            });
            resetEvent.WaitOne();
            dispatchQueue.LastItem.Wait();
            Assert.IsTrue(hasState.HasValue);
            Assert.IsTrue(hasState.Value);
        }