forked from sofastack/sofa-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reconnect of bolt/rest/h2c . (sofastack#180)
- Loading branch information
Showing
9 changed files
with
386 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
test/test-common/src/main/java/com/alipay/sofa/rpc/test/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alipay.sofa.rpc.test; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">GengZhang</a> | ||
*/ | ||
public class TestUtils { | ||
|
||
public static <T> T delayGet(Callable<T> callable, T expect, int period, int times) { | ||
T result = null; | ||
int i = 0; | ||
while (i++ < times) { | ||
try { | ||
Thread.sleep(period); | ||
result = callable.call(); | ||
if (result != null && result.equals(expect)) { | ||
break; | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
test/test-integration/src/test/java/com/alipay/sofa/rpc/client/bolt/BoltDirectUrlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alipay.sofa.rpc.client.bolt; | ||
|
||
import com.alipay.sofa.rpc.common.RpcConstants; | ||
import com.alipay.sofa.rpc.common.utils.CommonUtils; | ||
import com.alipay.sofa.rpc.config.ApplicationConfig; | ||
import com.alipay.sofa.rpc.config.ConsumerConfig; | ||
import com.alipay.sofa.rpc.config.ProviderConfig; | ||
import com.alipay.sofa.rpc.config.ServerConfig; | ||
import com.alipay.sofa.rpc.core.exception.SofaRpcException; | ||
import com.alipay.sofa.rpc.test.ActivelyDestroyTest; | ||
import com.alipay.sofa.rpc.test.HelloService; | ||
import com.alipay.sofa.rpc.test.HelloServiceImpl; | ||
import com.alipay.sofa.rpc.test.TestUtils; | ||
import org.junit.Assert; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">GengZhang</a> | ||
*/ | ||
public class BoltDirectUrlTest extends ActivelyDestroyTest { | ||
|
||
// @Test | ||
// FIXME 目前bolt的IO线程关闭时未释放,暂不支持本测试用例 | ||
public void testAll() { | ||
// 只有2个线程 执行 | ||
ServerConfig serverConfig = new ServerConfig() | ||
.setPort(12300) | ||
.setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT) | ||
.setDaemon(true); | ||
|
||
// 发布一个服务,每个请求要执行1秒 | ||
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>() | ||
.setInterfaceId(HelloService.class.getName()) | ||
.setRef(new HelloServiceImpl()) | ||
.setBootstrap("bolt") | ||
.setApplication(new ApplicationConfig().setAppName("serverApp")) | ||
.setServer(serverConfig) | ||
.setRegister(false); | ||
providerConfig.export(); | ||
|
||
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>() | ||
.setInterfaceId(HelloService.class.getName()) | ||
.setDirectUrl("bolt://127.0.0.1:12300") | ||
.setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT) | ||
.setBootstrap("bolt") | ||
.setApplication(new ApplicationConfig().setAppName("clientApp")) | ||
.setReconnectPeriod(1000); | ||
|
||
HelloService helloService = consumerConfig.refer(); | ||
|
||
Assert.assertNotNull(helloService.sayHello("xx", 22)); | ||
|
||
serverConfig.getServer().stop(); | ||
|
||
// 关闭后再调用一个抛异常 | ||
try { | ||
helloService.sayHello("xx", 22); | ||
} catch (Exception e) { | ||
// 应该抛出异常 | ||
Assert.assertTrue(e instanceof SofaRpcException); | ||
} | ||
|
||
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() { | ||
@Override | ||
public Boolean call() throws Exception { | ||
return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap() | ||
.getCluster().getConnectionHolder().getAvailableConnections()); | ||
} | ||
}, true, 50, 40)); | ||
|
||
serverConfig.getServer().start(); | ||
// 等待客户端重连服务端 | ||
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() { | ||
@Override | ||
public Boolean call() throws Exception { | ||
return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap() | ||
.getCluster().getConnectionHolder().getAvailableConnections()); | ||
} | ||
}, true, 50, 60)); | ||
|
||
Assert.assertNotNull(helloService.sayHello("xx", 22)); | ||
} | ||
} |
Oops, something went wrong.