Search in sources :

Example 1 with ConcurrencyThrottleInterceptor

use of cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor in project today-infrastructure by TAKETODAY.

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor) Test(org.junit.jupiter.api.Test)

Example 2 with ConcurrencyThrottleInterceptor

use of cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor in project today-infrastructure by TAKETODAY.

the class ConcurrencyThrottleInterceptorTests method testMultipleThreads.

private void testMultipleThreads(int concurrencyLimit) {
    TestBean tb = new TestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    cti.setConcurrencyLimit(concurrencyLimit);
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    Thread[] threads = new Thread[NR_OF_THREADS];
    for (int i = 0; i < NR_OF_THREADS; i++) {
        threads[i] = new ConcurrencyThread(proxy, null);
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS / 10; i++) {
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threads[i] = new ConcurrencyThread(proxy, i % 2 == 0 ? new OutOfMemoryError() : new IllegalStateException());
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor)

Example 3 with ConcurrencyThrottleInterceptor

use of cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor in project today-framework by TAKETODAY.

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor) Test(org.junit.jupiter.api.Test)

Example 4 with ConcurrencyThrottleInterceptor

use of cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor in project today-framework by TAKETODAY.

the class ConcurrencyThrottleInterceptorTests method testMultipleThreads.

private void testMultipleThreads(int concurrencyLimit) {
    TestBean tb = new TestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    cti.setConcurrencyLimit(concurrencyLimit);
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    Thread[] threads = new Thread[NR_OF_THREADS];
    for (int i = 0; i < NR_OF_THREADS; i++) {
        threads[i] = new ConcurrencyThread(proxy, null);
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS / 10; i++) {
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threads[i] = new ConcurrencyThread(proxy, i % 2 == 0 ? new OutOfMemoryError() : new IllegalStateException());
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor)

Aggregations

ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)4 ConcurrencyThrottleInterceptor (cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor)4 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)4 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)4 Advised (cn.taketoday.aop.framework.Advised)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 Test (org.junit.jupiter.api.Test)2