forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegistryDirectoryTest.java
1126 lines (924 loc) · 48.3 KB
/
RegistryDirectoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.dubbo;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.LogUtil;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import org.apache.dubbo.registry.integration.RegistryDirectory;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.cluster.RouterChain;
import org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import org.apache.dubbo.rpc.cluster.router.script.ScriptRouterFactory;
import org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import javax.script.ScriptEngineManager;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings({"rawtypes", "unchecked"})
public class RegistryDirectoryTest {
private static boolean isScriptUnsupported = new ScriptEngineManager().getEngineByName("javascript") == null;
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
String service = DemoService.class.getName();
RpcInvocation invocation = new RpcInvocation();
URL noMeaningUrl = URL.valueOf("notsupport:/" + service + "?refer=" + URL.encode("interface=" + service));
URL SERVICEURL = URL.valueOf("dubbo://127.0.0.1:9091/" + service + "?lazy=true&side=consumer&application=mockName");
URL SERVICEURL2 = URL.valueOf("dubbo://127.0.0.1:9092/" + service + "?lazy=true&side=consumer&application=mockName");
URL SERVICEURL3 = URL.valueOf("dubbo://127.0.0.1:9093/" + service + "?lazy=true&side=consumer&application=mockName");
URL SERVICEURL_DUBBO_NOPATH = URL.valueOf("dubbo://127.0.0.1:9092" + "?lazy=true&side=consumer&application=mockName");
private Registry registry = Mockito.mock(Registry.class);
@BeforeEach
public void setUp() {
}
private RegistryDirectory getRegistryDirectory(URL url) {
RegistryDirectory registryDirectory = new RegistryDirectory(URL.class, url);
registryDirectory.setProtocol(protocol);
registryDirectory.setRegistry(registry);
registryDirectory.setRouterChain(RouterChain.buildChain(url));
registryDirectory.subscribe(url);
// asert empty
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(0, invokers.size());
Assertions.assertEquals(false, registryDirectory.isAvailable());
return registryDirectory;
}
private RegistryDirectory getRegistryDirectory() {
return getRegistryDirectory(noMeaningUrl);
}
@Test
public void test_Constructor_WithErrorParam() {
try {
new RegistryDirectory(null, null);
fail();
} catch (IllegalArgumentException e) {
}
try {
// null url
new RegistryDirectory(null, noMeaningUrl);
fail();
} catch (IllegalArgumentException e) {
}
try {
// no servicekey
new RegistryDirectory(RegistryDirectoryTest.class, URL.valueOf("dubbo://10.20.30.40:9090"));
fail();
} catch (IllegalArgumentException e) {
}
}
@Test
public void test_Constructor_CheckStatus() throws Exception {
URL url = URL.valueOf("notsupported://10.20.30.40/" + service + "?a=b").addParameterAndEncoded(Constants.REFER_KEY,
"foo=bar");
RegistryDirectory reg = getRegistryDirectory(url);
Field field = reg.getClass().getDeclaredField("queryMap");
field.setAccessible(true);
Map<String, String> queryMap = (Map<String, String>) field.get(reg);
Assertions.assertEquals("bar", queryMap.get("foo"));
Assertions.assertEquals(url.clearParameters().addParameter("foo", "bar"), reg.getUrl());
}
@Test
public void testNotified_Normal() {
RegistryDirectory registryDirectory = getRegistryDirectory();
test_Notified2invokers(registryDirectory);
test_Notified1invokers(registryDirectory);
test_Notified3invokers(registryDirectory);
testforbid(registryDirectory);
}
/**
* Test push only router
*/
@Test
public void testNotified_Normal_withRouters() {
LogUtil.start();
RegistryDirectory registryDirectory = getRegistryDirectory();
test_Notified1invokers(registryDirectory);
test_Notified_only_routers(registryDirectory);
Assertions.assertEquals(true, registryDirectory.isAvailable());
Assertions.assertTrue(LogUtil.checkNoError(), "notify no invoker urls ,should not error");
LogUtil.stop();
test_Notified2invokers(registryDirectory);
}
@Test
public void testNotified_WithError() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
// ignore error log
URL badurl = URL.valueOf("notsupported://127.0.0.1/" + service);
serviceUrls.add(badurl);
serviceUrls.add(SERVICEURL);
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
}
@Test
public void testNotified_WithDuplicateUrls() {
List<URL> serviceUrls = new ArrayList<URL>();
// ignore error log
serviceUrls.add(SERVICEURL);
serviceUrls.add(SERVICEURL);
RegistryDirectory registryDirectory = getRegistryDirectory();
registryDirectory.notify(serviceUrls);
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
}
// forbid
private void testforbid(RegistryDirectory registryDirectory) {
invocation = new RpcInvocation();
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(new URL(Constants.EMPTY_PROTOCOL, Constants.ANYHOST_VALUE, 0, service, Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(false,
registryDirectory.isAvailable(), "invokers size=0 ,then the registry directory is not available");
try {
registryDirectory.list(invocation);
fail("forbid must throw RpcException");
} catch (RpcException e) {
Assertions.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode());
}
}
//The test call is independent of the path of the registry url
@Test
public void test_NotifiedDubbo1() {
URL errorPathUrl = URL.valueOf("notsupport:/" + "xxx" + "?refer=" + URL.encode("interface=" + service));
RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
List<URL> serviceUrls = new ArrayList<URL>();
URL Dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true");
serviceUrls.add(Dubbo1URL.addParameter("methods", "getXXX"));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invocation = new RpcInvocation();
List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
invocation.setMethodName("getXXX");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
Assertions.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath());
}
// notify one invoker
private void test_Notified_only_routers(RegistryDirectory registryDirectory) {
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(URL.valueOf("empty://127.0.0.1/?category=routers"));
registryDirectory.notify(serviceUrls);
}
// notify one invoker
private void test_Notified1invokers(RegistryDirectory registryDirectory) {
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1").addParameter(Constants.APPLICATION_KEY, "mockApplicationName"));// .addParameter("refer.autodestroy", "true")
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
invocation.setMethodName("getXXX");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
invocation.setMethodName("getXXX1");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
invocation.setMethodName("getXXX2");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
}
// 2 invokers===================================
private void test_Notified2invokers(RegistryDirectory registryDirectory) {
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
invocation.setMethodName("getXXX");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
invocation.setMethodName("getXXX1");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
}
// 3 invoker notifications===================================
private void test_Notified3invokers(RegistryDirectory registryDirectory) {
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(3, invokers.size());
invocation.setMethodName("getXXX");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(3, invokers.size());
invocation.setMethodName("getXXX1");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(3, invokers.size());
invocation.setMethodName("getXXX2");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(3, invokers.size());
invocation.setMethodName("getXXX3");
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(3, invokers.size());
}
@Test
public void testParametersMerge() {
RegistryDirectory registryDirectory = getRegistryDirectory();
URL regurl = noMeaningUrl.addParameter("test", "reg").addParameterAndEncoded(Constants.REFER_KEY,
"key=query&"
+ Constants.LOADBALANCE_KEY
+ "="
+ LeastActiveLoadBalance.NAME);
RegistryDirectory<RegistryDirectoryTest> registryDirectory2 = new RegistryDirectory(
RegistryDirectoryTest.class,
regurl);
registryDirectory2.setProtocol(protocol);
List<URL> serviceUrls = new ArrayList<URL>();
// The parameters of the inspection registry need to be cleared
{
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Invoker invoker = (Invoker) invokers.get(0);
URL url = invoker.getUrl();
Assertions.assertEquals(null, url.getParameter("key"));
}
// The parameters of the provider for the inspection service need merge
{
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX2").addParameter("key", "provider"));
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Invoker invoker = (Invoker) invokers.get(0);
URL url = invoker.getUrl();
Assertions.assertEquals("provider", url.getParameter("key"));
}
// The parameters of the test service query need to be with the providermerge.
{
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX3").addParameter("key", "provider"));
registryDirectory2.setRegistry(registry);
registryDirectory2.setRouterChain(RouterChain.buildChain(noMeaningUrl));
registryDirectory2.subscribe(noMeaningUrl);
registryDirectory2.notify(serviceUrls);
invocation = new RpcInvocation();
List invokers = registryDirectory2.list(invocation);
Invoker invoker = (Invoker) invokers.get(0);
URL url = invoker.getUrl();
Assertions.assertEquals("query", url.getParameter("key"));
}
{
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Invoker invoker = (Invoker) invokers.get(0);
URL url = invoker.getUrl();
Assertions.assertEquals(false, url.getParameter(Constants.CHECK_KEY, false));
}
{
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter(Constants.LOADBALANCE_KEY, RoundRobinLoadBalance.NAME));
registryDirectory2.notify(serviceUrls);
invocation = new RpcInvocation();
invocation.setMethodName("get");
List invokers = registryDirectory2.list(invocation);
Invoker invoker = (Invoker) invokers.get(0);
URL url = invoker.getUrl();
Assertions.assertEquals(LeastActiveLoadBalance.NAME, url.getMethodParameter("get", Constants.LOADBALANCE_KEY));
}
//test geturl
{
Assertions.assertEquals(null, registryDirectory2.getUrl().getParameter("mock"));
serviceUrls.clear();
serviceUrls.add(SERVICEURL.addParameter(Constants.MOCK_KEY, "true"));
registryDirectory2.notify(serviceUrls);
Assertions.assertEquals("true", registryDirectory2.getUrl().getParameter("mock"));
}
}
/**
* When destroying, RegistryDirectory should: 1. be disconnected from Registry 2. destroy all invokers
*/
@Test
public void testDestroy() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
List<Invoker> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(true, registryDirectory.isAvailable());
Assertions.assertEquals(true, invokers.get(0).isAvailable());
registryDirectory.destroy();
Assertions.assertEquals(false, registryDirectory.isAvailable());
Assertions.assertEquals(false, invokers.get(0).isAvailable());
registryDirectory.destroy();
List<Invoker<RegistryDirectoryTest>> cachedInvokers = registryDirectory.getInvokers();
Map<String, Invoker<RegistryDirectoryTest>> urlInvokerMap = registryDirectory.getUrlInvokerMap();
Assertions.assertTrue(cachedInvokers == null);
Assertions.assertEquals(0, urlInvokerMap.size());
// List<U> urls = mockRegistry.getSubscribedUrls();
RpcInvocation inv = new RpcInvocation();
try {
registryDirectory.list(inv);
fail();
} catch (RpcException e) {
Assertions.assertTrue(e.getMessage().contains("already destroyed"));
}
}
@Test
public void testDestroy_WithDestroyRegistry() {
RegistryDirectory registryDirectory = getRegistryDirectory();
CountDownLatch latch = new CountDownLatch(1);
registryDirectory.setRegistry(new MockRegistry(latch));
registryDirectory.subscribe(URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/DemoService?category=providers"));
registryDirectory.destroy();
Assertions.assertEquals(0, latch.getCount());
}
@Test
public void testDestroy_WithDestroyRegistry_WithError() {
RegistryDirectory registryDirectory = getRegistryDirectory();
registryDirectory.setRegistry(new MockRegistry(true));
registryDirectory.destroy();
}
@Test
public void testDubbo1UrlWithGenericInvocation() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
URL serviceURL = SERVICEURL_DUBBO_NOPATH.addParameter("methods", "getXXX1,getXXX2,getXXX3");
serviceUrls.add(serviceURL);
registryDirectory.notify(serviceUrls);
// Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException;
invocation = new RpcInvocation(Constants.$INVOKE, new Class[]{String.class, String[].class, Object[].class},
new Object[]{"getXXX1", "", new Object[]{}});
List<Invoker> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
// Assertions.assertEquals(
// serviceURL.setPath(service).addParameters("check", "false", "interface", DemoService.class.getName(), REMOTE_APPLICATION_KEY, serviceURL.getParameter(APPLICATION_KEY))
// , invokers.get(0).getUrl()
// );
}
;
/**
* When the first arg of a method is String or Enum, Registry server can do parameter-value-based routing.
*/
@Disabled("Parameter routing is not available at present.")
@Test
public void testParmeterRoute() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1.napoli"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1.MORGAN,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1.morgan,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation(
Constants.$INVOKE,
new Class[]{String.class, String[].class, Object[].class},
new Object[]{"getXXX1", new String[]{"Enum"}, new Object[]{Param.MORGAN}});
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
}
/**
* Empty notify cause forbidden, non-empty notify cancels forbidden state
*/
@Test
public void testEmptyNotifyCauseForbidden() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List invokers = null;
List<URL> serviceUrls = new ArrayList<URL>();
registryDirectory.notify(serviceUrls);
RpcInvocation inv = new RpcInvocation();
try {
invokers = registryDirectory.list(inv);
} catch (RpcException e) {
Assertions.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode());
Assertions.assertEquals(false, registryDirectory.isAvailable());
}
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));
registryDirectory.notify(serviceUrls);
inv.setMethodName("getXXX2");
invokers = registryDirectory.list(inv);
Assertions.assertEquals(true, registryDirectory.isAvailable());
Assertions.assertEquals(3, invokers.size());
}
/**
* 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
* no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
* current routers
*/
@Test
public void testNotifyRouterUrls() {
if (isScriptUnsupported) return;
RegistryDirectory registryDirectory = getRegistryDirectory();
URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");
List<URL> serviceUrls = new ArrayList<URL>();
// without ROUTER_KEY, the first router should not be created.
serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
"notsupported").addParameter(Constants.RULE_KEY,
"function test1(){}"));
serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY,
ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY,
"function test1(){}"));
// FIXME
/*registryDirectory.notify(serviceUrls);
RouterChain routerChain = registryDirectory.getRouterChain();
//default invocation selector
Assertions.assertEquals(1 + 1, routers.size());
Assertions.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());
registryDirectory.notify(new ArrayList<URL>());
routers = registryDirectory.getRouters();
Assertions.assertEquals(1 + 1, routers.size());
Assertions.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());
serviceUrls.clear();
serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
registryDirectory.notify(serviceUrls);
routers = registryDirectory.getRouters();
Assertions.assertEquals(0 + 1, routers.size());*/
}
/**
* Test whether the override rule have a high priority
* Scene: first push override , then push invoker
*/
@Test
public void testNotifyoverrideUrls_beforeInvoker() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> overrideUrls = new ArrayList<URL>();
overrideUrls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
registryDirectory.notify(overrideUrls);
//The registry is initially pushed to override only, and the dirctory state should be false because there is no invoker.
Assertions.assertEquals(false, registryDirectory.isAvailable());
//After pushing two provider, the directory state is restored to true
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("timeout", "1000"));
serviceUrls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10"));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
//Start validation of parameter values
invocation = new RpcInvocation();
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority");
Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority");
}
/**
* Test whether the override rule have a high priority
* Scene: first push override , then push invoker
*/
@Test
public void testNotifyoverrideUrls_afterInvoker() {
RegistryDirectory registryDirectory = getRegistryDirectory();
//After pushing two provider, the directory state is restored to true
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("timeout", "1000"));
serviceUrls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10"));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
List<URL> overrideUrls = new ArrayList<URL>();
overrideUrls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
registryDirectory.notify(overrideUrls);
//Start validation of parameter values
invocation = new RpcInvocation();
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority");
Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority");
}
/**
* Test whether the override rule have a high priority
* Scene: push override rules with invoker
*/
@Test
public void testNotifyoverrideUrls_withInvoker() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.addParameter("timeout", "1000"));
durls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10"));
durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
registryDirectory.notify(durls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
//Start validation of parameter values
invocation = new RpcInvocation();
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Assertions.assertEquals("1", invokers.get(0).getUrl().getParameter("timeout"), "override rute must be first priority");
Assertions.assertEquals("5", invokers.get(0).getUrl().getParameter("connections"), "override rute must be first priority");
}
/**
* Test whether the override rule have a high priority
* Scene: the rules of the push are the same as the parameters of the provider
* Expectation: no need to be re-referenced
*/
@Test
public void testNotifyoverrideUrls_Nouse() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.addParameter("timeout", "1"));//One is the same, one is different
durls.add(SERVICEURL2.addParameter("timeout", "1").addParameter("connections", "5"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Map<String, Invoker<?>> map = new HashMap<>();
map.put(invokers.get(0).getUrl().getAddress(), invokers.get(0));
map.put(invokers.get(1).getUrl().getAddress(), invokers.get(1));
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
registryDirectory.notify(durls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Map<String, Invoker<?>> map2 = new HashMap<>();
map2.put(invokers.get(0).getUrl().getAddress(), invokers.get(0));
map2.put(invokers.get(1).getUrl().getAddress(), invokers.get(1));
//The parameters are different and must be rereferenced.
Assertions.assertFalse(map.get(SERVICEURL.getAddress()) == map2.get(SERVICEURL.getAddress()), "object should not same");
//The parameters can not be rereferenced
Assertions.assertTrue(map.get(SERVICEURL2.getAddress()) == map2.get(SERVICEURL2.getAddress()), "object should not same");
}
/**
* Test override rules for a certain provider
*/
@Test
public void testNofityOverrideUrls_Provider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1").addParameter(Constants.SIDE_KEY, Constants.CONSUMER_SIDE));//One is the same, one is different
durls.add(SERVICEURL2.setHost("10.20.30.141").addParameter("timeout", "2").addParameter(Constants.SIDE_KEY, Constants.CONSUMER_SIDE));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=3"));
durls.add(URL.valueOf("override://10.20.30.141:9092?timeout=4"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
URL aUrl = invokers.get(0).getUrl();
URL bUrl = invokers.get(1).getUrl();
Assertions.assertEquals(aUrl.getHost().equals("10.20.30.140") ? "3" : "4", aUrl.getParameter("timeout"));
Assertions.assertEquals(bUrl.getHost().equals("10.20.30.141") ? "4" : "3", bUrl.getParameter("timeout"));
}
/**
* Test cleanup override rules, and sent remove rules and other override rules
* Whether the test can be restored to the providerUrl when it is pushed
*/
@Test
public void testNofityOverrideUrls_Clean1() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=1000"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=3"));
durls.add(URL.valueOf("override://0.0.0.0"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Invoker<?> aInvoker = invokers.get(0);
//Need to be restored to the original providerUrl
Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout"));
}
/**
* The test clears the override rule and only sends the override cleanup rules
* Whether the test can be restored to the providerUrl when it is pushed
*/
@Test
public void testNofityOverrideUrls_CleanOnly() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
registryDirectory.notify(durls);
Assertions.assertEquals(null, registryDirectory.getUrl().getParameter("mock"));
//override
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=1000&mock=fail"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Invoker<?> aInvoker = invokers.get(0);
Assertions.assertEquals("1000", aInvoker.getUrl().getParameter("timeout"));
Assertions.assertEquals("fail", registryDirectory.getUrl().getParameter("mock"));
//override clean
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0/dubbo.test.api.HelloService"));
registryDirectory.notify(durls);
invokers = registryDirectory.list(invocation);
aInvoker = invokers.get(0);
//Need to be restored to the original providerUrl
Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout"));
Assertions.assertEquals(null, registryDirectory.getUrl().getParameter("mock"));
}
/**
* Test the simultaneous push to clear the override and the override for a certain provider
* See if override can take effect
*/
@Test
public void testNofityOverrideUrls_CleanNOverride() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=3"));
durls.add(URL.valueOf("override://0.0.0.0"));
durls.add(URL.valueOf("override://10.20.30.140:9091?timeout=4"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Invoker<?> aInvoker = invokers.get(0);
Assertions.assertEquals("4", aInvoker.getUrl().getParameter("timeout"));
}
/**
* Test override disables all service providers through enable=false
* Expectation: all service providers can not be disabled through override.
*/
@Test
public void testNofityOverrideUrls_disabled_allProvider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140"));
durls.add(SERVICEURL.setHost("10.20.30.141"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?" + Constants.ENABLED_KEY + "=false"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
//All service providers can not be disabled through override.
Assertions.assertEquals(2, invokers.size());
}
/**
* Test override disables a specified service provider through enable=false
* It is expected that a specified service provider can be disable.
*/
@Test
public void testNofityOverrideUrls_disabled_specifiedProvider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140"));
durls.add(SERVICEURL.setHost("10.20.30.141"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://10.20.30.140:9091?" + Constants.DISABLED_KEY + "=true"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
Assertions.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost());
durls = new ArrayList<URL>();
durls.add(URL.valueOf("empty://0.0.0.0?" + Constants.DISABLED_KEY + "=true&" + Constants.CATEGORY_KEY + "=" + Constants.CONFIGURATORS_CATEGORY));
registryDirectory.notify(durls);
List<Invoker<?>> invokers2 = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers2.size());
}
/**
* Test override disables a specified service provider through enable=false
* It is expected that a specified service provider can be disable.
*/
@Test
public void testNofity_To_Decrease_provider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140"));
durls.add(SERVICEURL.setHost("10.20.30.141"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers2 = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers2.size());
Assertions.assertEquals("10.20.30.140", invokers2.get(0).getUrl().getHost());
durls = new ArrayList<URL>();
durls.add(URL.valueOf("empty://0.0.0.0?" + Constants.DISABLED_KEY + "=true&" + Constants.CATEGORY_KEY + "=" + Constants.CONFIGURATORS_CATEGORY));
registryDirectory.notify(durls);
List<Invoker<?>> invokers3 = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers3.size());
}
/**
* Test override disables a specified service provider through enable=false
* It is expected that a specified service provider can be disable.
*/
@Test
public void testNofity_disabled_specifiedProvider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
// Initially disable
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140").addParameter(Constants.ENABLED_KEY, "false"));
durls.add(SERVICEURL.setHost("10.20.30.141"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
Assertions.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost());
//Enabled by override rule
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://10.20.30.140:9091?" + Constants.DISABLED_KEY + "=false"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers2 = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers2.size());
}
@Test
public void testNotifyRouterUrls_Clean() {
if (isScriptUnsupported) return;
RegistryDirectory registryDirectory = getRegistryDirectory();
URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/").addParameter(Constants.ROUTER_KEY,
"javascript").addParameter(Constants.RULE_KEY,
"function test1(){}").addParameter(Constants.ROUTER_KEY,
"script"); // FIX
// BAD
List<URL> serviceUrls = new ArrayList<URL>();
// without ROUTER_KEY, the first router should not be created.
serviceUrls.add(routerurl);
registryDirectory.notify(serviceUrls);
// FIXME
/* List routers = registryDirectory.getRouters();
Assertions.assertEquals(1 + 1, routers.size());
serviceUrls.clear();
serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
registryDirectory.notify(serviceUrls);
routers = registryDirectory.getRouters();
Assertions.assertEquals(0 + 1, routers.size());*/
}
/**
* Test mock provider distribution
*/
@Test
public void testNotify_MockProviderOnly() {
RegistryDirectory registryDirectory = getRegistryDirectory();
List<URL> serviceUrls = new ArrayList<URL>();
serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));
registryDirectory.notify(serviceUrls);
Assertions.assertEquals(true, registryDirectory.isAvailable());
invocation = new RpcInvocation();
List invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
RpcInvocation mockinvocation = new RpcInvocation();
mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
invokers = registryDirectory.list(mockinvocation);
Assertions.assertEquals(1, invokers.size());
}
// mock protocol
//Test the matching of protocol and select only the matched protocol for refer
@Test
public void test_Notified_acceptProtocol0() {
URL errorPathUrl = URL.valueOf("notsupport:/xxx?refer=" + URL.encode("interface=" + service));
RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
List<URL> serviceUrls = new ArrayList<URL>();
URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
serviceUrls.add(dubbo1URL);
serviceUrls.add(dubbo2URL);
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation();
List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
}
//Test the matching of protocol and select only the matched protocol for refer
@Test
public void test_Notified_acceptProtocol1() {
URL errorPathUrl = URL.valueOf("notsupport:/xxx");
errorPathUrl = errorPathUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface=" + service + "&protocol=dubbo");