Exemple #1
0
        public static Task <RunSummary> TimeoutRunAsync(this ITimeoutTestCase testCase, IMessageBus messageBus, CancellationTokenSource cancellationTokenSource, Func <Task <RunSummary> > runTest)
        {
            DateTime begin = DateTime.UtcNow;

            // Run test on STA thread (https://github.com/Haacked/samples/blob/9de33a206e0e3fb654479580445642f4bcc0dc84/STAExamples/STATestCase.cs).
            var    apartmentState = testCase.UseStaThread ? ApartmentState.STA : ApartmentState.Unknown;
            Thread thread;
            var    tcs = RunAsync(() => {
                var testCaseTask = runTest( );
                return(testCaseTask.Result);
            }, apartmentState, out thread);

            // If no timeout was specified, return task for test.
            if (testCase.Timeout < 1)
            {
                return(tcs.Task);
            }


            // Otherwise, wait for test to complete, or timeout to elapse.
            return(Task.Run(() => {
                var test = tcs.Task;
                Task delay = Task.Delay(testCase.Timeout, cancellationTokenSource.Token);
                if (Task.WaitAny(test, delay) != 0 && !test.IsCompleted)
                {
                    DateTime end = DateTime.UtcNow;
                    TimeSpan elapsed = end - begin;

                    var exception = new TimeoutException(testCase.DisplayName + " has exceeded the execution timeout of " + testCase.Timeout + "ms.");
                    var message = new TestFailed(new XunitTest(testCase, testCase.DisplayName), (decimal)elapsed.TotalMilliseconds, null, exception);

                    if (!messageBus.QueueMessage(message))
                    {
                        cancellationTokenSource.Cancel( );
                    }

                    tcs.TrySetResult(new RunSummary {
                        Total = 1, Failed = 1, Time = (decimal)elapsed.TotalMilliseconds
                    });
                    thread.Abort( );
                }

                return test.Result;
            }, cancellationTokenSource.Token));
        }
Exemple #2
0
 public static void SerializeTimeoutTestCase(this IXunitSerializationInfo data, ITimeoutTestCase testCase)
 {
     data.AddValue("UseStaThread", testCase.UseStaThread);
     data.AddValue("Timeout", testCase.Timeout);
 }