Skip to content

Commit

Permalink
🧪 fix etcd test in pr error
Browse files Browse the repository at this point in the history
  • Loading branch information
songxiaosheng committed Feb 3, 2024
1 parent 070f6e0 commit b20d5aa
Showing 1 changed file with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

package org.apache.dubbo.configcenter.support.etcd;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
Expand All @@ -29,6 +30,9 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
Expand All @@ -48,15 +52,43 @@ public class EtcdDynamicConfigurationTest {

private static EtcdDynamicConfiguration config;

public EtcdCluster etcdCluster = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false);

//public EtcdCluster etcdCluster= new Etcd.Builder().withClusterName(getClass().getSimpleName()).withNodes(3).withSsl(false).build();
private static final Logger logger = LoggerFactory.getLogger(EtcdDynamicConfigurationTest.class);
//public EtcdCluster etcdCluster= new Etcd.Builder().withClusterName(getClass().getSimpleName()).withNodes(3).withSsl(false).build();

private static Client client;

public EtcdCluster etcdCluster;

//这里会涉及到docker拉取镜像很慢
@Before
public void setUp() {
try {
etcdCluster = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false);

etcdCluster.start();

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

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

String ipAddress = clientEndPoints.get(0).getHost() + ":" + clientEndPoints.get(0).getPort(); //"127.0.0.1:2379";

String urlForDubbo = "etcd3://" + ipAddress + "/org.apache.dubbo.etcd.testService";

// timeout in 15 seconds.
URL url = URL.valueOf(urlForDubbo).addParameter(SESSION_TIMEOUT_KEY, 15000);
config = new EtcdDynamicConfiguration(url);
} catch (Exception e) {
logger.error("Failed to start etcd cluster", e);
}
}

@Test
public void testGetConfig() {
public void testGetConfig() {
if (config == null) {
logger.error("Failed to start etcd cluster ,config is null");
return;
}
put("/dubbo/config/dubbo/org.apache.dubbo.etcd.testService/configurators", "hello");
put("/dubbo/config/test/dubbo.properties", "aaa=bbb");
Assert.assertEquals("hello", config.getConfig("org.apache.dubbo.etcd.testService/configurators", DynamicConfiguration.DEFAULT_GROUP));
Expand All @@ -66,7 +98,10 @@ public void testGetConfig() {

@Test
public void testAddListener1() throws Exception {

if (config == null) {
logger.error("Failed to start etcd cluster ,config is null");
return;
}
CountDownLatch latch = new CountDownLatch(4);
TestListener listener1 = new TestListener(latch);
TestListener listener2 = new TestListener(latch);
Expand Down Expand Up @@ -99,7 +134,6 @@ public void testAddListener1() throws Exception {
}



private class TestListener implements ConfigurationListener {
private CountDownLatch latch;
private String value;
Expand Down Expand Up @@ -134,29 +168,15 @@ private void put(String key, String value) {
}
}

//这里会涉及到docker拉取镜像很慢
@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(); //"127.0.0.1:2379";

String urlForDubbo = "etcd3://" + ipAddress + "/org.apache.dubbo.etcd.testService";

// timeout in 15 seconds.
URL url = URL.valueOf(urlForDubbo).addParameter(SESSION_TIMEOUT_KEY, 15000);
config = new EtcdDynamicConfiguration(url);
}

@After
public void tearDown() {
etcdCluster.close();
client.close();
if (etcdCluster != null) {
etcdCluster.close();
}
if (client != null) {
client.close();
}
}

}

0 comments on commit b20d5aa

Please sign in to comment.