use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class ReuseBoltClientConnectionManagerTest method buildConfig.
private ClientTransportConfig buildConfig(int port) {
ClientTransportConfig config = new ClientTransportConfig();
ProviderInfo providerInfo2 = new ProviderInfo().setHost("127.0.0.1").setPort(port);
config.setProviderInfo(providerInfo2).setContainer("bolt");
return config;
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class ReuseBoltClientConnectionManagerTest method testAll.
@Test
public void testAll() throws Exception {
ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
Connection connection = manager.getConnection(null, null, null);
Assert.assertNull(connection);
connection = manager.getConnection(rpcClient, null, null);
Assert.assertNull(connection);
ClientTransportConfig wrongConfig = buildConfig(12224);
connection = manager.getConnection(rpcClient, wrongConfig, null);
Assert.assertNull(connection);
// 连不上的端口
Connection result = manager.getConnection(rpcClient, wrongConfig, buildUrl(wrongConfig));
Assert.assertNull(result);
// ok
final ClientTransportConfig config = buildConfig(12222);
connection = manager.getConnection(rpcClient, config, buildUrl(config));
Assert.assertNotNull(connection);
Assert.assertTrue(manager.urlConnectionMap.size() == 1);
Assert.assertTrue(manager.connectionRefCounter.size() == 1);
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
// 同一个config去get,计数器不增加
Connection connection1 = manager.getConnection(rpcClient, config, buildUrl(config));
Assert.assertNotNull(connection1);
Assert.assertTrue(manager.urlConnectionMap.size() == 1);
Assert.assertTrue(manager.connectionRefCounter.size() == 1);
Assert.assertTrue(connection == connection1);
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
Assert.assertTrue(manager.connectionRefCounter.get(connection1).get() == 1);
// 相同地址的config去get,计数器加一
final ClientTransportConfig config2 = buildConfig(12222);
Connection connection2 = manager.getConnection(rpcClient, config2, buildUrl(config2));
Assert.assertNotNull(connection2);
Assert.assertTrue(manager.urlConnectionMap.size() == 2);
Assert.assertTrue(manager.connectionRefCounter.size() == 1);
Assert.assertTrue(connection1 == connection2);
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 2);
Assert.assertTrue(manager.connectionRefCounter.get(connection2).get() == 2);
// 不同地址的config去get,地址和计数器加1
ClientTransportConfig config3 = buildConfig(12223);
Connection connection3 = manager.getConnection(rpcClient, config3, buildUrl(config3));
Assert.assertNotNull(connection3);
Assert.assertFalse(connection == connection3);
Assert.assertTrue(manager.urlConnectionMap.size() == 3);
Assert.assertTrue(manager.connectionRefCounter.size() == 2);
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 2);
Assert.assertTrue(manager.connectionRefCounter.get(connection3).get() == 1);
// 非法关闭
manager.closeConnection(null, null, null);
Assert.assertTrue(manager.urlConnectionMap.size() == 3);
manager.closeConnection(rpcClient, null, null);
Assert.assertTrue(manager.urlConnectionMap.size() == 3);
manager.closeConnection(rpcClient, config, null);
Assert.assertTrue(manager.urlConnectionMap.size() == 3);
// 正常关闭1
manager.closeConnection(rpcClient, config, buildUrl(config));
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
// 重复关闭1
manager.closeConnection(rpcClient, config, buildUrl(config));
Assert.assertTrue(manager.urlConnectionMap.size() == 2);
Assert.assertTrue(manager.connectionRefCounter.size() == 2);
Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
// 正常关闭2
manager.closeConnection(rpcClient, config2, buildUrl(config2));
Assert.assertTrue(manager.urlConnectionMap.size() == 1);
Assert.assertTrue(manager.connectionRefCounter.size() == 1);
// 正常关闭3
manager.closeConnection(rpcClient, config3, buildUrl(config3));
Assert.assertTrue(manager.urlConnectionMap.size() == 0);
Assert.assertTrue(manager.connectionRefCounter.size() == 0);
// 检查泄漏
manager.checkLeak();
Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class ReuseBoltClientConnectionManagerTest method testConcurrentCreate.
@Test
public void testConcurrentCreate() throws Exception {
final ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
final ClientTransportConfig config = buildConfig(12222);
// 并发创建
final CountDownLatch latch = new CountDownLatch(5);
List<Thread> threads = new ArrayList<Thread>(5);
final Url url = buildUrl(config);
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Connection innerConnection = manager.getConnection(rpcClient, config, url);
System.out.println("url=" + url + ",connection=" + innerConnection);
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
}, "thread" + i);
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
latch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, manager.urlConnectionMap.size());
Assert.assertEquals(1, manager.connectionRefCounter.size());
Connection connection = manager.getConnection(rpcClient, config, url);
Assert.assertNotNull(connection);
final AtomicInteger atomicInteger = manager.connectionRefCounter.get(connection);
Assert.assertEquals(1, atomicInteger.get());
// 检查泄漏
manager.checkLeak();
Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class BoltClientTransportTest method testConvertToRpcException.
@Test
public void testConvertToRpcException() {
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport transport = new BoltClientTransport(config1);
Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class AllConnectConnectionHolder method initClientRunnable.
/**
* 线程池建立长连接
*/
protected void initClientRunnable(ThreadPoolExecutor initPool, final CountDownLatch latch, final ProviderInfo providerInfo) {
final ClientTransportConfig config = providerToClientConfig(providerInfo);
initPool.execute(new Runnable() {
@Override
public void run() {
ClientTransport transport = ClientTransportFactory.getClientTransport(config);
if (consumerConfig.isLazy()) {
uninitializedConnections.put(providerInfo, transport);
latch.countDown();
} else {
try {
initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
} finally {
// 连上或者抛异常
latch.countDown();
}
}
}
});
}
Aggregations