/*
 * 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(200, TimeUnit.MILLISECONDS)
                .silentFailMode()
                .invoke((CheckedRunnable) () -> {
                    throw new SystemException();
                });

        executor("config34")
                .withBreakerRetryTimeout(200, TimeUnit.MILLISECONDS)
                .silentFailMode()
                .invoke(()
                        -> latch.countDown()
                );

        Sleep.untilTrue(() -> true, 200, TimeUnit.MILLISECONDS);
        executor("config34")
                .withBreakerRetryTimeout(200, TimeUnit.MILLISECONDS)
                .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);
    }
}

@@ -2068,24 +2068,81 @@
 atch(2);%0D%0A%0D%0A
+        System.err.println(System.currentTimeMillis());%0D%0A
         exec
@@ -2151,32 +2151,32 @@
 or(%22config34%22)%0D%0A
-
                 
@@ -2380,32 +2380,89 @@
          %7D);%0D%0A%0D%0A
+        System.err.println(System.currentTimeMillis());%0D%0A
         executor
@@ -2665,32 +2665,89 @@
              );%0D
+%0A        System.err.println(System.currentTimeMillis());%0D
 %0A%0D%0A        Sleep
@@ -2791,32 +2791,89 @@
 MILLISECONDS);%0D%0A
+        System.err.println(System.currentTimeMillis());%0D%0A
         executor
@@ -3057,32 +3057,32 @@
 ch.countDown()%0D%0A
-
                 
@@ -3076,32 +3076,89 @@
              );%0D
+%0A        System.err.println(System.currentTimeMillis());%0D
 %0A%0D%0A        asser

Test fix - test travis