001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.thread;
018:
019: import org.apache.avalon.framework.logger.ConsoleLogger;
020: import org.easymock.MockControl;
021:
022: /**
023: * The $classType$ class ...
024: *
025: * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati</a>
026: * @version $Id$
027: */
028: public class DefaultThreadPoolTestCase extends AbstractTestCase {
029: //~ Methods ----------------------------------------------------------------
030:
031: /**
032: * DOCUMENT ME!
033: */
034: public final void testDefaultThreadPool() {
035: final DefaultThreadPool pool = new DefaultThreadPool();
036: pool
037: .enableLogging(new ConsoleLogger(
038: ConsoleLogger.LEVEL_DEBUG));
039: pool.setName("mypool");
040:
041: // We cannot mock the DefaultThreadFactory as the underlying
042: // PooledExecutor of the DefaultThreadPool will again wrapp it into a
043: // PooledExecutor.Worker instance that does some bookeeping.
044: // Using a easymocked DefaultThreadFactory will prevent the
045: // PooledExecutor from shutting down and thus hangs forever.
046: final ThreadFactory threadFactory = new DefaultThreadFactory();
047: threadFactory.setPriority(Thread.MAX_PRIORITY);
048: pool.setThreadFactory(threadFactory);
049: pool.setQueue(230);
050: pool.setMaximumPoolSize(15);
051: pool.setMinimumPoolSize(9);
052: pool.setKeepAliveTime(11000);
053: pool.setBlockPolicy("ABORT");
054: pool.setShutdownGraceful(false);
055: pool.setShutdownWaitTimeMs(12345);
056:
057: assertEquals("block-policy", "ABORT", pool.getBlockPolicy());
058: assertEquals("keep-alive-time-ms", 11000L, pool
059: .getKeepAliveTime());
060: assertEquals("max-queueu-size", 230, pool.getMaximumQueueSize());
061: assertEquals("max-pool-size", 15, pool.getMaximumPoolSize());
062: assertEquals("min-pool-size", 9, pool.getMinimumPoolSize());
063: assertEquals("name", "mypool", pool.getName());
064: assertEquals("priority", Thread.MAX_PRIORITY, pool
065: .getPriority());
066: assertEquals("queue-size", 0, pool.getQueueSize());
067: assertEquals("isQueued", true, pool.isQueued());
068: assertEquals("isTerminatedAfterShutdown", false, pool
069: .isTerminatedAfterShutdown());
070: verify();
071: }
072:
073: /*
074: * Class under test for void execute(Runnable)
075: */
076: public final void testExecuteRunnable() throws InterruptedException {
077: final MockControl runnableControl = createStrictControl(Runnable.class);
078: final Runnable runnable = (Runnable) runnableControl.getMock();
079: runnable.run();
080: runnableControl.replay();
081:
082: final DefaultThreadPool pool = new DefaultThreadPool();
083: pool
084: .enableLogging(new ConsoleLogger(
085: ConsoleLogger.LEVEL_DEBUG));
086: pool.setName("mypool");
087: // We cannot mock the DefaultThreadFactory as the underlying
088: // PooledExecutor of the DefaultThreadPool will again wrapp it into a
089: // PooledExecutor.Worker instance that does some bookeeping.
090: // Using a easymocked DefaultThreadFactory will prevent the
091: // PooledExecutor from shutting down and thus hangs forever.
092: pool.setThreadFactory(new DefaultThreadFactory());
093: pool.setQueue(230);
094: pool.setMaximumPoolSize(15);
095: pool.setMinimumPoolSize(9);
096: pool.setKeepAliveTime(100);
097: pool.setBlockPolicy("ABORT");
098: pool.setShutdownGraceful(false);
099: pool.setShutdownWaitTimeMs(1234);
100: pool.execute(runnable);
101: Thread.yield();
102: Thread.sleep(100);
103: pool.shutdown();
104: verify();
105: }
106:
107: /**
108: * DOCUMENT ME!
109: *
110: * @throws InterruptedException DOCUMENT ME!
111: */
112: public final void testShutdown() throws InterruptedException {
113: final Runnable runnable = new Runnable() {
114: public void run() {
115: final ConsoleLogger logger = new ConsoleLogger(
116: ConsoleLogger.LEVEL_DEBUG);
117: logger.info("runnable runs");
118: try {
119: Thread.sleep(1000);
120: } catch (final InterruptedException ie) {
121: logger.info("runnable has been interrupted ");
122: }
123: logger.info("runnable terminated");
124: }
125: };
126:
127: final DefaultThreadPool pool = new DefaultThreadPool();
128: pool
129: .enableLogging(new ConsoleLogger(
130: ConsoleLogger.LEVEL_DEBUG));
131: pool.setName("mypool");
132: pool.setThreadFactory(new DefaultThreadFactory());
133: pool.setQueue(0);
134: pool.setMaximumPoolSize(15);
135: pool.setMinimumPoolSize(9);
136: pool.setKeepAliveTime(1000);
137: pool.setBlockPolicy("ABORT");
138: pool.setShutdownGraceful(true);
139: pool.setShutdownWaitTimeMs(100);
140: pool.execute(runnable);
141: pool.execute(runnable);
142: Thread.yield();
143: Thread.sleep(200);
144: pool.shutdown();
145: Thread.sleep(200);
146: verify();
147: }
148: }
|