Beispiel #1
0
        private async Task <decimal> InvokeTestMethodAsync(ExceptionAggregator aggregator, ITestOutputHelper output)
        {
            var retryAttribute = GetRetryAttribute(TestMethod);
            var collectDump    = TestMethod.GetCustomAttribute <CollectDumpAttribute>() != null;

            if (!typeof(LoggedTestBase).IsAssignableFrom(TestClass) || retryAttribute == null)
            {
                return(await new LoggedTestInvoker(Test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, BeforeAfterAttributes, aggregator, CancellationTokenSource, output, null, collectDump).RunAsync());
            }

            var retryPredicateMethodName = retryAttribute.RetryPredicateName;
            var retryPredicateMethod     = TestClass.GetMethod(retryPredicateMethodName,
                                                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static,
                                                               null,
                                                               new Type[] { typeof(Exception) },
                                                               null)
                                           ?? throw new InvalidOperationException($"No valid static retry predicate method {retryPredicateMethodName} was found on the type {TestClass.FullName}.");

            if (retryPredicateMethod.ReturnType != typeof(bool))
            {
                throw new InvalidOperationException($"Retry predicate method {retryPredicateMethodName} on {TestClass.FullName} does not return bool.");
            }

            var retryContext = new RetryContext()
            {
                Limit  = retryAttribute.RetryLimit,
                Reason = retryAttribute.RetryReason,
            };

            var retryAggregator   = new ExceptionAggregator();
            var loggedTestInvoker = new LoggedTestInvoker(Test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, BeforeAfterAttributes, retryAggregator, CancellationTokenSource, output, retryContext, collectDump);
            var totalTime         = 0.0M;

            do
            {
                retryAggregator.Clear();
                totalTime += await loggedTestInvoker.RunAsync();

                retryContext.CurrentIteration++;
            }while (retryAggregator.HasExceptions &&
                    retryContext.CurrentIteration < retryContext.Limit &&
                    (retryPredicateMethod.IsStatic
                    ? (bool)retryPredicateMethod.Invoke(null, new object[] { retryAggregator.ToException() })
                    : (bool)retryPredicateMethod.Invoke(retryContext.TestClassInstance, new object[] { retryAggregator.ToException() }))
                    );

            aggregator.Aggregate(retryAggregator);
            return(totalTime);
        }
Beispiel #2
0
 public LoggedTestInvoker(
     ITest test,
     IMessageBus messageBus,
     Type testClass,
     object[] constructorArguments,
     MethodInfo testMethod,
     object[] testMethodArguments,
     IReadOnlyList <BeforeAfterTestAttribute> beforeAfterAttributes,
     ExceptionAggregator aggregator,
     CancellationTokenSource cancellationTokenSource,
     ITestOutputHelper output,
     RetryContext retryContext)
     : base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, beforeAfterAttributes, aggregator, cancellationTokenSource)
 {
     _output       = output;
     _retryContext = retryContext;
 }