From e8b6bb94806fa57850272950f6654aa34ad30a59 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 23 Jun 2017 12:54:53 +0800 Subject: [PATCH] add test case for #75 , test should FAIL! add test case for ThreadPoolExecutor#remove in AgentCheck --- src/test/java/com/alibaba/ttl/Utils.java | 4 +- .../ttl/threadpool/agent/AgentCheck.java | 60 ++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/alibaba/ttl/Utils.java b/src/test/java/com/alibaba/ttl/Utils.java index 73119faa0..14378cf8d 100644 --- a/src/test/java/com/alibaba/ttl/Utils.java +++ b/src/test/java/com/alibaba/ttl/Utils.java @@ -19,9 +19,9 @@ public final class Utils { public static final String PARENT_MODIFIED_IN_CHILD = "parent-created-modified-in-child"; public static final String PARENT_AFTER_CREATE_TTL_TASK = "parent-created-after-create-TtlTask"; public static final String CHILD = "child-created"; - + private Utils() { - throw new InstantiationError( "Must not instantiate this class" ); + throw new InstantiationError("Must not instantiate this class"); } public static ConcurrentMap> createTestTtlValue() { diff --git a/src/test/java/com/alibaba/ttl/threadpool/agent/AgentCheck.java b/src/test/java/com/alibaba/ttl/threadpool/agent/AgentCheck.java index a69c44fca..9487298c7 100644 --- a/src/test/java/com/alibaba/ttl/threadpool/agent/AgentCheck.java +++ b/src/test/java/com/alibaba/ttl/threadpool/agent/AgentCheck.java @@ -4,15 +4,19 @@ import com.alibaba.ttl.Utils; import com.alibaba.ttl.testmodel.Task; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import static com.alibaba.ttl.Utils.CHILD; import static com.alibaba.ttl.Utils.PARENT_AFTER_CREATE_TTL_TASK; @@ -22,6 +26,8 @@ import static com.alibaba.ttl.Utils.copied; import static com.alibaba.ttl.Utils.createTestTtlValue; import static com.alibaba.ttl.Utils.expandThreadPool; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; /** * @author Jerry Lee (oldratlee at gmail dot com) @@ -35,7 +41,7 @@ private AgentCheck() { public static void main(String[] args) throws Exception { try { ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 3, - 3L, TimeUnit.SECONDS, + 10L, TimeUnit.SECONDS, new LinkedBlockingQueue()); ScheduledThreadPoolExecutor scheduledExecutorService = new ScheduledThreadPoolExecutor(3); @@ -45,6 +51,7 @@ public static void main(String[] args) throws Exception { ConcurrentMap> ttlInstances = createTestTtlValue(); checkExecutorService(executorService, ttlInstances); + checkThreadPoolExecutorForRemoveMethod(executorService); checkScheduledExecutorService(scheduledExecutorService, ttlInstances); System.out.println(); @@ -64,7 +71,8 @@ public static void main(String[] args) throws Exception { System.exit(1); } } catch (Throwable e) { - e.printStackTrace(); + System.out.println("Exception when run AgentCheck: "); + e.printStackTrace(System.out); System.exit(2); } } @@ -97,6 +105,52 @@ private static void checkExecutorService(ExecutorService executorService, Concur ); } + private static void checkThreadPoolExecutorForRemoveMethod(ThreadPoolExecutor executor) throws Exception { + List> sleepTasks = new ArrayList>(); + + final int COUNT = 4; + for (int i = 0; i < COUNT; i++) { + FutureTask futureTask = new FutureTask(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + System.out.println("Run sleep task!"); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }, null); + + sleepTasks.add(futureTask); + executor.execute(futureTask); + } + + final FutureTask taskToRemove = + new FutureTask(new Runnable() { + @Override + public void run() { + System.out.println("Run taskToRemove!"); + } + }, null); + + executor.execute(taskToRemove); + executor.remove(taskToRemove); + + // wait sleep task finished. + for (FutureTask sleepTask : sleepTasks) { + sleepTask.get(); + } + + ///////////////////////////////////////////////////////////// + // Is ThreadPoolExecutor#remove method take effect? + ///////////////////////////////////////////////////////////// + assertEquals(0, executor.getActiveCount()); + assertFalse(taskToRemove.isDone()); + assertFalse(taskToRemove.isCancelled()); // task is directly removed from work queue, so not cancelled! + + } + private static void checkScheduledExecutorService(ScheduledExecutorService scheduledExecutorService, ConcurrentMap> ttlInstances) throws Exception { Task task = new Task("2", ttlInstances); ScheduledFuture future = scheduledExecutorService.schedule(task, 200, TimeUnit.MILLISECONDS);