Esempio n. 1
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);
        }