package net.jodah.failsafe.issues;

import static org.testng.Assert.assertEquals;

import java.util.concurrent.atomic.AtomicInteger;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

/**
 * https://github.com/jhalterman/failsafe/issues/36
 */
@Test
@SuppressWarnings("unchecked")
public class Issue36 {
  RetryPolicy retryPolicy = new RetryPolicy().retryWhen(false).retryOn(Exception.class).withMaxRetries(3);
  AtomicInteger calls;
  AtomicInteger failedAttempts;
  AtomicInteger retries;

  @BeforeMethod
  protected void beforeMethod() {
    calls = new AtomicInteger();
    failedAttempts = new AtomicInteger();
    retries = new AtomicInteger();
  }

  public void test() {
    Failsafe.with(retryPolicy)
        .onFailedAttempt(e -> failedAttempts.incrementAndGet())
        .onRetry(e -> retries.incrementAndGet())
        .get(() -> {
          calls.incrementAndGet();
          return false;
        });

    // Then
    assertCounters();
  }

  void assertCounters() {
    assertEquals(calls.get(), 4);
    assertEquals(failedAttempts.get(), 4);
    assertEquals(retries.get(), 3);
  }
}

@@ -61,28 +61,17 @@
 .Assert.
-assertEquals
+*
 ;%0A%0Aimpor
@@ -448,19 +448,49 @@
 etry
-When(false)
+If((Boolean r) -%3E r == null %7C%7C !r)%0A      
 .ret
@@ -510,16 +510,23 @@
 n.class)
+%0A      
 .withMax
@@ -802,24 +802,36 @@
 id test() %7B%0A
+    try %7B%0A  
     Failsafe
@@ -849,16 +849,18 @@
 Policy)%0A
+  
         
@@ -876,17 +876,25 @@
 Attempt(
-e
+(r, f, c)
  -%3E fail
@@ -915,32 +915,34 @@
 rementAndGet())%0A
+  
         .onRetry
@@ -966,32 +966,34 @@
 rementAndGet())%0A
+  
         .get(() 
@@ -1003,24 +1003,26 @@
 %7B%0A          
+  
 calls.increm
@@ -1048,33 +1048,101 @@
     
-return false;%0A    
+  throw new Exception();%0A          %7D);%0A      fail();%0A    %7D catch (Exception expected) %7B%0A
     %7D
-);
 %0A%0A  

Updated issue #36 test. test