Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-2187] Use CompletetableFuture to rewrite the embedded ListenableFuture #2213

Merged
merged 15 commits into from
Aug 13, 2018

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,85 +16,76 @@
*/
package org.apache.dubbo.common.concurrent;

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

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.*;

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

public class ListenableFutureTaskTest {

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);
ListenableFutureTask<Boolean> futureTask = ListenableFutureTask.create(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
countDownLatch.countDown();
return true;
}
});
futureTask.run();
CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
countDownLatch.countDown();
return true;
},executor);
countDownLatch.await();
}

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

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

@Test
public void testListener() throws InterruptedException {
ListenableFutureTask<String> futureTask = ListenableFutureTask.create(new Callable<String>() {
@Override
public String call() throws Exception {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500);
return "hello";
} catch (InterruptedException e) {
e.printStackTrace();
}
});
return "hello";

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


@Test
public void testCustomExecutor() {
Executor mockedExecutor = mock(Executor.class);
ListenableFutureTask<Integer> futureTask = ListenableFutureTask.create(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 0;
}
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
return 0;
});
futureTask.addListener(mock(Runnable.class), mockedExecutor);
futureTask.run();

verify(mockedExecutor).execute(any(Runnable.class));
completableFuture.thenRunAsync(mock(Runnable.class), verify(mockedExecutor));
}
}
Loading