001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
022     * @author Shuyang Zhou
023     */
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    }