public void BehaviorManager_MultipleWorflows()
        {
            using (Sequence.Create())
            {
                //setup
                var             b1          = new Mock <IBehavior>();
                var             w1          = new Mock <IWorkflowB1>();
                var             w1_same     = new Mock <IWorkflowB1>();
                var             w2          = new Mock <IWorkflowB2>();
                var             w3          = new Mock <IWorkflowB3>();
                WorkflowManager w1_end      = null;
                WorkflowManager w1_same_end = null;
                WorkflowManager w3_end      = null;
                b1.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                w1.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                w1.Setup(i => i.StartWorkflow(It.IsAny <WorkflowManager>())).Callback((WorkflowManager i) => w1_end = i);
                w1.Setup(i => i.Disable()).InSequence();
                w1.Setup(i => i.StopWorkflow()).InSequence();
                w1_same.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                w1_same.Setup(i => i.StartWorkflow(It.IsAny <WorkflowManager>())).Callback((WorkflowManager i) => w1_same_end = i);
                w2.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                w3.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                w2.Setup(i => i.Disable()).InSequence();
                w1_same.Setup(i => i.Disable()).InSequence();
                w1_same.Setup(i => i.StopWorkflow()).InSequence();
                w3.Setup(i => i.StartWorkflow(It.IsAny <WorkflowManager>())).Callback((WorkflowManager i) => w3_end = i);
                b1.Setup(i => i.Disable()).InSequence();
                w3.Setup(i => i.Disable()).InSequence();
                w3.Setup(i => i.StopWorkflow()).InSequence();
                //never called
                w2.Setup(i => i.StartWorkflow(It.IsAny <WorkflowManager>())).InSequence(Times.Never());
                w2.Setup(i => i.StopWorkflow()).InSequence(Times.Never());

                //test
                var behaviors = new BehaviorManager(null);
                behaviors.Add(b1.Object);
                behaviors.Add(w1.Object);
                behaviors.Add(w1_same.Object);
                behaviors.Add(w2.Object);
                behaviors.Add(w3.Object);
                behaviors.Remove(w2.Object);
                w1_same_end.EndWorkflow();
                behaviors.Remove(b1.Object);
                behaviors.Remove(w3.Object);
            }
        }
        public void BehaviorManager_MultipleBehavior()
        {
            using (Sequence.Create())
            {
                //setup
                var b1 = new Mock <IBehavior>();
                var b2 = new Mock <IBehavior2>();
                b1.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                b2.Setup(i => i.Enable(It.IsAny <Arm>())).InSequence();
                b1.Setup(i => i.Disable()).InSequence();
                b2.Setup(i => i.Disable()).InSequence();

                //test
                var behaviors = new BehaviorManager(null);
                behaviors.Add(b1.Object);
                behaviors.Add(b2.Object);
                behaviors.Remove(b1.Object);
                behaviors.Remove(b2.Object);
            }
        }
Esempio n. 3
0
        public void TestRapidAddRemoveIterateAcrossThreads()
        {
            // Start with a bunch of behaviors already collected, to work with.
            for (int i = 0; i < 100; i++)
            {
                behaviorManager.Add(new SimpleTestBehavior());
            }

            // Prepare stress threads to determine if BehaviorManager is safe from simultaneous add/remove/iterate attempts.
            DateTime endTime      = DateTime.Now.AddMilliseconds(100);
            Action   addAction    = () => behaviorManager.Add(new SimpleTestBehavior());
            Action   removeAction = () =>
            {
                var toRemove = behaviorManager.FindFirst <SimpleTestBehavior>();
                if (toRemove != null)
                {
                    behaviorManager.Remove(toRemove);
                }
            };
            Action iterateAction = () =>
            {
                foreach (var behavior in behaviorManager.AllBehaviors)
                {
                    // Do nothing; just iterate.
                }
            };
            Action typedAction = () =>
            {
                foreach (var behavior in behaviorManager.OfType <SimpleTestBehavior>())
                {
                    // Do nothing; just iterate.
                }
            };
            var addThread     = new BackgroundStressTestThread(endTime, addAction);
            var removeThread  = new BackgroundStressTestThread(endTime, removeAction);
            var iterateThread = new BackgroundStressTestThread(endTime, iterateAction);
            var typedThread   = new BackgroundStressTestThread(endTime, typedAction);

            addThread.Join();
            removeThread.Join();
            iterateThread.Join();
            typedThread.Join();
            Assert.IsNull(addThread.Exception, "Add: " + addThread.ExceptionTestMessage);
            Assert.IsNull(removeThread.Exception, "Remove: " + removeThread.ExceptionTestMessage);
            Assert.IsNull(iterateThread.Exception, "Iterate: " + iterateThread.ExceptionTestMessage);
            Assert.IsNull(typedThread.Exception, "OfType: " + typedThread.ExceptionTestMessage);
        }