Пример #1
0
            internal static Exception Intercept(object invocation)
            {
                var invocationDescriptor = GetInvocationDescriptor(invocation);

#if NET_4_0 || NET_4_5 || PORTABLE
                if (AsyncInvocationRegion.IsAsyncOperation(invocationDescriptor.Delegate))
                {
                    using (var region = AsyncInvocationRegion.Create(invocationDescriptor.Delegate))
                    {
                        try
                        {
                            object result = invocationDescriptor.Invoke();
                            region.WaitForPendingOperationsToComplete(result);
                            return(null);
                        }
                        catch (Exception ex)
                        {
                            return(ex);
                        }
                    }
                }
                else
#endif
                {
                    try
                    {
                        invocationDescriptor.Invoke();
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        return(ex);
                    }
                }
            }
Пример #2
0
 private object RunAsyncTestMethod(TestExecutionContext context)
 {
     TLogger.Write("##### RunAsyncTestMethod in TTestMethodCommand class #####");
     #region tronghieu.d - invoke async test method in application thread. This thread is blocked for waiting result.
     using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(testMethod.Method.MethodInfo))
     {
         TAsyncThreadMgr asyncThreadMgr = TAsyncThreadMgr.GetInstance();
         asyncThreadMgr.SetData(this, testMethod, arguments, context, true);
         asyncThreadMgr.GetMethodExecutionResetEvent().Set(); // release main thread to invoke method
         _testMethodRunComplete.WaitOne();                    // wait for result in current thread
         if (asyncThreadMgr.GetNonAsyncMethodException() != null)
         {
             throw asyncThreadMgr.GetNonAsyncMethodException();
         }
         try
         {
             if (asyncThreadMgr.GetResult() == null)
             {
                 return(asyncThreadMgr.GetResult());
             }
             return(region.WaitForPendingOperationsToComplete(asyncThreadMgr.GetResult()));
         }
         catch (Exception e)
         {
             throw new NUnitException("Rethrown", e);
         }
     }
     #endregion
 }
Пример #3
0
        private static object InvokeDelegate <T>(ActualValueDelegate <T> del)
        {
            if (AsyncInvocationRegion.IsAsyncOperation(del))
            {
                using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(del))
                    return(region.WaitForPendingOperationsToComplete(del()));
            }

            return(del());
        }
Пример #4
0
        /// <summary>
        /// Test whether the constraint is satisfied by an
        /// ActualValueDelegate that returns the value to be tested.
        /// The default implementation simply evaluates the delegate
        /// but derived classes may override it to provide for delayed
        /// processing.
        /// </summary>
        /// <param name="del">An <see cref="ActualValueDelegate{T}"/></param>
        /// <returns>True for success, false for failure</returns>
        public virtual bool Matches <T>(ActualValueDelegate <T> del)
        {
            if (AsyncInvocationRegion.IsAsyncOperation(del))
            {
                using (var region = AsyncInvocationRegion.Create(del))
                    return(Matches(region.WaitForPendingOperationsToComplete(del())));
            }

            return(Matches(del()));
        }
Пример #5
0
        /// <summary>
        /// Applies the constraint to an ActualValueDelegate that returns
        /// the value to be tested. The default implementation simply evaluates
        /// the delegate but derived classes may override it to provide for
        /// delayed processing.
        /// </summary>
        /// <param name="del">An ActualValueDelegate</param>
        /// <returns>A ConstraintResult</returns>
        public virtual ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del)
        {
#if ASYNC
            if (AsyncInvocationRegion.IsAsyncOperation(del))
            {
                using (var region = AsyncInvocationRegion.Create(del))
                    return(ApplyTo(region.WaitForPendingOperationsToComplete(del())));
            }
#endif
            return(ApplyTo(GetTestObject(del)));
        }
Пример #6
0
        /// <summary>
        /// Applies the constraint to an ActualValueDelegate that returns
        /// the value to be tested. The default implementation simply evaluates
        /// the delegate but derived classes may override it to provide for
        /// delayed processing.
        /// </summary>
        /// <param name="del">An ActualValueDelegate</param>
        /// <returns>A ConstraintResult</returns>
        public virtual ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del)
        {
#if NET_4_0 || NET_4_5 || PORTABLE
            if (AsyncInvocationRegion.IsAsyncOperation(del))
            {
                using (var region = AsyncInvocationRegion.Create(del))
                    return(ApplyTo(region.WaitForPendingOperationsToComplete(del())));
            }
#endif
            return(ApplyTo(del()));
        }
Пример #7
0
        private static object InvokeDelegate <T>(ActualValueDelegate <T> del)
        {
#if NET_4_0 || NET_4_5 || NETSTANDARD1_3 || NETSTANDARD1_6
            if (AsyncInvocationRegion.IsAsyncOperation(del))
            {
                using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(del))
                    return(region.WaitForPendingOperationsToComplete(del()));
            }
#endif

            return(del());
        }
Пример #8
0
        private object RunAsyncTestMethod(TestExecutionContext context)
        {
            using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(testMethod.Method.MethodInfo))
            {
                object result = Reflect.InvokeMethod(testMethod.Method.MethodInfo, context.TestObject, arguments);

                try
                {
                    return(region.WaitForPendingOperationsToComplete(result));
                }
                catch (Exception e)
                {
                    throw new NUnitException("Rethrown", e);
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Executes the code and returns success if an exception is thrown.
        /// </summary>
        /// <param name="actual">A delegate representing the code to be tested</param>
        /// <returns>True if an exception is thrown, otherwise false</returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            TestDelegate code            = actual as TestDelegate;
            Exception    caughtException = null;

            if (code != null)
            {
                try
                {
                    code();
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }
            }
#if ASYNC
            AsyncTestDelegate asyncCode = actual as AsyncTestDelegate;
            if (asyncCode != null)
            {
                using (var region = AsyncInvocationRegion.Create(asyncCode))
                {
                    try
                    {
                        var task = asyncCode();
                        region.WaitForPendingOperationsToComplete(task);
                    }
                    catch (Exception ex)
                    {
                        caughtException = ex;
                    }
                }
            }
            if (code == null && asyncCode == null)
#else
            else
#endif
            {
                throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), nameof(actual));
            }
            return(new ThrowsExceptionConstraintResult(this, caughtException));
        }
Пример #10
0
            internal static Exception Intercept(object invocation)
            {
                var invocationDescriptor = GetInvocationDescriptor(invocation);

#if ASYNC
                if (AsyncInvocationRegion.IsAsyncOperation(invocationDescriptor.Delegate))
                {
                    using (var region = AsyncInvocationRegion.Create(invocationDescriptor.Delegate))
                    {
                        try
                        {
                            object result = invocationDescriptor.Invoke();
                            region.WaitForPendingOperationsToComplete(result);
                            return(null);
                        }
                        catch (Exception ex)
                        {
                            return(ex);
                        }
                    }
                }
                else
#endif
                {
                    using (new TestExecutionContext.IsolatedContext())
                    {
                        try
                        {
                            invocationDescriptor.Invoke();
                            return(null);
                        }
                        catch (Exception ex)
                        {
                            return(ex);
                        }
                    }
                }
            }
Пример #11
0
 private void RunAsyncMethod(MethodInfo method, TestExecutionContext context)
 {
     using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(method))
         region.WaitForPendingOperationsToComplete(RunNonAsyncMethod(method, context));
 }