Search in sources :

Example 91 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class UniqueIdInvokerTest method test.

@Test
public void test() {
    try {
        // Prepare data
        UniqueIdInvoker uniqueIdInvoker = new UniqueIdInvoker();
        ProviderConfig uniqueOne = new ProviderConfig();
        uniqueOne.setInterfaceId(INTERFACE_ID);
        uniqueOne.setUniqueId(UNIQUE_ID_ONE);
        uniqueIdInvoker.registerInvoker(uniqueOne, new UniqueIdTestInvoker(UNIQUE_ID_ONE));
        ProviderConfig uniqueTwo = new ProviderConfig();
        uniqueTwo.setInterfaceId(INTERFACE_ID);
        uniqueTwo.setUniqueId(UNIQUE_ID_TWO);
        uniqueIdInvoker.registerInvoker(uniqueTwo, new UniqueIdTestInvoker(UNIQUE_ID_TWO));
        // Case 1: Invoke invoker which unique id is one
        SofaRequest sofaRequest = new SofaRequest();
        sofaRequest.setInterfaceName(INTERFACE_ID);
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_ONE);
        SofaResponse sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        String appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_ONE);
        // Case 2: Invoke invoker which unique id is two
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_TWO);
        // Case 3: There was only one invoker in UniqueIdInvoker and can not find invoker without unique id,
        // invoke the last invoker.
        uniqueIdInvoker.unRegisterInvoker(uniqueTwo);
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
        sofaResponse = uniqueIdInvoker.invoke(sofaRequest);
        appResponse = (String) sofaResponse.getAppResponse();
        Assert.assertEquals(appResponse, UNIQUE_ID_ONE);
        // Case 3: There was only one invoker in UniqueIdInvoker and can not find invoker with unique id,
        // invoke fail.
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        boolean throwException = false;
        try {
            uniqueIdInvoker.invoke(sofaRequest);
        } catch (SofaRpcException throwable) {
            // Except exception
            throwException = true;
        }
        Assert.assertTrue(throwException);
        // Case 4: There was more than one invoker in UniqueIdInvoker, can not find invoker with unique id two,
        // invoke fail.
        ProviderConfig uniqueThree = new ProviderConfig();
        uniqueThree.setInterfaceId(INTERFACE_ID);
        uniqueThree.setUniqueId(UNIQUE_ID_THREE);
        uniqueIdInvoker.registerInvoker(uniqueThree, new UniqueIdTestInvoker(UNIQUE_ID_THREE));
        RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, UNIQUE_ID_TWO);
        throwException = false;
        try {
            uniqueIdInvoker.invoke(sofaRequest);
        } catch (SofaRpcException throwable) {
            // Except exception
            throwException = true;
        }
        Assert.assertTrue(throwException);
    } finally {
        RpcInvokeContext.removeContext();
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7