/*
 * 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 com.watchrabbit.commons.sleep.Sleep;
import static com.watchrabbit.executor.command.ExecutorCommand.executor;
import com.watchrabbit.executor.wrapper.CheckedRunnable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

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

    @Test
    public void shoudlInvokeMethod() {
        CountDownLatch latch = new CountDownLatch(1);
        executor("")
                .silentFailMode()
                .invoke(()
                        -> latch.countDown()
                );

        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void shoudlBreakCircut() {
        CountDownLatch latch = new CountDownLatch(1);

        executor("config")
                .silentFailMode()
                .invoke((CheckedRunnable) () -> {
                    throw new SystemException();
                });

        executor("config")
                .silentFailMode()
                .invoke(()
                        -> latch.countDown()
                );

        assertThat(latch.getCount()).isEqualTo(1);
    }

    @Test
    public void shoudlBreakCircutAndClose() {
        CountDownLatch latch = new CountDownLatch(2);

        executor("config34")
                .withBreakerRetryTimeout(100, TimeUnit.MICROSECONDS)
                .silentFailMode()
                .invoke((CheckedRunnable) () -> {
                    throw new SystemException();
                });

        executor("config34")
                .withBreakerRetryTimeout(100, TimeUnit.MICROSECONDS)
                .silentFailMode()
                .invoke(()
                        -> latch.countDown()
                );
        Sleep.untilTrue(() -> true, 100, TimeUnit.MICROSECONDS);
        executor("config34")
                .withBreakerRetryTimeout(100, TimeUnit.MICROSECONDS)
                .silentFailMode()
                .invoke(()
                        -> latch.countDown()
                );

        assertThat(latch.getCount()).isEqualTo(1);
    }

    @Test
    public void shoudlInvokeMethodAsynchronously() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        executor("")
                .queue(()
                        -> latch.countDown()
                );

        latch.await(1000, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void shoudlCallOnSuccessMethod() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);
        executor("")
                .observe(() -> latch.countDown(),
                        () -> latch.countDown()
                );

        latch.await(1000, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void shoudlCallOnSuccessMethodWithResult() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        executor("")
                .observe(() -> true,
                        (result) -> {
                            if (result) {
                                latch.countDown();
                            }
                        }
                );

        latch.await(1000, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void shoudlCallOnSuccessMethodWithResultAndErrorMethod() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        executor("")
                .observe(() -> true,
                        (result) -> {
                            if (result) {
                                latch.countDown();
                            }
                        },
                        (exception) -> {
                        }
                );

        latch.await(1000, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void shoudlCallOnErrorMethod() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        executor("")
                .observe((CheckedRunnable) () -> {
                    throw new SystemException("exception");
                },
                () -> {
                },
                (Exception ex) -> latch.countDown()
                );

        latch.await(1000, TimeUnit.MILLISECONDS);
        assertThat(latch.getCount()).isEqualTo(0);
    }
}

@@ -2135,33 +2135,33 @@
 kerRetryTimeout(
-1
+2
 00, TimeUnit.MIC
@@ -2151,35 +2151,35 @@
 200, TimeUnit.MI
-CRO
+LLI
 SECONDS)%0D%0A      
@@ -2394,33 +2394,33 @@
 kerRetryTimeout(
-1
+2
 00, TimeUnit.MIC
@@ -2410,35 +2410,35 @@
 200, TimeUnit.MI
-CRO
+LLI
 SECONDS)%0D%0A      
@@ -2556,24 +2556,26 @@
         );%0D%0A
+%0D%0A
         Slee
@@ -2598,17 +2598,17 @@
 %3E true, 
-1
+2
 00, Time
@@ -2606,35 +2606,35 @@
 200, TimeUnit.MI
-CRO
+LLI
 SECONDS);%0D%0A     
@@ -2699,17 +2699,17 @@
 Timeout(
-1
+2
 00, Time
@@ -2719,11 +2719,11 @@
 t.MI
-CRO
+LLI
 SECO

Test fix