/*
 * Copyright 2014 Mariusz.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.watchrabbit.executor.command;

import com.watchrabbit.commons.exception.SystemException;
import static com.watchrabbit.executor.wrapper.RetryConfig.retry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

/**
 *
 * @author Mariusz
 */
public class RetryTest {

    @Test(timeout = 200)
    public void shoudlInvokeTwice() throws ExecutionException {
        CountDownLatch latch = new CountDownLatch(2);

        String invoke = ExecutorCommand.<String>executor("shoudlInvokeTwice")
                .withBreakerRetryTimeout(-1, TimeUnit.MILLISECONDS)
                .withRetry(
                        retry()
                        .withRetryInterval(10, TimeUnit.MILLISECONDS)
                ).invoke(
                        () -> {
                            latch.countDown();
                            if (latch.getCount() == 1) {
                                throw new SystemException();
                            } else {
                                return "abc";
                            }
                        }
                );

        assertThat(latch.getCount()).isEqualTo(0);
        assertThat(invoke).isEqualTo("abc");
    }
}

@@ -1,10 +1,11 @@
 /*
+%0D
 %0A * Copy
@@ -23,19 +23,21 @@
 Mariusz.
+%0D
 %0A *
+%0D
 %0A * Lice
@@ -95,16 +95,17 @@
 cense%22);
+%0D
 %0A * you 
@@ -164,16 +164,17 @@
 License.
+%0D
 %0A * You 
@@ -208,19 +208,21 @@
 cense at
+%0D
 %0A *
+%0D
 %0A *     
@@ -264,19 +264,21 @@
 ENSE-2.0
+%0D
 %0A *
+%0D
 %0A * Unle
@@ -340,16 +340,17 @@
 software
+%0D
 %0A * dist
@@ -410,16 +410,17 @@
 %22 BASIS,
+%0D
 %0A * WITH
@@ -487,16 +487,17 @@
 implied.
+%0D
 %0A * See 
@@ -559,16 +559,17 @@
 ions and
+%0D
 %0A * limi
@@ -594,20 +594,22 @@
 License.
+%0D
 %0A */
+%0D
 %0Apackage
@@ -642,17 +642,19 @@
 command;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -695,32 +695,33 @@
 SystemException;
+%0D
 %0Aimport static c
@@ -770,16 +770,17 @@
 g.retry;
+%0D
 %0Aimport 
@@ -815,16 +815,17 @@
 wnLatch;
+%0D
 %0Aimport 
@@ -864,16 +864,17 @@
 ception;
+%0D
 %0Aimport 
@@ -903,16 +903,17 @@
 imeUnit;
+%0D
 %0Aimport 
@@ -962,16 +962,17 @@
 ertThat;
+%0D
 %0Aimport 
@@ -990,16 +990,20 @@
 est;
-%0A
+%0D%0A%0D
 %0A/**
+%0D
 %0A *
+%0D
 %0A * 
@@ -1017,20 +1017,22 @@
  Mariusz
+%0D
 %0A */
+%0D
 %0Apublic 
@@ -1048,17 +1048,19 @@
 ryTest %7B
-%0A
+%0D%0A%0D
 %0A    @Te
@@ -1076,12 +1076,13 @@
 t = 
-2
+5
 00)
+%0D
 %0A   
@@ -1141,16 +1141,17 @@
 eption %7B
+%0D
 %0A       
@@ -1196,17 +1196,19 @@
 atch(2);
-%0A
+%0D%0A%0D
 %0A       
@@ -1277,16 +1277,17 @@
 eTwice%22)
+%0D
 %0A       
@@ -1338,32 +1338,33 @@
 it.MILLISECONDS)
+%0D
 %0A               
@@ -1375,16 +1375,17 @@
 thRetry(
+%0D
 %0A       
@@ -1408,16 +1408,17 @@
  retry()
+%0D
 %0A       
@@ -1479,16 +1479,17 @@
 SECONDS)
+%0D
 %0A       
@@ -1506,16 +1506,17 @@
 .invoke(
+%0D
 %0A       
@@ -1539,16 +1539,17 @@
  () -%3E %7B
+%0D
 %0A       
@@ -1583,24 +1583,25 @@
 countDown();
+%0D
 %0A           
@@ -1645,16 +1645,17 @@
  == 1) %7B
+%0D
 %0A       
@@ -1707,16 +1707,17 @@
 ption();
+%0D
 %0A       
@@ -1745,16 +1745,17 @@
 %7D else %7B
+%0D
 %0A       
@@ -1792,16 +1792,17 @@
 n %22abc%22;
+%0D
 %0A       
@@ -1815,32 +1815,33 @@
                %7D
+%0D
 %0A               
@@ -1850,16 +1850,17 @@
        %7D
+%0D
 %0A       
@@ -1870,17 +1870,19 @@
       );
-%0A
+%0D%0A%0D
 %0A       
@@ -1924,16 +1924,17 @@
 alTo(0);
+%0D
 %0A       
@@ -1974,13 +1974,16 @@
 c%22);
+%0D
 %0A    %7D
+%0D
 %0A%7D
+%0D
 %0A

Test fix