1 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 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 { 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 [] {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 [] threads = new Thread [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 ex) { 73 ex.printStackTrace(); 74 } 75 threads[i] = new ConcurrencyTest(proxy, 76 i % 2 == 0 ? (Throwable ) new OutOfMemoryError () : (Throwable ) new IllegalStateException ()); 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 ex) { 84 ex.printStackTrace(); 85 } 86 } 87 } 88 89 90 private static class ConcurrencyTest extends Thread { 91 92 private ITestBean proxy; 93 private Throwable ex; 94 95 public ConcurrencyTest(ITestBean proxy, Throwable 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 ex) { 106 if (ex == this.ex) { 107 logger.debug("Expected exception thrown", ex); 108 } 109 else { 110 ex.printStackTrace(); 112 } 113 } 114 catch (Error err) { 115 if (err == this.ex) { 116 logger.debug("Expected exception thrown", err); 117 } 118 else { 119 ex.printStackTrace(); 121 } 122 } 123 catch (Throwable ex) { 124 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 |