Skip to content

Commit 2d0c07d

Browse files
moriadryralf0131
authored andcommitted
etcd3 integration test api (apache#3887)
* etcd config center integrate test * clean code
1 parent 7d83fde commit 2d0c07d

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

dubbo-configcenter/dubbo-configcenter-etcd/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
<artifactId>dubbo-configcenter-api</artifactId>
3838
<version>${project.parent.version}</version>
3939
</dependency>
40+
<dependency>
41+
<groupId>io.etcd</groupId>
42+
<artifactId>jetcd-launcher</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.testcontainers</groupId>
47+
<artifactId>testcontainers</artifactId>
48+
<scope>test</scope>
49+
</dependency>
4050
<dependency>
4151
<groupId>org.apache.dubbo</groupId>
4252
<artifactId>dubbo-remoting-etcd3</artifactId>

dubbo-configcenter/dubbo-configcenter-etcd/src/test/java/org/apache/dubbo/configcenter/support/etcd/EtcdDynamicConfigurationTest.java

+44-32
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919

2020
import io.etcd.jetcd.ByteSequence;
2121
import io.etcd.jetcd.Client;
22+
import io.etcd.jetcd.launcher.EtcdCluster;
23+
import io.etcd.jetcd.launcher.EtcdClusterFactory;
2224
import org.apache.dubbo.common.Constants;
2325
import org.apache.dubbo.common.URL;
2426
import org.apache.dubbo.configcenter.ConfigChangeEvent;
2527
import org.apache.dubbo.configcenter.ConfigurationListener;
26-
import org.junit.jupiter.api.AfterAll;
27-
import org.junit.jupiter.api.Assertions;
28-
import org.junit.jupiter.api.BeforeAll;
29-
import org.junit.jupiter.api.Disabled;
30-
import org.junit.jupiter.api.Test;
28+
import org.junit.After;
29+
import org.junit.Assert;
30+
import org.junit.Before;
31+
import org.junit.Test;
3132

33+
import java.net.URI;
3234
import java.util.HashMap;
35+
import java.util.List;
3336
import java.util.Map;
3437
import java.util.concurrent.CountDownLatch;
3538
import java.util.concurrent.TimeUnit;
@@ -38,23 +41,23 @@
3841

3942
/**
4043
* Unit test for etcd config center support
41-
* TODO Integrate with https://github.com/etcd-io/jetcd#launcher or using mock data.
44+
* Integrate with https://github.com/etcd-io/jetcd#launcher
4245
*/
43-
@Disabled
4446
public class EtcdDynamicConfigurationTest {
4547

46-
private static final String ENDPOINT = "http://127.0.0.1:2379";
47-
4848
private static EtcdDynamicConfiguration config;
4949

50-
private static Client etcdClient;
50+
public EtcdCluster etcdCluster = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false, false);
51+
52+
private static Client client;
5153

5254
@Test
5355
public void testGetConfig() {
56+
5457
put("/dubbo/config/org.apache.dubbo.etcd.testService/configurators", "hello");
5558
put("/dubbo/config/test/dubbo.properties", "aaa=bbb");
56-
Assertions.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService.configurators"));
57-
Assertions.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
59+
Assert.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService.configurators"));
60+
Assert.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
5861
}
5962

6063
@Test
@@ -77,16 +80,16 @@ public void testAddListener() throws Exception {
7780

7881
Thread.sleep(1000);
7982

80-
Assertions.assertTrue(latch.await(5, TimeUnit.SECONDS));
81-
Assertions.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators"));
82-
Assertions.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators"));
83-
Assertions.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters"));
84-
Assertions.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters"));
83+
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
84+
Assert.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators"));
85+
Assert.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators"));
86+
Assert.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters"));
87+
Assert.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters"));
8588

86-
Assertions.assertEquals("new value1", listener1.getValue());
87-
Assertions.assertEquals("new value1", listener2.getValue());
88-
Assertions.assertEquals("new value2", listener3.getValue());
89-
Assertions.assertEquals("new value2", listener4.getValue());
89+
Assert.assertEquals("new value1", listener1.getValue());
90+
Assert.assertEquals("new value1", listener2.getValue());
91+
Assert.assertEquals("new value2", listener3.getValue());
92+
Assert.assertEquals("new value2", listener4.getValue());
9093
}
9194

9295
private class TestListener implements ConfigurationListener {
@@ -115,27 +118,36 @@ public String getValue() {
115118
}
116119
}
117120

118-
static void put(String key, String value) {
121+
private void put(String key, String value) {
119122
try {
120-
etcdClient.getKVClient()
121-
.put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8))
122-
.get();
123+
124+
client.getKVClient().put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8)).get();
123125
} catch (Exception e) {
124126
System.out.println("Error put value to etcd.");
125127
}
126128
}
127129

128-
@BeforeAll
129-
static void setUp() {
130-
etcdClient = Client.builder().endpoints(ENDPOINT).build();
130+
@Before
131+
public void setUp() {
132+
133+
etcdCluster.start();
134+
135+
client = Client.builder().endpoints(etcdCluster.getClientEndpoints()).build();
136+
137+
List<URI> clientEndPoints = etcdCluster.getClientEndpoints();
138+
139+
String ipAddress = clientEndPoints.get(0).getHost() + ":" + clientEndPoints.get(0).getPort();
140+
String urlForDubbo = "etcd3://" + ipAddress + "/org.apache.dubbo.etcd.testService";
141+
131142
// timeout in 15 seconds.
132-
URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.etcd.testService")
143+
URL url = URL.valueOf(urlForDubbo)
133144
.addParameter(Constants.SESSION_TIMEOUT_KEY, 15000);
134145
config = new EtcdDynamicConfiguration(url);
135146
}
136147

137-
@AfterAll
138-
static void tearDown() {
139-
etcdClient.close();
148+
@After
149+
public void tearDown() {
150+
etcdCluster.close();
140151
}
152+
141153
}

dubbo-dependencies-bom/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137

138138
<jaxb_version>2.2.7</jaxb_version>
139139
<activation_version>1.2.0</activation_version>
140+
<test_container_version>1.11.2</test_container_version>
141+
<etcd_launcher_version>0.3.0</etcd_launcher_version>
140142
<hessian_lite_version>3.2.5</hessian_lite_version>
141143
<swagger_version>1.5.19</swagger_version>
142144
<spring_test_version>4.3.16.RELEASE</spring_test_version>
@@ -552,6 +554,17 @@
552554
<artifactId>portlet-api</artifactId>
553555
<version>${portlet_version}</version>
554556
</dependency>
557+
<!-- for dubbo-configcenter-etcd -->
558+
<dependency>
559+
<groupId>io.etcd</groupId>
560+
<artifactId>jetcd-launcher</artifactId>
561+
<version>${etcd_launcher_version}</version>
562+
</dependency>
563+
<dependency>
564+
<groupId>org.testcontainers</groupId>
565+
<artifactId>testcontainers</artifactId>
566+
<version>${test_container_version}</version>
567+
</dependency>
555568
</dependencies>
556569
</dependencyManagement>
557570

0 commit comments

Comments
 (0)