Пример #1
0
        private static void ReapTasks(TestContext context, TaskContainer container)
        {
            if (!container.JoinAll(JoinBeforeAbortTimeout))
            {
                LogMessageAboutActiveTasks(context, container,
                    String.Format("Some tasks failed to complete within {0} seconds of test termination: ", JoinBeforeAbortTimeout.TotalSeconds));

                container.AbortAll();

                if (!container.JoinAll(JoinAfterAbortTimeout))
                {
                    LogMessageAboutActiveTasks(context, container,
                        String.Format("Some tasks failed to abort within {0} seconds of test termination: ", (JoinBeforeAbortTimeout + JoinAfterAbortTimeout).TotalSeconds));
                }
            }
        }
Пример #2
0
        private static TaskContainer CreateContainer(TestContext context)
        {
            var container = new TaskContainer();

            context.Finishing += delegate { ReapTasks(context, container); };
            container.TaskTerminated += delegate(object sender, TaskEventArgs e) { RecordTaskResult(context, e.Task); };

            return container;
        }
Пример #3
0
        private static void LogMessageAboutActiveTasks(TestContext context, TaskContainer container, string messagePrefix)
        {
            IList<Task> activeTasks = container.GetActiveTasks();
            if (activeTasks.Count == 0)
                return;

            var message = new StringBuilder(messagePrefix);

            for (int i = 0; i < activeTasks.Count; i++)
            {
                if (i != 0)
                    message.Append(", ");
                message.Append(activeTasks[i].Name);
            }

            context.LogWriter.Warnings.WriteLine(message.ToString());
        }
        /// <inheritdoc />
        protected override void DecorateTest(IPatternScope scope, ICodeElementInfo codeElement)
        {
            scope.TestBuilder.TestInstanceActions.RunTestInstanceBodyChain.Around((state, inner) =>
            {
                TaskContainer container = new TaskContainer();
                try
                {
                    TestOutcome[] threadOutcomes = new TestOutcome[numThreads];
                    TestContext context = TestContext.CurrentContext;

                    for (int i = 0; i < numThreads; i++)
                    {
                        int index = i;

                        string name = String.Format("Threaded Repetition #{0}", index + 1);
                        var task = new TestEnvironmentAwareThreadTask(name, delegate
                        {
                            TestContext threadContext = TestStep.RunStep(name, delegate
                            {
                                TestOutcome innerOutcome = inner(state);
                                if (innerOutcome.Status != TestStatus.Passed)
                                    throw new SilentTestException(innerOutcome);
                            }, null, false, codeElement);

                            threadOutcomes[index] = threadContext.Outcome;
                        }, null);

                        task.Terminated += delegate
                        {
                            if (! task.Result.HasValue)
                            {
                                threadOutcomes[index] = TestOutcome.Error;
                                context.LogWriter.Default.WriteException(task.Result.Exception,
                                    String.Format("An exception occurred while starting Threaded Repetition #{0}.",
                                        index));
                            }
                        };

                        container.Watch(task);
                        task.Start();
                    }

                    container.JoinAll(null);

                    TestOutcome outcome = TestOutcome.Passed;
                    int passedCount = 0;
                    foreach (TestOutcome threadOutcome in threadOutcomes)
                    {
                        outcome = outcome.CombineWith(threadOutcome);
                        if (threadOutcome.Status == TestStatus.Passed)
                            passedCount += 1;
                    }

                    context.LogWriter.Default.WriteLine(String.Format("{0} of {1} threaded repetitions passed.",
                        passedCount, numThreads));

                    return outcome;
                }
                finally
                {
                    container.AbortAll();
                    container.JoinAll(null);
                }
            });
        }