Пример #1
0
        public virtual object InvokeBinding(IBinding binding, IContextManager contextManager, object[] arguments, ITestTracer testTracer, out TimeSpan duration)
        {
            MethodInfo methodInfo;
            Delegate   bindingAction;

            EnsureReflectionInfo(binding, out methodInfo, out bindingAction);

            try
            {
                object    result;
                Stopwatch stopwatch = new Stopwatch();
                using (CreateCultureInfoScope(contextManager))
                {
                    stopwatch.Start();
                    object[] invokeArgs = new object[arguments == null ? 1 : arguments.Length + 1];
                    if (arguments != null)
                    {
                        Array.Copy(arguments, 0, invokeArgs, 1, arguments.Length);
                    }
                    invokeArgs[0] = contextManager;

                    result = AsyncHelpers.RunSync(async() =>
                    {
                        var r = bindingAction.DynamicInvoke(invokeArgs);
                        if (r is Task t)
                        {
                            await t;
                        }
                        return(r);
                    });

                    stopwatch.Stop();
                }

                if (specFlowConfiguration.TraceTimings && stopwatch.Elapsed >= specFlowConfiguration.MinTracedDuration)
                {
                    testTracer.TraceDuration(stopwatch.Elapsed, binding.Method, arguments);
                }

                duration = stopwatch.Elapsed;
                return(result);
            }
            catch (ArgumentException ex)
            {
                throw errorProvider.GetCallError(binding.Method, ex);
            }
            catch (TargetInvocationException invEx)
            {
                var ex = invEx.InnerException;
                ex = ex.PreserveStackTrace(errorProvider.GetMethodText(binding.Method));
                throw ex;
            }
            catch (AggregateException aggregateEx)
            {
                var ex = aggregateEx.InnerExceptions.First();
                ex = ex.PreserveStackTrace(errorProvider.GetMethodText(binding.Method));
                throw ex;
            }
        }
Пример #2
0
        public object InvokeAction(IContextManager contextManager, object[] arguments, ITestTracer testTracer, out TimeSpan duration)
        {
            try
            {
                object    result;
                Stopwatch stopwatch = new Stopwatch();
                using (CreateCultureInfoScope(contextManager))
                {
                    stopwatch.Start();
                    object[] invokeArgs = new object[arguments == null ? 1 : arguments.Length + 1];
                    if (arguments != null)
                    {
                        Array.Copy(arguments, 0, invokeArgs, 1, arguments.Length);
                    }
                    invokeArgs[0] = contextManager;
                    result        = BindingAction.DynamicInvoke(invokeArgs);
                    stopwatch.Stop();
                }

                if (runtimeConfiguration.TraceTimings && stopwatch.Elapsed >= runtimeConfiguration.MinTracedDuration)
                {
                    testTracer.TraceDuration(stopwatch.Elapsed, MethodInfo, arguments);
                }

                duration = stopwatch.Elapsed;
                return(result);
            }
            catch (ArgumentException ex)
            {
                throw errorProvider.GetCallError(MethodInfo, ex);
            }
            catch (TargetInvocationException invEx)
            {
                var ex = invEx.InnerException;
                ex = ex.PreserveStackTrace(errorProvider.GetMethodText(MethodInfo));
                throw ex;
            }
        }