Skip to content

Commit

Permalink
etcd3 integration test api (apache#3887)
Browse files Browse the repository at this point in the history
* etcd config center integrate test
* clean code
  • Loading branch information
moriadry authored and vio-lin committed Apr 29, 2019
1 parent a36e17d commit 225ef4c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
10 changes: 10 additions & 0 deletions dubbo-configcenter/dubbo-configcenter-etcd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
<artifactId>dubbo-configcenter-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-etcd3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.launcher.EtcdCluster;
import io.etcd.jetcd.launcher.EtcdClusterFactory;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand All @@ -38,23 +41,23 @@

/**
* Unit test for etcd config center support
* TODO Integrate with https://github.com/etcd-io/jetcd#launcher or using mock data.
* Integrate with https://github.com/etcd-io/jetcd#launcher
*/
@Disabled
public class EtcdDynamicConfigurationTest {

private static final String ENDPOINT = "http://127.0.0.1:2379";

private static EtcdDynamicConfiguration config;

private static Client etcdClient;
public EtcdCluster etcdCluster = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false, false);

private static Client client;

@Test
public void testGetConfig() {

put("/dubbo/config/org.apache.dubbo.etcd.testService/configurators", "hello");
put("/dubbo/config/test/dubbo.properties", "aaa=bbb");
Assertions.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService.configurators"));
Assertions.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
Assert.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService.configurators"));
Assert.assertEquals("aaa=bbb", config.getConfig("dubbo.properties", "test"));
}

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

Thread.sleep(1000);

Assertions.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assertions.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators"));
Assertions.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators"));
Assertions.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters"));
Assertions.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters"));
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(1, listener1.getCount("/dubbo/config/AService/configurators"));
Assert.assertEquals(1, listener2.getCount("/dubbo/config/AService/configurators"));
Assert.assertEquals(1, listener3.getCount("/dubbo/config/testapp/tagrouters"));
Assert.assertEquals(1, listener4.getCount("/dubbo/config/testapp/tagrouters"));

Assertions.assertEquals("new value1", listener1.getValue());
Assertions.assertEquals("new value1", listener2.getValue());
Assertions.assertEquals("new value2", listener3.getValue());
Assertions.assertEquals("new value2", listener4.getValue());
Assert.assertEquals("new value1", listener1.getValue());
Assert.assertEquals("new value1", listener2.getValue());
Assert.assertEquals("new value2", listener3.getValue());
Assert.assertEquals("new value2", listener4.getValue());
}

private class TestListener implements ConfigurationListener {
Expand Down Expand Up @@ -115,27 +118,36 @@ public String getValue() {
}
}

static void put(String key, String value) {
private void put(String key, String value) {
try {
etcdClient.getKVClient()
.put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8))
.get();

client.getKVClient().put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8)).get();
} catch (Exception e) {
System.out.println("Error put value to etcd.");
}
}

@BeforeAll
static void setUp() {
etcdClient = Client.builder().endpoints(ENDPOINT).build();
@Before
public void setUp() {

etcdCluster.start();

client = Client.builder().endpoints(etcdCluster.getClientEndpoints()).build();

List<URI> clientEndPoints = etcdCluster.getClientEndpoints();

String ipAddress = clientEndPoints.get(0).getHost() + ":" + clientEndPoints.get(0).getPort();
String urlForDubbo = "etcd3://" + ipAddress + "/org.apache.dubbo.etcd.testService";

// timeout in 15 seconds.
URL url = URL.valueOf("etcd3://127.0.0.1:2379/org.apache.dubbo.etcd.testService")
URL url = URL.valueOf(urlForDubbo)
.addParameter(Constants.SESSION_TIMEOUT_KEY, 15000);
config = new EtcdDynamicConfiguration(url);
}

@AfterAll
static void tearDown() {
etcdClient.close();
@After
public void tearDown() {
etcdCluster.close();
}

}
13 changes: 13 additions & 0 deletions dubbo-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@

<jaxb_version>2.2.7</jaxb_version>
<activation_version>1.2.0</activation_version>
<test_container_version>1.11.2</test_container_version>
<etcd_launcher_version>0.3.0</etcd_launcher_version>
<hessian_lite_version>3.2.5</hessian_lite_version>
<swagger_version>1.5.19</swagger_version>
<spring_test_version>4.3.16.RELEASE</spring_test_version>
Expand Down Expand Up @@ -564,6 +566,17 @@
<artifactId>portlet-api</artifactId>
<version>${portlet_version}</version>
</dependency>
<!-- for dubbo-configcenter-etcd -->
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-launcher</artifactId>
<version>${etcd_launcher_version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${test_container_version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 225ef4c

Please sign in to comment.