Search in sources :

Example 1 with SofaRequest

use of com.alipay.sofa.rpc.core.request.SofaRequest in project Sentinel by alibaba.

the class SentinelSofaRpcProviderFilterTest method testInvokeSentinelWorks.

@Test
public void testInvokeSentinelWorks() {
    SentinelSofaRpcProviderFilter filter = new SentinelSofaRpcProviderFilter();
    final String applicationName = "demo-provider";
    final String interfaceResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService";
    final String methodResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService#sayHello(java.lang.String,int)";
    SofaRequest request = mock(SofaRequest.class);
    when(request.getRequestProp("app")).thenReturn(applicationName);
    when(request.getInvokeType()).thenReturn(RpcConstants.INVOKER_TYPE_SYNC);
    when(request.getInterfaceName()).thenReturn(interfaceResourceName);
    when(request.getMethodName()).thenReturn("sayHello");
    when(request.getMethodArgSigs()).thenReturn(new String[] { "java.lang.String", "int" });
    when(request.getMethodArgs()).thenReturn(new Object[] { "Sentinel", 2020 });
    FilterInvoker filterInvoker = mock(FilterInvoker.class);
    when(filterInvoker.invoke(request)).thenAnswer(new Answer<SofaResponse>() {

        @Override
        public SofaResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            verifyInvocationStructure(applicationName, interfaceResourceName, methodResourceName);
            SofaResponse response = new SofaResponse();
            response.setAppResponse("Hello Sentinel 2020");
            return response;
        }
    });
    // Before invoke
    assertNull(ContextUtil.getContext());
    // Do invoke
    SofaResponse response = filter.invoke(filterInvoker, request);
    assertEquals("Hello Sentinel 2020", response.getAppResponse());
    verify(filterInvoker).invoke(request);
    // After invoke, make sure exit context
    assertNull(ContextUtil.getContext());
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 2 with SofaRequest

use of com.alipay.sofa.rpc.core.request.SofaRequest in project Sentinel by alibaba.

the class SofaRpcUtilsTest method testGetApplicationName.

@Test
public void testGetApplicationName() {
    SofaRequest request = new SofaRequest();
    String applicationName = SofaRpcUtils.getApplicationName(request);
    assertEquals("", applicationName);
    request.addRequestProp("app", "test-app");
    applicationName = SofaRpcUtils.getApplicationName(request);
    assertEquals("test-app", applicationName);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Test(org.junit.Test)

Example 3 with SofaRequest

use of com.alipay.sofa.rpc.core.request.SofaRequest in project Sentinel by alibaba.

the class SofaRpcUtilsTest method testGetMethodArguments.

@Test
public void testGetMethodArguments() {
    SofaRequest request = new SofaRequest();
    request.setMethodArgs(new Object[] { "Sentinel", 2020 });
    Object[] arguments = SofaRpcUtils.getMethodArguments(request);
    assertEquals(arguments.length, 2);
    assertEquals("Sentinel", arguments[0]);
    assertEquals(2020, arguments[1]);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Test(org.junit.Test)

Example 4 with SofaRequest

use of com.alipay.sofa.rpc.core.request.SofaRequest in project Sentinel by alibaba.

the class SofaRpcFallbackRegistryTest method testCustomFallback.

@Test
public void testCustomFallback() {
    // Test invoke custom provider fallback
    SofaRpcFallbackRegistry.setProviderFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test provider response");
            return response;
        }
    });
    SofaResponse providerResponse = SofaRpcFallbackRegistry.getProviderFallback().handle(null, null, null);
    assertNotNull(providerResponse);
    assertEquals("test provider response", providerResponse.getAppResponse());
    // Test invoke custom consumer fallback
    SofaRpcFallbackRegistry.setConsumerFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test consumer response");
            return response;
        }
    });
    SofaResponse consumerResponse = SofaRpcFallbackRegistry.getConsumerFallback().handle(null, null, null);
    assertNotNull(consumerResponse);
    assertEquals("test consumer response", consumerResponse.getAppResponse());
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 5 with SofaRequest

use of com.alipay.sofa.rpc.core.request.SofaRequest in project httpx by servicex-sh.

the class SofaRpcInvocation method content.

public byte[] content() throws Exception {
    SofaRequest request = new SofaRequest();
    request.setTargetServiceUniqueName(this.targetServiceUniqueName);
    request.setMethodName(this.methodName);
    request.setMethodArgSigs(this.methodArgSigs);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Hessian2Output out = new HessianSerializerOutput(bos);
    out.writeObject(request);
    if (stubArguments != null) {
        for (Object argument : stubArguments) {
            out.writeObject(argument);
        }
    }
    out.flush();
    return bos.toByteArray();
}
Also used : HessianSerializerOutput(com.caucho.hessian.io.HessianSerializerOutput) Hessian2Output(com.caucho.hessian.io.Hessian2Output) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)75 Test (org.junit.Test)43 HashMap (java.util.HashMap)25 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)19 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)11 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)8 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)7 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)7 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)6 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)4 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)4 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)4 FilterInvoker (com.alipay.sofa.rpc.filter.FilterInvoker)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)3