Search in sources :

Example 1 with BlockMyInvoker

use of org.apache.dubbo.rpc.support.BlockMyInvoker in project dubbo by alibaba.

the class ActiveLimitFilterTest method testInvokeTimeOut.

@Test
public void testInvokeTimeOut() throws Exception {
    int totalThread = 100;
    int maxActives = 10;
    long timeout = 1;
    long blockTime = 100;
    AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latchBlocking = new CountDownLatch(totalThread);
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=" + maxActives + "&timeout=" + timeout);
    final Invoker<ActiveLimitFilterTest> invoker = new BlockMyInvoker<ActiveLimitFilterTest>(url, blockTime);
    final Invocation invocation = new MockInvocation();
    RpcStatus.removeStatus(url);
    RpcStatus.removeStatus(url, invocation.getMethodName());
    for (int i = 0; i < totalThread; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Result asyncResult = activeLimitFilter.invoke(invoker, invocation);
                        Result result = asyncResult.get();
                        activeLimitFilter.onResponse(result, invoker, invocation);
                    } catch (RpcException expected) {
                        count.incrementAndGet();
                    } catch (Exception e) {
                        fail();
                    }
                } finally {
                    latchBlocking.countDown();
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        latchBlocking.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertEquals(90, count.intValue());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) Test(org.junit.jupiter.api.Test)

Example 2 with BlockMyInvoker

use of org.apache.dubbo.rpc.support.BlockMyInvoker in project dubbo by alibaba.

the class ActiveLimitFilterTest method testInvokeGreaterActives.

@Test
public void testInvokeGreaterActives() {
    AtomicInteger count = new AtomicInteger(0);
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=1&timeout=1");
    final Invoker<ActiveLimitFilterTest> invoker = new BlockMyInvoker<ActiveLimitFilterTest>(url, 100);
    final Invocation invocation = new MockInvocation();
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < 100; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 100; i++) {
                    try {
                        activeLimitFilter.invoke(invoker, invocation);
                    } catch (RpcException expected) {
                        count.incrementAndGet();
                    }
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertNotSame(0, count.intValue());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) Test(org.junit.jupiter.api.Test)

Example 3 with BlockMyInvoker

use of org.apache.dubbo.rpc.support.BlockMyInvoker in project dubbo by alibaba.

the class ActiveLimitFilterTest method testInvokeNotTimeOut.

@Test
public void testInvokeNotTimeOut() throws Exception {
    int totalThread = 100;
    int maxActives = 10;
    long timeout = 1000;
    long blockTime = 0;
    AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latchBlocking = new CountDownLatch(totalThread);
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=" + maxActives + "&timeout=" + timeout);
    final Invoker<ActiveLimitFilterTest> invoker = new BlockMyInvoker<ActiveLimitFilterTest>(url, blockTime);
    final Invocation invocation = new MockInvocation();
    for (int i = 0; i < totalThread; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Result asyncResult = activeLimitFilter.invoke(invoker, invocation);
                        Result result = asyncResult.get();
                        activeLimitFilter.onResponse(result, invoker, invocation);
                    } catch (RpcException expected) {
                        count.incrementAndGet();
                        activeLimitFilter.onError(expected, invoker, invocation);
                    } catch (Exception e) {
                        fail();
                    }
                } finally {
                    latchBlocking.countDown();
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        latchBlocking.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertEquals(0, count.intValue());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) Test(org.junit.jupiter.api.Test)

Example 4 with BlockMyInvoker

use of org.apache.dubbo.rpc.support.BlockMyInvoker in project dubbo by alibaba.

the class ExecuteLimitFilterTest method testMoreThanExecuteLimitInvoke.

@Test
public void testMoreThanExecuteLimitInvoke() throws Exception {
    int maxExecute = 10;
    int totalExecute = 20;
    final AtomicInteger failed = new AtomicInteger(0);
    final Invocation invocation = Mockito.mock(Invocation.class);
    when(invocation.getMethodName()).thenReturn("testMoreThanExecuteLimitInvoke");
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&executes=" + maxExecute);
    final Invoker<ExecuteLimitFilter> invoker = new BlockMyInvoker<ExecuteLimitFilter>(url, 1000);
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < totalExecute; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    executeLimitFilter.invoke(invoker, invocation);
                } catch (RpcException expected) {
                    failed.incrementAndGet();
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Assertions.assertEquals(totalExecute - maxExecute, failed.get());
}
Also used : BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) Invocation(org.apache.dubbo.rpc.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 5 with BlockMyInvoker

use of org.apache.dubbo.rpc.support.BlockMyInvoker in project dubbo by alibaba.

the class TimeoutFilterTest method testInvokeWithTimeout.

@Test
public void testInvokeWithTimeout() throws Exception {
    int timeout = 100;
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&timeout=" + timeout);
    Invoker invoker = new BlockMyInvoker(url, (timeout + 100));
    Invocation invocation = Mockito.mock(Invocation.class);
    when(invocation.getMethodName()).thenReturn("testInvokeWithTimeout");
    Result result = timeoutFilter.invoke(invoker, invocation);
    Assertions.assertEquals("Dubbo", result.getValue());
}
Also used : BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

URL (org.apache.dubbo.common.URL)5 Invocation (org.apache.dubbo.rpc.Invocation)5 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)5 Test (org.junit.jupiter.api.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RpcException (org.apache.dubbo.rpc.RpcException)4 Result (org.apache.dubbo.rpc.Result)3 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)3 Invoker (org.apache.dubbo.rpc.Invoker)1