01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.pool;
17:
18: import junit.framework.TestCase;
19:
20: /**
21: * @version $Rev: 598759 $ $Date: 2007-11-27 12:32:26 -0800 (Tue, 27 Nov 2007) $
22: */
23: public class ThreadPoolTest extends TestCase {
24: private final Object lock = new Object();
25: private boolean ready;
26: private ThreadPool threadPool;
27:
28: public void testPoolLimit() throws Exception {
29: // grab the only thread in the pool
30: ready = false;
31: threadPool.execute(new Runnable() {
32: public void run() {
33: synchronized (lock) {
34: ready = true;
35: lock.notifyAll();
36: try {
37: lock.wait();
38: } catch (InterruptedException e) {
39: }
40: }
41: }
42: });
43:
44: // wait for up to 5 seconds for the thread above to start running
45: synchronized (lock) {
46: if (!ready) {
47: lock.wait(5000);
48: }
49: }
50: assertTrue(ready);
51:
52: // try to schedule another task
53: try {
54: threadPool.execute(new Runnable() {
55: public void run() {
56: }
57: });
58: fail("Should not have been able to schedule second task");
59: } catch (RuntimeException e) {
60: // expected
61: }
62: }
63:
64: public void setUp() throws Exception {
65: threadPool = new ThreadPool(1, 1, "foo", Long.MAX_VALUE,
66: ThreadPoolTest.class.getClassLoader(), "foo:bar=baz");
67: threadPool.doStart();
68: }
69:
70: public void tearDown() throws Exception {
71: threadPool.doStop();
72: synchronized (lock) {
73: lock.notifyAll();
74: }
75: }
76: }
|