KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > interceptor > ConcurrencyThrottleInterceptorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.interceptor;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import org.springframework.aop.framework.ProxyFactory;
24 import org.springframework.beans.ITestBean;
25 import org.springframework.beans.TestBean;
26 import org.springframework.util.SerializationTestUtils;
27
28 /**
29  * @author Juergen Hoeller
30  * @since 06.04.2004
31  */

32 public class ConcurrencyThrottleInterceptorTests extends TestCase {
33
34     protected static final Log logger = LogFactory.getLog(ConcurrencyThrottleInterceptorTests.class);
35
36     public static final int NR_OF_THREADS = 100;
37
38     public static final int NR_OF_ITERATIONS = 1000;
39     
40     public void testSerializable() throws Exception JavaDoc {
41         ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
42         SerializationTestUtils.testSerialization(cti);
43     }
44
45     public void testMultipleThreadsWithLimit1() {
46         testMultipleThreads(1);
47     }
48
49     public void testMultipleThreadsWithLimit10() {
50         testMultipleThreads(10);
51     }
52
53     private void testMultipleThreads(int concurrencyLimit) {
54         TestBean tb = new TestBean();
55         ProxyFactory proxyFactory = new ProxyFactory();
56         proxyFactory.setInterfaces(new Class JavaDoc[] {ITestBean.class});
57         ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
58         cti.setConcurrencyLimit(concurrencyLimit);
59         proxyFactory.addAdvice(cti);
60         proxyFactory.setTarget(tb);
61         ITestBean proxy = (ITestBean) proxyFactory.getProxy();
62
63         Thread JavaDoc[] threads = new Thread JavaDoc[NR_OF_THREADS];
64         for (int i = 0; i < NR_OF_THREADS; i++) {
65             threads[i] = new ConcurrencyTest(proxy, null);
66             threads[i].start();
67         }
68         for (int i = 0; i < NR_OF_THREADS / 10; i++) {
69             try {
70                 Thread.sleep(5);
71             }
72             catch (InterruptedException JavaDoc ex) {
73                 ex.printStackTrace();
74             }
75             threads[i] = new ConcurrencyTest(proxy,
76                     i % 2 == 0 ? (Throwable JavaDoc) new OutOfMemoryError JavaDoc() : (Throwable JavaDoc) new IllegalStateException JavaDoc());
77             threads[i].start();
78         }
79         for (int i = 0; i < NR_OF_THREADS; i++) {
80             try {
81                 threads[i].join();
82             }
83             catch (InterruptedException JavaDoc ex) {
84                 ex.printStackTrace();
85             }
86         }
87     }
88
89
90     private static class ConcurrencyTest extends Thread JavaDoc {
91
92         private ITestBean proxy;
93         private Throwable JavaDoc ex;
94
95         public ConcurrencyTest(ITestBean proxy, Throwable JavaDoc ex) {
96             this.proxy = proxy;
97             this.ex = ex;
98         }
99
100         public void run() {
101             if (this.ex != null) {
102                 try {
103                     this.proxy.exceptional(this.ex);
104                 }
105                 catch (RuntimeException JavaDoc ex) {
106                     if (ex == this.ex) {
107                         logger.debug("Expected exception thrown", ex);
108                     }
109                     else {
110                         // should never happen
111
ex.printStackTrace();
112                     }
113                 }
114                 catch (Error JavaDoc err) {
115                     if (err == this.ex) {
116                         logger.debug("Expected exception thrown", err);
117                     }
118                     else {
119                         // should never happen
120
ex.printStackTrace();
121                     }
122                 }
123                 catch (Throwable JavaDoc ex) {
124                     // should never happen
125
ex.printStackTrace();
126                 }
127             }
128             else {
129                 for (int i = 0; i < NR_OF_ITERATIONS; i++) {
130                     this.proxy.getName();
131                 }
132             }
133             logger.debug("finished");
134         }
135     }
136
137 }
138
Popular Tags