|
|
|
@ -22,20 +22,23 @@ import org.junit.Assert;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Synchronous placement queue policy implementation test
|
|
|
|
|
*/
|
|
|
|
|
public class SyncPutQueuePolicyTest {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test thread pool rejected execution
|
|
|
|
|
* test thread pool rejected execution without timeout
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testRejectedExecution() {
|
|
|
|
|
public void testRejectedExecutionWithoutTimeout() {
|
|
|
|
|
SyncPutQueuePolicy syncPutQueuePolicy = new SyncPutQueuePolicy();
|
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2,
|
|
|
|
|
60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), syncPutQueuePolicy);
|
|
|
|
@ -50,4 +53,34 @@ public class SyncPutQueuePolicyTest {
|
|
|
|
|
}
|
|
|
|
|
Assert.assertEquals(4, threadPoolExecutor.getCompletedTaskCount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test thread pool rejected execution with timeout
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testRejectedExecutionWithTimeout() {
|
|
|
|
|
SyncPutQueuePolicy syncPutQueuePolicy = new SyncPutQueuePolicy(300);
|
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1,
|
|
|
|
|
60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), syncPutQueuePolicy);
|
|
|
|
|
threadPoolExecutor.prestartAllCoreThreads();
|
|
|
|
|
|
|
|
|
|
Assert.assertSame(syncPutQueuePolicy, threadPoolExecutor.getRejectedExecutionHandler());
|
|
|
|
|
IntStream.range(0, 4).forEach(s -> {
|
|
|
|
|
threadPoolExecutor.execute(() -> ThreadUtil.sleep(200L));
|
|
|
|
|
});
|
|
|
|
|
IntStream.range(0, 2).forEach(s -> {
|
|
|
|
|
threadPoolExecutor.execute(() -> ThreadUtil.sleep(500L));
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
threadPoolExecutor.execute(() -> ThreadUtil.sleep(100L));
|
|
|
|
|
ThreadUtil.sleep(1000L);
|
|
|
|
|
fail("should throw RejectedExecutionException");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Assert.assertTrue(e instanceof RejectedExecutionException);
|
|
|
|
|
}
|
|
|
|
|
threadPoolExecutor.shutdown();
|
|
|
|
|
while (!threadPoolExecutor.isTerminated()) {
|
|
|
|
|
}
|
|
|
|
|
Assert.assertEquals(6, threadPoolExecutor.getCompletedTaskCount());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|