Search in sources :

Example 16 with Result

use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.

the class GenericImplFilterTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    RpcInvocation invocation = new RpcInvocation("getPerson", "org.apache.dubbo.rpc.support.DemoService", "org.apache.dubbo.rpc.support.DemoService:dubbo", new Class[] { Person.class }, new Object[] { new Person("dubbo", 10) });
    URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1&generic=true");
    Invoker invoker = Mockito.mock(Invoker.class);
    Map<String, Object> person = new HashMap<String, Object>();
    person.put("name", "dubbo");
    person.put("age", 10);
    AppResponse mockRpcResult = new AppResponse(person);
    when(invoker.invoke(any(Invocation.class))).thenReturn(AsyncRpcResult.newDefaultAsyncResult(mockRpcResult, invocation));
    when(invoker.getUrl()).thenReturn(url);
    when(invoker.getInterface()).thenReturn(DemoService.class);
    Result asyncResult = genericImplFilter.invoke(invoker, invocation);
    Result result = asyncResult.get();
    genericImplFilter.onResponse(result, invoker, invocation);
    Assertions.assertEquals(Person.class, result.getValue().getClass());
    Assertions.assertEquals(10, ((Person) result.getValue()).getAge());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) AppResponse(org.apache.dubbo.rpc.AppResponse) Person(org.apache.dubbo.rpc.support.Person) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 17 with Result

use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.

the class GenericFilterTest method testInvokeWithDefault.

@Test
public void testInvokeWithDefault() throws Exception {
    Method genericInvoke = GenericService.class.getMethods()[0];
    Map<String, Object> person = new HashMap<String, Object>();
    person.put("name", "dubbo");
    person.put("age", 10);
    RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), "", genericInvoke.getParameterTypes(), new Object[] { "getPerson", new String[] { Person.class.getCanonicalName() }, new Object[] { person } });
    URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" + "accesslog=true&group=dubbo&version=1.1");
    Invoker invoker = Mockito.mock(Invoker.class);
    when(invoker.invoke(any(Invocation.class))).thenReturn(AsyncRpcResult.newDefaultAsyncResult(new Person("person", 10), invocation));
    when(invoker.getUrl()).thenReturn(url);
    when(invoker.getInterface()).thenReturn(DemoService.class);
    Result asyncResult = genericFilter.invoke(invoker, invocation);
    AppResponse appResponse = (AppResponse) asyncResult.get();
    genericFilter.onResponse(appResponse, invoker, invocation);
    Assertions.assertEquals(HashMap.class, appResponse.getValue().getClass());
    Assertions.assertEquals(10, ((HashMap) appResponse.getValue()).get("age"));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) GenericService(org.apache.dubbo.rpc.service.GenericService) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Invoker(org.apache.dubbo.rpc.Invoker) AppResponse(org.apache.dubbo.rpc.AppResponse) Person(org.apache.dubbo.rpc.support.Person) Test(org.junit.jupiter.api.Test)

Example 18 with Result

use of org.apache.dubbo.rpc.Result 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 19 with Result

use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.

the class CompatibleFilterFilterTest method testInvokerGeneric.

@Test
public void testInvokerGeneric() {
    invocation = mock(Invocation.class);
    given(invocation.getMethodName()).willReturn("$enumlength");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[] { Enum.class });
    given(invocation.getArguments()).willReturn(new Object[] { "hello" });
    invoker = mock(Invoker.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getInterface()).willReturn(DemoService.class);
    AppResponse result = new AppResponse();
    result.setValue("High");
    given(invoker.invoke(invocation)).willReturn(result);
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    given(invoker.getUrl()).willReturn(url);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals(filterResult, result);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) Invoker(org.apache.dubbo.rpc.Invoker) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 20 with Result

use of org.apache.dubbo.rpc.Result in project dubbo by alibaba.

the class CompatibleFilterFilterTest method testInvokerJsonPojoSerialization.

@Test
public void testInvokerJsonPojoSerialization() throws Exception {
    invocation = mock(Invocation.class);
    given(invocation.getMethodName()).willReturn("enumlength");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[] { Type[].class });
    given(invocation.getArguments()).willReturn(new Object[] { "hello" });
    invoker = mock(Invoker.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getInterface()).willReturn(DemoService.class);
    AppResponse result = new AppResponse();
    result.setValue("High");
    given(invoker.invoke(invocation)).willReturn(AsyncRpcResult.newDefaultAsyncResult(result, invocation));
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1&serialization=json");
    given(invoker.getUrl()).willReturn(url);
    Result asyncResult = compatibleFilter.invoke(invoker, invocation);
    AppResponse appResponse = (AppResponse) asyncResult.get();
    compatibleFilter.onResponse(appResponse, invoker, invocation);
    assertEquals(Type.High, appResponse.getValue());
}
Also used : Type(org.apache.dubbo.rpc.support.Type) Invocation(org.apache.dubbo.rpc.Invocation) Invoker(org.apache.dubbo.rpc.Invoker) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

Result (org.apache.dubbo.rpc.Result)97 Test (org.junit.jupiter.api.Test)74 URL (org.apache.dubbo.common.URL)55 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)39 Invocation (org.apache.dubbo.rpc.Invocation)33 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)31 AppResponse (org.apache.dubbo.rpc.AppResponse)28 Invoker (org.apache.dubbo.rpc.Invoker)27 RpcException (org.apache.dubbo.rpc.RpcException)22 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)7 RpcContext (org.apache.dubbo.rpc.RpcContext)7 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)6 DemoService (org.apache.dubbo.rpc.support.DemoService)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Protocol (org.apache.dubbo.rpc.Protocol)5 Person (org.apache.dubbo.rpc.support.Person)5 Method (java.lang.reflect.Method)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4