Skip to content

Commit 3d98b80

Browse files
authored
Use embedded zookeeper for integration test. (sofastack#137)
1.use curator-test to mock zk 2.register a dubbo shutdown hook
1 parent 8cc5c1c commit 3d98b80

File tree

11 files changed

+168
-24
lines changed

11 files changed

+168
-24
lines changed

.travis.yml

-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ before_install:
1818
&& export M2_HOME=$PWD/apache-maven-3.2.5
1919
&& export PATH=$M2_HOME/bin:$PATH
2020
&& mvn -version
21-
- echo "Install Zookeeper 3.4.11"
22-
&& wget http://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
23-
&& tar -zxf zookeeper-3.4.11.tar.gz
24-
&& mv zookeeper-3.4.11/conf/zoo_sample.cfg zookeeper-3.4.11/conf/zoo.cfg
25-
&& /bin/bash zookeeper-3.4.11/bin/zkServer.sh start
2621

2722
install:
2823
- mvn clean install -Pci-install -B -U -e

bom/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@
258258
<artifactId>curator-recipes</artifactId>
259259
<version>${curator.version}</version>
260260
</dependency>
261+
<dependency>
262+
<groupId>org.apache.curator</groupId>
263+
<artifactId>curator-test</artifactId>
264+
<version>${curator.version}</version>
265+
<scope>test</scope>
266+
</dependency>
261267

262268
<!--dubbo-->
263269
<dependency>

extension-impl/bootstrap-dubbo/src/main/java/com/alipay/sofa/rpc/bootstrap/dubbo/DubboSingleton.java

+16
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.alipay.sofa.rpc.bootstrap.dubbo;
1818

1919
import com.alibaba.dubbo.config.ProtocolConfig;
20+
import com.alipay.sofa.rpc.base.Destroyable;
2021
import com.alipay.sofa.rpc.config.RegistryConfig;
2122
import com.alipay.sofa.rpc.config.ServerConfig;
23+
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
2224

2325
import java.util.Map;
2426
import java.util.concurrent.ConcurrentHashMap;
@@ -30,6 +32,20 @@
3032
*/
3133
public class DubboSingleton {
3234

35+
static {
36+
RpcRuntimeContext.registryDestroyHook(new Destroyable.DestroyHook() {
37+
@Override
38+
public void preDestroy() {
39+
destroyAll();
40+
}
41+
42+
@Override
43+
public void postDestroy() {
44+
45+
}
46+
});
47+
}
48+
3349
/**
3450
* sofa.SeverConfig --> dubbo.ProtocolConfig
3551
*/

extension-impl/registry-zk/pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>junit</artifactId>
3535
<scope>test</scope>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.apache.curator</groupId>
39+
<artifactId>curator-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>
@@ -86,7 +91,7 @@
8691
<groupId>org.apache.maven.plugins</groupId>
8792
<artifactId>maven-surefire-plugin</artifactId>
8893
<configuration>
89-
<skipTests>${dependency.test.skip}</skipTests>
94+
<skipTests>${skipTests}</skipTests>
9095
<includes>
9196
<!-- 这里需要根据自己的需要指定要跑的单元测试 -->
9297
<include>**/*Test.java</include>

extension-impl/registry-zk/src/test/java/com/alipay/sofa/rpc/registry/zk/ZookeeperRegistryTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.alipay.sofa.rpc.listener.ConfigListener;
2929
import com.alipay.sofa.rpc.listener.ProviderInfoListener;
3030
import com.alipay.sofa.rpc.registry.RegistryFactory;
31+
import com.alipay.sofa.rpc.registry.zk.base.BaseZkTest;
3132
import org.junit.AfterClass;
3233
import org.junit.Assert;
3334
import org.junit.BeforeClass;
@@ -44,7 +45,7 @@
4445
/**
4546
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
4647
*/
47-
public class ZookeeperRegistryTest {
48+
public class ZookeeperRegistryTest extends BaseZkTest {
4849

4950
private static RegistryConfig registryConfig;
5051

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.registry.zk.base;
18+
19+
import com.alipay.sofa.rpc.context.RpcInternalContext;
20+
import com.alipay.sofa.rpc.context.RpcInvokeContext;
21+
import com.alipay.sofa.rpc.context.RpcRunningState;
22+
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
23+
import org.apache.curator.test.TestingServer;
24+
import org.junit.AfterClass;
25+
import org.junit.BeforeClass;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* @author bystander
31+
* if you use zk to be registry ,your test case must be extends this class
32+
* @version $Id: BaseZkTest.java, v 0.1 2018年05月22日 7:55 PM bystander Exp $
33+
*/
34+
public abstract class BaseZkTest {
35+
protected static TestingServer server = null;
36+
37+
@BeforeClass
38+
public static void adBeforeClass() {
39+
RpcRunningState.setUnitTestMode(true);
40+
41+
try {
42+
server = new TestingServer(2181, true);
43+
server.start();
44+
} catch (Exception e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
49+
@AfterClass
50+
public static void adAfterClass() {
51+
RpcRuntimeContext.destroy();
52+
RpcInternalContext.removeContext();
53+
RpcInvokeContext.removeContext();
54+
55+
if (server != null) {
56+
try {
57+
server.stop();
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
}
63+
}

pom.xml

-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
<module.deploy.skip>true</module.deploy.skip>
5151
<maven.javadoc.skip>true</maven.javadoc.skip>
5252
<module.jacoco.skip>true</module.jacoco.skip>
53-
<dependency.test.skip>true</dependency.test.skip>
5453
<maven.compiler.source>1.6</maven.compiler.source>
5554
<maven.compiler.target>1.6</maven.compiler.target>
5655
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
@@ -273,12 +272,6 @@
273272
<module.jacoco.skip>false</module.jacoco.skip>
274273
</properties>
275274
</profile>
276-
<profile>
277-
<id>it-test</id>
278-
<properties>
279-
<dependency.test.skip>false</dependency.test.skip>
280-
</properties>
281-
</profile>
282275
<profile>
283276
<id>release</id>
284277
<properties>

test/test-integration-3rd/pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<scope>test</scope>
5959
</dependency>
6060

61+
<dependency>
62+
<groupId>org.apache.curator</groupId>
63+
<artifactId>curator-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
6167
</dependencies>
6268
<build>
6369
<testSourceDirectory>src/test/java</testSourceDirectory>
@@ -99,7 +105,7 @@
99105
<groupId>org.apache.maven.plugins</groupId>
100106
<artifactId>maven-surefire-plugin</artifactId>
101107
<configuration>
102-
<skipTests>${dependency.test.skip}</skipTests>
108+
<skipTests>${skipTests}</skipTests>
103109
<includes>
104110
<!-- 这里需要根据自己的需要指定要跑的单元测试 -->
105111
<include>**/*Test.java</include>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.rpc.base;
18+
19+
import com.alipay.sofa.rpc.context.RpcInternalContext;
20+
import com.alipay.sofa.rpc.context.RpcInvokeContext;
21+
import com.alipay.sofa.rpc.context.RpcRunningState;
22+
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
23+
import org.apache.curator.test.TestingServer;
24+
import org.junit.AfterClass;
25+
import org.junit.BeforeClass;
26+
27+
import java.io.IOException;
28+
29+
/**
30+
* @author bystander
31+
* @version $Id: BaseZkTest.java, v 0.1 2018年05月22日 7:55 PM bystander Exp $
32+
*/
33+
public abstract class BaseZkTest {
34+
protected static TestingServer server = null;
35+
36+
@BeforeClass
37+
public static void adBeforeClass() {
38+
RpcRunningState.setUnitTestMode(true);
39+
40+
try {
41+
server = new TestingServer(2181, true);
42+
server.start();
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
48+
@AfterClass
49+
public static void adAfterClass() {
50+
RpcRuntimeContext.destroy();
51+
RpcInternalContext.removeContext();
52+
RpcInvokeContext.removeContext();
53+
54+
if (server != null) {
55+
try {
56+
server.stop();
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
}

test/test-integration-3rd/src/test/java/com/alipay/sofa/rpc/bootstrap/dubbo/DubooServerTest.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616
*/
1717
package com.alipay.sofa.rpc.bootstrap.dubbo;
1818

19+
import com.alipay.sofa.rpc.base.BaseZkTest;
1920
import com.alipay.sofa.rpc.common.SystemInfo;
20-
import com.alipay.sofa.rpc.config.ApplicationConfig;
21-
import com.alipay.sofa.rpc.config.ConsumerConfig;
22-
import com.alipay.sofa.rpc.config.ProviderConfig;
23-
import com.alipay.sofa.rpc.config.RegistryConfig;
24-
import com.alipay.sofa.rpc.config.ServerConfig;
25-
import com.alipay.sofa.rpc.test.ActivelyDestroyTest;
21+
import com.alipay.sofa.rpc.config.*;
2622
import com.alipay.sofa.rpc.test.HelloService;
2723
import com.alipay.sofa.rpc.test.HelloServiceImpl;
2824
import org.junit.Assert;
@@ -35,14 +31,14 @@
3531
* @author bystander
3632
* @version $Id: DubooServerTest.java, v 0.1 2017年10月30日 下午9:23 bystander Exp $
3733
*/
38-
public class DubooServerTest extends ActivelyDestroyTest {
34+
public class DubooServerTest extends BaseZkTest {
3935

4036
@Test
4137
//同步调用,走配置中心
4238
public void testRegistrySync() {
4339
// 只有1个线程 执行
4440
ServerConfig serverConfig = new ServerConfig()
45-
.setStopTimeout(60000)
41+
.setStopTimeout(10)
4642
.setPort(20880)
4743
.setProtocol("dubbo")
4844
.setQueues(100).setCoreThreads(1).setMaxThreads(2).setHost(SystemInfo.getLocalHost());

test/test-integration-3rd/src/test/java/com/alipay/sofa/rpc/registry/zk/WarmUpTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alipay.sofa.rpc.registry.zk;
1818

19+
import com.alipay.sofa.rpc.base.BaseZkTest;
1920
import com.alipay.sofa.rpc.client.ProviderInfoAttrs;
2021
import com.alipay.sofa.rpc.common.RpcConstants;
2122
import com.alipay.sofa.rpc.config.ConsumerConfig;
@@ -30,7 +31,7 @@
3031
*
3132
* @author <a href="mailto:lw111072@antfin.com">LiWei.Liangen</a>
3233
*/
33-
public class WarmUpTest extends ActivelyDestroyTest {
34+
public class WarmUpTest extends BaseZkTest {
3435

3536
@Test
3637
public void testWarmUp() throws InterruptedException {

0 commit comments

Comments
 (0)