title: How to capture last trial result in Poly retry policy?
body: <p>I am working on extending the <code>TestMethod</code> attribute in .NET Core. I am using Polly library for retry logic along with an outer timeout policy.</p>
  <p>I want a helper method that can retry invoking the <code>ITestMethod</code> until it passes. I don't mind the number of times it'll be retried. But I'll be putting a timeout within which it has to be completed. If the delegate is executed successfully within timeout, it's fine. But if there is a timeout exception, I still want the failed result value (the result of the last iteration) instead of <code>TimeOutRejectedException</code> or the default value of the return type.</p>
  <p>Below is my extended test method attribute class:</p>
  <pre><code>public sealed class GuaranteedPassTestMethodAttribute : TestMethodAttribute
  {
      /// &lt;inheritdoc/&gt;
      public override TestResult[] Execute(ITestMethod testMethod)
      {
          return ExecuteTestTillSuccess(testMethod);
      }
  
      private TestResult[] ExecuteTestTillSuccess(ITestMethod testMethod)
      {
          var gracefulTestRun =
              TestExecutionHelper.ExecuteTestTillPassAsync(
                  () =&gt; TestInvokeDelegate(testMethod));
  
          return gracefulTestRun.Result;
      }
  
      private Task&lt;TestResult[]&gt; TestInvokeDelegate(ITestMethod testMethod)
      {
          TestResult[] result = null;
          var thread = new Thread(() =&gt; result = Invoke(testMethod));
          thread.Start();
          thread.Join();
  
          return Task.FromResult(result);
      }
  }
  </code></pre>
  <p>Below is my <code>TestExecutionHelper</code> that uses Polly:</p>
  <pre><code>internal static class TestExecutionHelper
  {
      private static readonly Func&lt;TestResult[], bool&gt; TestFailurePredicate =
          results =&gt; results != null &amp;&amp;
                     results.Length == 1 &amp;&amp;
                     results.First().Outcome != UnitTestOutcome.Passed;
  
      internal static async Task&lt;TestResult[]&gt; ExecuteTestTillPassAsync(
          Func&lt;Task&lt;TestResult[]&gt;&gt; testInvokeDelegate,
          int delayBetweenExecutionInMs = 3000,
          int timeoutInSeconds = 60 * 10)
      {
          var timeoutPolicy = Policy.TimeoutAsync&lt;TestResult[]&gt;(timeoutInSeconds);
          var retryPolicy = Policy.HandleResult&lt;TestResult[]&gt;(TestFailurePredicate)
                                  .WaitAndRetryAsync(int.MaxValue, x =&gt; TimeSpan.FromMilliseconds(delayBetweenExecutionInMs));
          var testRunPolicy = timeoutPolicy.WrapAsync(retryPolicy);
          return await testRunPolicy.ExecuteAsync(testInvokeDelegate);
      }
  }
  </code></pre>
  <p>With this setup, I either get a passed test method execution or <code>TimeOutRejectedException</code> for failed tests. I want to capture failed test's <code>TestResult</code> even after retries.</p>