001
014
015 package com.liferay.portal.kernel.concurrent.test;
016
017 import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
018
019 import java.util.concurrent.TimeUnit;
020
021
024 public class TestUtil {
025
026 public static final long KEEPALIVE_TIME = 50;
027
028 public static final long KEEPALIVE_WAIT = KEEPALIVE_TIME * 2;
029
030 public static final long LONG_WAIT = 30 * 1000;
031
032 public static final long SHORT_WAIT = 10;
033
034 public static void closePool(ThreadPoolExecutor threadPoolExecutor) {
035 closePool(threadPoolExecutor, false);
036 }
037
038 public static void closePool(
039 ThreadPoolExecutor threadPoolExecutor, boolean force) {
040
041 try {
042 if (force) {
043 threadPoolExecutor.shutdownNow();
044 }
045 else {
046 threadPoolExecutor.shutdown();
047 }
048
049 if (!threadPoolExecutor.awaitTermination(
050 LONG_WAIT, TimeUnit.MILLISECONDS)) {
051
052 throw new IllegalStateException();
053 }
054
055 if (!threadPoolExecutor.isTerminated()) {
056 throw new IllegalStateException();
057 }
058 }
059 catch (InterruptedException ie) {
060 throw new RuntimeException();
061 }
062 }
063
064 public static void unblock(MarkerBlockingJob... markerBlockingJobs) {
065 for (MarkerBlockingJob markerBlockingJob : markerBlockingJobs) {
066 markerBlockingJob.unBlock();
067 }
068 }
069
070 public static void waitUntilBlock(MarkerBlockingJob... markerBlockingJobs)
071 throws InterruptedException {
072
073 for (MarkerBlockingJob markerBlockingJob : markerBlockingJobs) {
074 markerBlockingJob.waitUntilBlock();
075 }
076 }
077
078 public static void waitUntilEnded(MarkerBlockingJob... markerBlockingJobs)
079 throws InterruptedException {
080
081 for (MarkerBlockingJob markerBlockingJob : markerBlockingJobs) {
082 markerBlockingJob.waitUntilEnded();
083 }
084
085 Thread.sleep(SHORT_WAIT);
086 }
087
088 }