protected override object ExecuteMethod()

in src/TestFramework/Core/Adapters/ManagedAdapterBase.cs [309:382]


        protected override object ExecuteMethod(MethodInfo targetMethod, object[] args)
        {
            object retVal = null;
            // Check if this is a method from IAdapter. Any IAdapter methods should be ignored.
            if (!AdapterType.IsAdapterTypeFullName(targetMethod.DeclaringType.FullName)
                && (targetMethod.DeclaringType.FullName != typeof(IDisposable).FullName)
                )
            {
                TestSite.Log.Add(LogEntryKind.EnterAdapter,
                        "Managed adapter: {0}, method: {1}",
                        ProxyType.Name,
                        targetMethod.Name);

                try
                {
                    int timeout = AdapterProxyHelpers.GetTimeout(targetMethod, int.Parse(TestSite.Properties["AdapterInvokeTimeout"]));
                    Task<object> invokeTask = Task.Run<object>(() =>
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, $"Start to invoke target method {targetMethod.Name}, timeout: {timeout}");
                        object invokeResult;
                        if (targetMethod.IsStatic)
                        {
                            invokeResult = targetMethod.Invoke(null, args);
                        }
                        else
                        {
                            invokeResult = targetMethod.Invoke(instance, args);
                        }
                        TestSite.Log.Add(LogEntryKind.Debug, $"Complete invoke target method {targetMethod.Name}.");
                        return invokeResult;
                    });

                    TimeSpan waiter = TimeSpan.FromMinutes(timeout);

                    if (invokeTask.Wait(waiter))
                    {
                        retVal = invokeTask.Result;
                    }
                    else
                    {
                        throw new TimeoutException($"Invoke adapater method timeout after wait {timeout} minutes.");
                    }
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    
                    if (ex is TargetInvocationException)
                    {
                        // thrown by methods invoked through reflection
                        // InnerException contains the actual exception thrown by methods
                        TestSite.Log.Add(LogEntryKind.Debug, ex.InnerException.ToString());
                        throw ex.InnerException;
                    }
                    else
                    {
                        TestSite.Log.Add(LogEntryKind.Debug, ex.ToString());
                        throw;
                    }
                }
                finally
                {
                    TestSite.Log.Add(LogEntryKind.ExitAdapter,
                        "Managed adapter: {0}, method: {1}",
                        ProxyType.Name,
                        targetMethod.Name);
                }
            }

            return retVal;
        }