/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.dubbo.common.concurrent;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.junit.Ignore;
import org.junit.Test;


public class CompletableFutureTaskTest {

    private static final ExecutorService executor = new ThreadPoolExecutor(0, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("DubboMonitorCreator", true));

    @Test
    public void testCreate() throws InterruptedException {

        final CountDownLatch countDownLatch = new CountDownLatch(1);
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
            countDownLatch.countDown();
            return true;
        },executor);
        countDownLatch.await();
    }

    @Test
    public void testRunnableResponse() throws ExecutionException, InterruptedException {
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }, executor);

        Boolean result = completableFuture.get();
        assertThat(result, is(true));
    }

    @Test
    public void testListener() throws InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";

        },executor);
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        completableFuture.thenRunAsync(new Runnable() {
            @Override
            public void run() {
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }


    @Test
    @Ignore
    public void testCustomExecutor() {
        Executor mockedExecutor = mock(Executor.class);
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            return 0;
        });
        completableFuture.thenRunAsync(mock(Runnable.class), verify(mockedExecutor));
    }
}
@@ -1008,16 +1008,105 @@
 .verify;
+%0Aimport static org.mockito.ArgumentMatchers.any;%0Aimport static org.mockito.Mockito.times;
 %0A%0Aimport
@@ -3602,15 +3602,8 @@
 s), 
-verify(
 mock
@@ -3612,16 +3612,72 @@
 Executor
+);%0A        verify(mockedExecutor, times(1)).execute(any(
 ));%0A    

fix testCustomExecutor (#2904)