Search in sources :

Example 1 with MockInvocation

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

the class AccessLogFilterTest method testInvokeException.

// Test filter won't throw an exception
@Test
public void testInvokeException() {
    Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(null);
    Invocation invocation = new MockInvocation();
    LogUtil.start();
    accessLogFilter.invoke(invoker, invocation);
    assertEquals(1, LogUtil.findMessage("Exception in AccessLogFilter of service"));
    LogUtil.stop();
    DubboAppender.clear();
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) Test(org.junit.jupiter.api.Test)

Example 2 with MockInvocation

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

the class AccessLogFilterTest method testDefault.

// TODO how to assert thread action
@Test
@SuppressWarnings("unchecked")
public void testDefault() throws NoSuchFieldException, IllegalAccessException {
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1");
    Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url);
    Invocation invocation = new MockInvocation();
    Field field = AccessLogFilter.class.getDeclaredField("LOG_ENTRIES");
    ReflectUtils.makeAccessible(field);
    assertTrue(((Map) field.get(AccessLogFilter.class)).isEmpty());
    accessLogFilter.invoke(invoker, invocation);
    Map<String, Queue<AccessLogData>> logs = (Map<String, Queue<AccessLogData>>) field.get(AccessLogFilter.class);
    assertFalse(logs.isEmpty());
    assertFalse(logs.get("true").isEmpty());
    AccessLogData log = logs.get("true").iterator().next();
    assertEquals("org.apache.dubbo.rpc.support.DemoService", log.getServiceName());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) URL(org.apache.dubbo.common.URL) Field(java.lang.reflect.Field) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) AccessLogData(org.apache.dubbo.rpc.support.AccessLogData) Queue(java.util.Queue) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with MockInvocation

use of org.apache.dubbo.rpc.support.MockInvocation 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 4 with MockInvocation

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

the class ActiveLimitFilterTest method testInvokeLessActives.

@Test
public void testInvokeLessActives() {
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=10");
    Invoker<ActiveLimitFilterTest> invoker = new MyInvoker<ActiveLimitFilterTest>(url);
    Invocation invocation = new MockInvocation();
    activeLimitFilter.invoke(invoker, invocation);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 5 with MockInvocation

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

the class ActiveLimitFilterTest method testInvokeRuntimeExceptionWithActiveCountMatch.

@Test
public void testInvokeRuntimeExceptionWithActiveCountMatch() {
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&actives=0");
    Invoker<ActiveLimitFilterTest> invoker = new RuntimeExceptionInvoker(url);
    Invocation invocation = new MockInvocation();
    RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());
    int beforeExceptionActiveCount = count.getActive();
    try {
        activeLimitFilter.invoke(invoker, invocation);
    } catch (RuntimeException ex) {
        activeLimitFilter.onError(ex, invoker, invocation);
        int afterExceptionActiveCount = count.getActive();
        assertEquals(beforeExceptionActiveCount, afterExceptionActiveCount, "After exception active count should be same");
    }
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) RuntimeExceptionInvoker(org.apache.dubbo.rpc.support.RuntimeExceptionInvoker) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) RpcStatus(org.apache.dubbo.rpc.RpcStatus) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)17 Test (org.junit.jupiter.api.Test)17 URL (org.apache.dubbo.common.URL)16 Invocation (org.apache.dubbo.rpc.Invocation)16 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)8 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)5 RpcException (org.apache.dubbo.rpc.RpcException)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Result (org.apache.dubbo.rpc.Result)3 RpcStatus (org.apache.dubbo.rpc.RpcStatus)2 DemoService (org.apache.dubbo.rpc.support.DemoService)2 RuntimeExceptionInvoker (org.apache.dubbo.rpc.support.RuntimeExceptionInvoker)2 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Queue (java.util.Queue)1 AccessLogData (org.apache.dubbo.rpc.support.AccessLogData)1