Skip to content

Commit

Permalink
add test case for #75 , test should FAIL!
Browse files Browse the repository at this point in the history
add test case for ThreadPoolExecutor#remove in AgentCheck
  • Loading branch information
oldratlee committed Jun 24, 2017
1 parent af62f18 commit e8b2f3d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/test/java/com/alibaba/ttl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, TransmittableThreadLocal<String>> createTestTtlValue() {
Expand Down
60 changes: 57 additions & 3 deletions src/test/java/com/alibaba/ttl/threadpool/agent/AgentCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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<Runnable>());
ScheduledThreadPoolExecutor scheduledExecutorService = new ScheduledThreadPoolExecutor(3);

Expand All @@ -45,6 +51,7 @@ public static void main(String[] args) throws Exception {
ConcurrentMap<String, TransmittableThreadLocal<String>> ttlInstances = createTestTtlValue();

checkExecutorService(executorService, ttlInstances);
checkThreadPoolExecutorForRemoveMethod(executorService);
checkScheduledExecutorService(scheduledExecutorService, ttlInstances);

System.out.println();
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -97,6 +105,52 @@ private static void checkExecutorService(ExecutorService executorService, Concur
);
}

private static void checkThreadPoolExecutorForRemoveMethod(ThreadPoolExecutor executor) throws Exception {
List<FutureTask<?>> sleepTasks = new ArrayList<FutureTask<?>>();

final int COUNT = 4;
for (int i = 0; i < COUNT; i++) {
FutureTask<?> futureTask = new FutureTask<Object>(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<Object>(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<String, TransmittableThreadLocal<String>> ttlInstances) throws Exception {
Task task = new Task("2", ttlInstances);
ScheduledFuture<?> future = scheduledExecutorService.schedule(task, 200, TimeUnit.MILLISECONDS);
Expand Down

0 comments on commit e8b2f3d

Please sign in to comment.