Ejemplo n.º 1
0
        public void PumpSynchronousAndAsynchronousEvents()
        {
            EventQueue q = new EventQueue();

            using (EventPump pump = new EventPump(TestListener.NULL, q))
            {
                pump.Name = "PumpSynchronousAndAsynchronousEvents";
                pump.Start();

                int       numberOfAsynchronousEvents   = 0;
                int       sumOfAsynchronousQueueLength = 0;
                const int Repetitions = 2;
                for (int i = 0; i < Repetitions; i++)
                {
                    foreach (Event e in events)
                    {
                        q.Enqueue(e);

                        sumOfAsynchronousQueueLength += q.Count;
                        numberOfAsynchronousEvents++;
                    }
                }

                Console.WriteLine("Average queue length: {0}", (float)sumOfAsynchronousQueueLength / numberOfAsynchronousEvents);
            }
        }
Ejemplo n.º 2
0
        public void EventPumpQueueLength(int numberOfProducers, bool producerDelay)
        {
            EventQueue q = new EventQueue();

            EventProducer[] producers = new EventProducer[numberOfProducers];
            for (int i = 0; i < numberOfProducers; i++)
            {
                producers[i] = new EventProducer(q, i, producerDelay);
            }

            using (EventPump pump = new EventPump(TestListener.NULL, q))
            {
                pump.Name = "EventPumpQueueLength";
                pump.Start();

                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Start();
                }
                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Join();
                }
                pump.Stop();
            }
            Assert.That(q.Count, Is.EqualTo(0));

            foreach (EventProducer p in producers)
            {
                Console.WriteLine("#Events: {0}, MaxQueueLength: {1}", p.SentEventsCount, p.MaxQueueLength);
                Assert.IsNull(p.Exception, "{0}", p.Exception);
            }
        }
Ejemplo n.º 3
0
        public void TracingEventListenerDoesNotDeadlock()
        {
            QueuingEventListener upstreamListener      = new QueuingEventListener();
            EventQueue           upstreamListenerQueue = upstreamListener.Events;

            // Install a TraceListener sending TestOutput events to the upstreamListener.
            // This simulates RemoteTestRunner.StartTextCapture, where TestContext installs such a TraceListener.
            TextWriter    traceWriter       = new EventListenerTextWriter(upstreamListener, TestOutputType.Trace);
            const string  TraceListenerName = "TracingEventListenerDoesNotDeadlock";
            TraceListener feedingTraceToUpstreamListener = new TextWriterTraceListener(traceWriter, TraceListenerName);

            try
            {
                Trace.Listeners.Add(feedingTraceToUpstreamListener);

                // downstreamListenerToTrace simulates an EventListener installed e.g. by an Addin,
                // which may call Trace within the EventListener methods:
                TracingEventListener downstreamListenerToTrace = new TracingEventListener();
                using (EventPump pump = new EventPump(downstreamListenerToTrace, upstreamListenerQueue))
                {
                    pump.Name = "TracingEventListenerDoesNotDeadlock";
                    pump.Start();

                    const int Repetitions = 10;
                    for (int i = 0; i < Repetitions; i++)
                    {
                        foreach (Event e in events)
                        {
                            Trace.WriteLine("Before sending {0} event.", e.GetType().Name);
                            e.Send(upstreamListener);
                            Trace.WriteLine("After sending {0} event.", e.GetType().Name);
                        }
                    }
                }
            }
            finally
            {
                Trace.Listeners.Remove(TraceListenerName);
                feedingTraceToUpstreamListener.Dispose();
            }
        }
Ejemplo n.º 4
0
 private static void StartPump(EventPump pump, int waitTime)
 {
     pump.Start();
     WaitForPumpToStart(pump, waitTime);
 }
Ejemplo n.º 5
0
        public void PumpSynchronousAndAsynchronousEvents()
        {
            EventQueue q = new EventQueue();
            using (EventPump pump = new EventPump(TestListener.NULL, q))
            {
                pump.Name = "PumpSynchronousAndAsynchronousEvents";
                pump.Start();

                int numberOfAsynchronousEvents = 0;
                int sumOfAsynchronousQueueLength = 0;
                const int Repetitions = 2;
                for (int i = 0; i < Repetitions; i++)
                {
                    foreach (Event e in events)
                    {
                        q.Enqueue(e);

                        sumOfAsynchronousQueueLength += q.Count;
                        numberOfAsynchronousEvents++;
                    }
                }

                Console.WriteLine("Average queue length: {0}", (float)sumOfAsynchronousQueueLength / numberOfAsynchronousEvents);
            }
        }
        /// <summary>
        /// Run selected tests and return a test result. The test is run synchronously,
        /// and the listener interface is notified as it progresses.
        /// </summary>
        /// <param name="listener">Interface to receive EventListener notifications.</param>
        /// <param name="filter">A test filter used to select tests to be run</param>
        /// <returns></returns>
        public ITestResult Run(ITestListener listener, ITestFilter filter)
        {
            log.Info("Running tests");
            if (_loadedTest == null)
                throw new InvalidOperationException("Run was called but no test has been loaded.");

            // Save Console.Out and Error for later restoration
            TextWriter savedOut = Console.Out;
            TextWriter savedErr = Console.Error;

            TestExecutionContext initialContext = CreateTestExecutionContext(_settings);

#if NUNITLITE
            initialContext.Listener = listener;

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);
            workItem.Execute();

            _runComplete.WaitOne();

            return workItem.Result;
#else
            QueuingEventListener queue = new QueuingEventListener();

            if (_settings.Contains("CaptureStandardOutput"))
                initialContext.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
            if (_settings.Contains("CapureStandardError"))
                initialContext.Error = new EventListenerTextWriter(queue, TestOutputType.Error);

            initialContext.Listener = queue;

            int numWorkers = _settings.Contains("NumberOfTestWorkers")
                ? (int)_settings["NumberOfTestWorkers"]
                : 0;

            WorkItemDispatcher dispatcher = null;

            if (numWorkers > 0)
            {
                dispatcher = new WorkItemDispatcher(numWorkers);
                initialContext.Dispatcher = dispatcher;
            }

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);

            using (EventPump pump = new EventPump(listener, queue.Events))
            {
                pump.Start();

                if (dispatcher != null)
                {
                    dispatcher.Dispatch(workItem);
                    dispatcher.Start();
                }
                else
                    workItem.Execute();

                _runComplete.WaitOne();
            }

            Console.SetOut(savedOut);
            Console.SetError(savedErr);

            if (dispatcher != null)
            {
                dispatcher.Stop();
                dispatcher = null;
            }

            return workItem.Result;
#endif
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initiate the test run.
        /// </summary>
        public void StartRun(ITestListener listener)
        {
#if !SILVERLIGHT && !NETCF && !PORTABLE
            // Save Console.Out and Error for later restoration
            _savedOut = Console.Out;
            _savedErr = Console.Error;

            Console.SetOut(new TextCapture(Console.Out));
            Console.SetError(new TextCapture(Console.Error));
#endif

#if PARALLEL
            QueuingEventListener queue = new QueuingEventListener();
            Context.Listener = queue;

            _pump = new EventPump(listener, queue.Events);
            _pump.Start();
#else
            Context.Dispatcher = new SimpleWorkItemDispatcher();
#endif

            Context.Dispatcher.Dispatch(TopLevelWorkItem);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initiate the test run.
        /// </summary>
        public void StartRun(ITestListener listener)
        {
#if !SILVERLIGHT && !NETCF && !PORTABLE
            // Save Console.Out and Error for later restoration
            _savedOut = Console.Out;
            _savedErr = Console.Error;

            Console.SetOut(new TextCapture(Console.Out));
            Console.SetError(new TextCapture(Console.Error));
#endif

#if PARALLEL
            // Queue and pump events, unless settings have SynchronousEvents == false
            if (!Settings.Contains(PackageSettings.SynchronousEvents) || !(bool)Settings[PackageSettings.SynchronousEvents])
            {
                QueuingEventListener queue = new QueuingEventListener();
                Context.Listener = queue;

                _pump = new EventPump(listener, queue.Events);
                _pump.Start();
            }
#endif

            if (!System.Diagnostics.Debugger.IsAttached &&
                Settings.Contains(PackageSettings.DebugTests) &&
                (bool)Settings[PackageSettings.DebugTests])
            {
                System.Diagnostics.Debugger.Launch();
            }

            Context.Dispatcher.Dispatch(TopLevelWorkItem);
        }
        /// <summary>
        /// Initiate the test run.
        /// </summary>
        public override void StartRun(ITestListener listener)
        {
            // Save Console.Out and Error for later restoration
            _savedOut = Console.Out;
            _savedErr = Console.Error;

            QueuingEventListener queue = new QueuingEventListener();

            if (Settings.Contains(DriverSettings.CaptureStandardOutput))
                Context.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
            if (Settings.Contains(DriverSettings.CaptureStandardError))
                Context.Error = new EventListenerTextWriter(queue, TestOutputType.Error);

            Context.Listener = queue;

            using (EventPump pump = new EventPump(listener, queue.Events))
            {
                pump.Start();

                Context.Dispatcher.Dispatch(TopLevelWorkItem);
            }
        }
        /// <summary>
        /// Initiate the test run.
        /// </summary>
        public override void StartRun(ITestListener listener)
        {
            // Save Console.Out and Error for later restoration
            _savedOut = Console.Out;
            _savedErr = Console.Error;

            Console.SetOut(new TextCapture(Console.Out));
            Console.SetError(new TextCapture(Console.Error));

            QueuingEventListener queue = new QueuingEventListener();
            Context.Listener = queue;

            _pump = new EventPump(listener, queue.Events);
            _pump.Start();

            Context.Dispatcher.Dispatch(TopLevelWorkItem);
        }
Ejemplo n.º 11
0
        public void EventPumpQueueLength(int numberOfProducers, bool producerDelay)
        {
            EventQueue q = new EventQueue();
            EventProducer[] producers = new EventProducer[numberOfProducers];
            for (int i = 0; i < numberOfProducers; i++)
            {
                producers[i] = new EventProducer(q, i, producerDelay);
            }

            using (EventPump pump = new EventPump(TestListener.NULL, q))
            {
                pump.Name = "EventPumpQueueLength";
                pump.Start();

                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Start();
                }
                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Join();
                }
                pump.Stop();
            }
            Assert.That(q.Count, Is.EqualTo(0));

            foreach (EventProducer p in producers)
            {
                Console.WriteLine(
                    "#Events: {0}, MaxQueueLength: {1}", p.SentEventsCount, p.MaxQueueLength);
                Assert.IsNull(p.Exception, "{0}", p.Exception);
            }
        }
Ejemplo n.º 12
0
        public void TracingEventListenerDoesNotDeadlock()
        {
            QueuingEventListener upstreamListener = new QueuingEventListener();
            EventQueue upstreamListenerQueue = upstreamListener.Events;

            // Install a TraceListener sending TestOutput events to the upstreamListener.
            // This simulates RemoteTestRunner.StartTextCapture, where TestContext installs such a TraceListener.
            TextWriter traceWriter = new EventListenerTextWriter(upstreamListener, TestOutputType.Trace);
            const string TraceListenerName = "TracingEventListenerDoesNotDeadlock";
            TraceListener feedingTraceToUpstreamListener = new TextWriterTraceListener(traceWriter, TraceListenerName);

            try
            {
                Trace.Listeners.Add(feedingTraceToUpstreamListener);

                // downstreamListenerToTrace simulates an EventListener installed e.g. by an Addin, 
                // which may call Trace within the EventListener methods:
                TracingEventListener downstreamListenerToTrace = new TracingEventListener();
                using (EventPump pump = new EventPump(downstreamListenerToTrace, upstreamListenerQueue))
                {
                    pump.Name = "TracingEventListenerDoesNotDeadlock";
                    pump.Start();

                    const int Repetitions = 10;
                    for (int i = 0; i < Repetitions; i++)
                    {
                        foreach (Event e in events)
                        {
                            Trace.WriteLine("Before sending {0} event.", e.GetType().Name);
                            e.Send(upstreamListener);
                            Trace.WriteLine("After sending {0} event.", e.GetType().Name);
                        }
                    }
                }
            }
            finally
            {
                Trace.Listeners.Remove(TraceListenerName);
                feedingTraceToUpstreamListener.Dispose();
            }
        }
        /// <summary>
        /// Run selected tests and return a test result. The test is run synchronously,
        /// and the listener interface is notified as it progresses.
        /// </summary>
        /// <param name="listener">Interface to receive EventListener notifications.</param>
        /// <param name="filter">A test filter used to select tests to be run</param>
        /// <returns></returns>
        public ITestResult Run(ITestListener listener, ITestFilter filter)
        {
            log.Info("Running tests");
            if (_loadedTest == null)
                throw new InvalidOperationException("The Run method was called but no test has been loaded");

            // Save Console.Out and Error for later restoration
            TextWriter savedOut = Console.Out;
            TextWriter savedErr = Console.Error;

            TestExecutionContext initialContext = CreateTestExecutionContext(_settings);

#if NUNITLITE
            initialContext.Listener = listener;

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);
            workItem.Execute();

            _runComplete.WaitOne();

            return workItem.Result;
#else
            QueuingEventListener queue = new QueuingEventListener();

            if (_settings.Contains(DriverSettings.CaptureStandardOutput))
                initialContext.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
            if (_settings.Contains(DriverSettings.CaptureStandardError))
                initialContext.Error = new EventListenerTextWriter(queue, TestOutputType.Error);

            initialContext.Listener = queue;

            int levelOfParallelization = _settings.Contains(DriverSettings.NumberOfTestWorkers)
                ? (int)_settings[DriverSettings.NumberOfTestWorkers]
                : _loadedTest.Properties.ContainsKey(PropertyNames.LevelOfParallelization)
                    ? (int)_loadedTest.Properties.Get(PropertyNames.LevelOfParallelization)
                    : Math.Max(Environment.ProcessorCount, 2);

            WorkItemDispatcher dispatcher = null;

            if (levelOfParallelization > 0)
            {
                dispatcher = new WorkItemDispatcher(levelOfParallelization);
                initialContext.Dispatcher = dispatcher;
                // Assembly does not have IApplyToContext attributes applied
                // when the test is built, so  we do it here.
                // TODO: Generalize this
                if (_loadedTest.Properties.ContainsKey(PropertyNames.ParallelScope))
                    initialContext.ParallelScope = 
                        (ParallelScope)_loadedTest.Properties.Get(PropertyNames.ParallelScope) & ~ParallelScope.Self;
            }

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);

            using (EventPump pump = new EventPump(listener, queue.Events))
            {
                pump.Start();

                if (dispatcher != null)
                {
                    dispatcher.Dispatch(workItem);
                    dispatcher.Start();
                }
                else
                    workItem.Execute();

                _runComplete.WaitOne();
            }

            Console.SetOut(savedOut);
            Console.SetError(savedErr);

            if (dispatcher != null)
            {
                dispatcher.Stop();
                dispatcher = null;
            }

            return workItem.Result;
#endif
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initiate the test run.
        /// </summary>
        private void StartRun(ITestListener listener)
        {
#if !PORTABLE
            // Save Console.Out and Error for later restoration
            _savedOut = Console.Out;
            _savedErr = Console.Error;

            Console.SetOut(new TextCapture(Console.Out));
            Console.SetError(new EventListenerTextWriter("Error", Console.Error));
#endif

#if PARALLEL
            // Queue and pump events, unless settings have SynchronousEvents == false
            if (!Settings.ContainsKey(FrameworkPackageSettings.SynchronousEvents) || !(bool)Settings[FrameworkPackageSettings.SynchronousEvents])
            {
                QueuingEventListener queue = new QueuingEventListener();
                Context.Listener = queue;

                _pump = new EventPump(listener, queue.Events);
                _pump.Start();
            }
#endif

            if (!System.Diagnostics.Debugger.IsAttached &&
                Settings.ContainsKey(FrameworkPackageSettings.DebugTests) &&
                (bool)Settings[FrameworkPackageSettings.DebugTests])
                System.Diagnostics.Debugger.Launch();

#if !PORTABLE
            if (Settings.ContainsKey(FrameworkPackageSettings.PauseBeforeRun) &&
                (bool)Settings[FrameworkPackageSettings.PauseBeforeRun])
                PauseBeforeRun();

#endif

            Context.Dispatcher.Dispatch(TopLevelWorkItem);
        }