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();
}
}
Aggregations