19
19
20
20
import io .etcd .jetcd .ByteSequence ;
21
21
import io .etcd .jetcd .Client ;
22
+ import io .etcd .jetcd .launcher .EtcdCluster ;
23
+ import io .etcd .jetcd .launcher .EtcdClusterFactory ;
22
24
import org .apache .dubbo .common .Constants ;
23
25
import org .apache .dubbo .common .URL ;
24
26
import org .apache .dubbo .configcenter .ConfigChangeEvent ;
25
27
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 ;
31
32
33
+ import java .net .URI ;
32
34
import java .util .HashMap ;
35
+ import java .util .List ;
33
36
import java .util .Map ;
34
37
import java .util .concurrent .CountDownLatch ;
35
38
import java .util .concurrent .TimeUnit ;
38
41
39
42
/**
40
43
* 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
42
45
*/
43
- @ Disabled
44
46
public class EtcdDynamicConfigurationTest {
45
47
46
- private static final String ENDPOINT = "http://127.0.0.1:2379" ;
47
-
48
48
private static EtcdDynamicConfiguration config ;
49
49
50
- private static Client etcdClient ;
50
+ public EtcdCluster etcdCluster = EtcdClusterFactory .buildCluster (getClass ().getSimpleName (), 3 , false , false );
51
+
52
+ private static Client client ;
51
53
52
54
@ Test
53
55
public void testGetConfig () {
56
+
54
57
put ("/dubbo/config/org.apache.dubbo.etcd.testService/configurators" , "hello" );
55
58
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" ));
58
61
}
59
62
60
63
@ Test
@@ -77,16 +80,16 @@ public void testAddListener() throws Exception {
77
80
78
81
Thread .sleep (1000 );
79
82
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" ));
85
88
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 ());
90
93
}
91
94
92
95
private class TestListener implements ConfigurationListener {
@@ -115,27 +118,36 @@ public String getValue() {
115
118
}
116
119
}
117
120
118
- static void put (String key , String value ) {
121
+ private void put (String key , String value ) {
119
122
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 ();
123
125
} catch (Exception e ) {
124
126
System .out .println ("Error put value to etcd." );
125
127
}
126
128
}
127
129
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
+
131
142
// 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 )
133
144
.addParameter (Constants .SESSION_TIMEOUT_KEY , 15000 );
134
145
config = new EtcdDynamicConfiguration (url );
135
146
}
136
147
137
- @ AfterAll
138
- static void tearDown () {
139
- etcdClient .close ();
148
+ @ After
149
+ public void tearDown () {
150
+ etcdCluster .close ();
140
151
}
152
+
141
153
}
0 commit comments