package me.alb_i986.testing.assertions.retry;

import me.alb_i986.testing.assertions.retry.internal.WaitStrategies;
import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import static me.alb_i986.testing.assertions.retry.RetryMatcher.eventually;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class AssertRetryIT {

    @Test
    public void supplierEventuallyReturnsMatchingValue() {
        Supplier<String> actual = Suppliers.fromList("a", "b", "c");

        assertThat(actual, eventually(containsString("c"), RetryConfig.builder()
                        .retryOnException(false)
                        .maxAttempts(3)
                        .waitStrategy(WaitStrategies.sleep(2, TimeUnit.SECONDS))
                        // TODO
//                        .sleepBetweenAttempts(5, TimeUnit.SECONDS)
//                        .waitBetweenAttempts(WaitStrategies.sleep(5, TimeUnit.SECONDS))
        ));
    }

    @Test(expected = AssertionError.class)
    public void retryWithTimeout_supplierNeverMatches() {
        assertThat(Suppliers.id("a"), eventually(containsString("c"), RetryConfig.builder()
                .timeoutAfter(1, TimeUnit.SECONDS)
                .waitStrategy(WaitStrategies.sleep(100, TimeUnit.MILLISECONDS))
                .retryOnException(false)
        ));
    }

    @Test
    public void sleepBetweenAttempts() {
        Supplier<String> actual = Suppliers.fromList("a", "b", "c");
        long startTimeMillis = System.currentTimeMillis();

        // when
        assertThat(actual, eventually(containsString("b"), RetryConfig.builder()
                .sleepBetweenAttempts(1, TimeUnit.SECONDS)
                .maxAttempts(2)
                .retryOnException(false)
        ));

        long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis;
        assertThat(elapsedTimeMillis, allOf(greaterThan(1000L), lessThanOrEqualTo(1300L)));
    }
}
@@ -1010,42 +1010,9 @@
 Test
-(expected = AssertionError.class)
 %0A
+
     
@@ -1061,24 +1061,42 @@
 Matches() %7B%0A
+        try %7B%0A    
         asse
@@ -1191,16 +1191,20 @@
         
+   
+ 
 .timeout
@@ -1222,32 +1222,36 @@
 meUnit.SECONDS)%0A
+    
                 
@@ -1322,32 +1322,36 @@
                 
+ 
+   
 .retryOnExceptio
@@ -1359,35 +1359,110 @@
 (false)%0A        
-));
+    ));%0A        %7D catch (AssertionError e) %7B%0A            // expected%0A        %7D
 %0A    %7D%0A%0A    @Tes

Test refactoring: avoid using @Test.expected