17
17
18
18
package com .tencent .polaris .circuitbreaker .factory .test ;
19
19
20
- import java .io .BufferedReader ;
21
20
import java .io .IOException ;
22
- import java .io .InputStream ;
23
- import java .io .InputStreamReader ;
24
- import java .nio .charset .StandardCharsets ;
25
21
import java .util .Collection ;
26
22
import java .util .Map ;
27
23
import java .util .Optional ;
28
24
import java .util .function .Consumer ;
29
- import java .util .stream .Collectors ;
30
25
31
26
import com .google .common .cache .Cache ;
32
27
import com .google .protobuf .StringValue ;
33
- import com .google .protobuf .util .JsonFormat ;
34
28
import com .tencent .polaris .api .config .Configuration ;
35
29
import com .tencent .polaris .api .plugin .circuitbreaker .CircuitBreaker ;
36
30
import com .tencent .polaris .api .plugin .circuitbreaker .entity .Resource ;
37
31
import com .tencent .polaris .api .pojo .ServiceKey ;
38
- import com .tencent .polaris .api .utils .StringUtils ;
39
32
import com .tencent .polaris .circuitbreak .api .CircuitBreakAPI ;
40
33
import com .tencent .polaris .circuitbreak .api .FunctionalDecorator ;
41
34
import com .tencent .polaris .circuitbreak .api .pojo .FunctionalDecoratorRequest ;
@@ -83,45 +76,24 @@ public void before() throws IOException {
83
76
Assert .fail (e .getMessage ());
84
77
}
85
78
86
- CircuitBreakerProto .CircuitBreakerRule cbRule1 = loadCbRule ("circuitBreakerMethodRuleNoDetect.json" );
79
+ CircuitBreakerProto .CircuitBreakerRule cbRule1 = CbTestUtils . loadCbRule ("circuitBreakerMethodRuleNoDetect.json" );
87
80
CircuitBreakerProto .CircuitBreaker circuitBreaker = CircuitBreakerProto .CircuitBreaker .newBuilder ()
88
81
.addRules (cbRule1 ).setRevision (StringValue .newBuilder ().setValue ("0000" ).build ()).build ();
89
82
namingServer .getNamingService ().setCircuitBreaker (matchMethodService , circuitBreaker );
90
83
91
84
92
- CircuitBreakerProto .CircuitBreakerRule cbRule3 = loadCbRule ("circuitBreakerMethodRule.json" );
93
- CircuitBreakerProto .CircuitBreakerRule cbRule4 = loadCbRule ("circuitBreakerRule.json" );
85
+ CircuitBreakerProto .CircuitBreakerRule cbRule3 = CbTestUtils . loadCbRule ("circuitBreakerMethodRule.json" );
86
+ CircuitBreakerProto .CircuitBreakerRule cbRule4 = CbTestUtils . loadCbRule ("circuitBreakerRule.json" );
94
87
circuitBreaker = CircuitBreakerProto .CircuitBreaker .newBuilder ()
95
88
.addRules (cbRule3 ).addRules (cbRule4 ).setRevision (StringValue .newBuilder ().setValue ("1111" ).build ()).build ();
96
89
namingServer .getNamingService ().setCircuitBreaker (matchMethodDetectService , circuitBreaker );
97
- FaultDetectorProto .FaultDetectRule rule1 = loadFdRule ("faultDetectRule.json" );
98
- FaultDetectorProto .FaultDetectRule rule2 = loadFdRule ("faultDetectMethodRule.json" );
90
+ FaultDetectorProto .FaultDetectRule rule1 = CbTestUtils . loadFdRule ("faultDetectRule.json" );
91
+ FaultDetectorProto .FaultDetectRule rule2 = CbTestUtils . loadFdRule ("faultDetectMethodRule.json" );
99
92
FaultDetectorProto .FaultDetector faultDetector = FaultDetectorProto .FaultDetector .newBuilder ()
100
93
.addRules (rule1 ).addRules (rule2 ).setRevision ("2222" ).build ();
101
94
namingServer .getNamingService ().setFaultDetector (matchMethodDetectService , faultDetector );
102
95
}
103
96
104
- private CircuitBreakerProto .CircuitBreakerRule loadCbRule (String fileName ) throws IOException {
105
- CircuitBreakerProto .CircuitBreakerRule .Builder circuitBreakerRuleBuilder = CircuitBreakerProto .CircuitBreakerRule
106
- .newBuilder ();
107
- InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (fileName );
108
- Assert .assertNotNull (inputStream );
109
- String json = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 )).lines ()
110
- .collect (Collectors .joining ("" ));
111
- JsonFormat .parser ().ignoringUnknownFields ().merge (json , circuitBreakerRuleBuilder );
112
- return circuitBreakerRuleBuilder .build ();
113
- }
114
-
115
- private FaultDetectorProto .FaultDetectRule loadFdRule (String fileName ) throws IOException {
116
- FaultDetectorProto .FaultDetectRule .Builder builder = FaultDetectorProto .FaultDetectRule .newBuilder ();
117
- InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (fileName );
118
- Assert .assertNotNull (inputStream );
119
- String json = new BufferedReader (new InputStreamReader (inputStream , StandardCharsets .UTF_8 )).lines ()
120
- .collect (Collectors .joining ("" ));
121
- JsonFormat .parser ().ignoringUnknownFields ().merge (json , builder );
122
- return builder .build ();
123
- }
124
-
125
97
@ Test
126
98
public void testMultipleUrlsNoRule () {
127
99
Configuration configuration = TestUtils .configWithEnvAddress ();
@@ -143,18 +115,13 @@ public void testMultipleUrlsNoRule() {
143
115
});
144
116
integerConsumer .accept (1 );
145
117
}
146
- try {
147
- Thread .sleep (10 * 1000 );
148
- }
149
- catch (InterruptedException e ) {
150
- throw new RuntimeException (e );
151
- }
152
-
118
+ Utils .sleepUninterrupted (10 * 1000 );
153
119
BaseEngine baseEngine = (BaseEngine ) circuitBreakAPI ;
154
120
CircuitBreaker resourceBreaker = baseEngine .getSDKContext ().getExtensions ().getResourceBreaker ();
155
121
PolarisCircuitBreaker polarisCircuitBreaker = (PolarisCircuitBreaker ) resourceBreaker ;
156
122
Cache <Resource , Optional <ResourceCounters >> methodCache = polarisCircuitBreaker .getCountersCache ()
157
123
.get (CircuitBreakerProto .Level .METHOD );
124
+ polarisCircuitBreaker .cleanupExpiredResources ();
158
125
Assert .assertEquals (0 , methodCache .size ());
159
126
}
160
127
}
@@ -178,74 +145,26 @@ public void testMultipleUrlsMethodRule() {
178
145
System .out .println ("invoke success" + finalI );
179
146
}
180
147
});
181
- try {
148
+ if ( i == 0 ) {
182
149
integerConsumer .accept (1 );
183
- Utils .sleepUninterrupted (1000 );
184
- integerConsumer .accept (2 );
185
- Utils .sleepUninterrupted (1000 );
186
- }
187
- catch (Exception e ) {
188
- if (!(e instanceof IllegalArgumentException )) {
189
- throw e ;
150
+ try {
151
+ integerConsumer .accept (2 );
152
+ } catch (Exception e ) {
153
+ if (!(e instanceof IllegalArgumentException )) {
154
+ throw e ;
155
+ }
190
156
}
157
+ Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (3 ));
158
+ } else {
159
+ Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (1 ));
191
160
}
192
- Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (3 ));
193
- }
194
- Utils .sleepUninterrupted (20 * 1000 );
195
-
196
- BaseEngine baseEngine = (BaseEngine ) circuitBreakAPI ;
197
- CircuitBreaker resourceBreaker = baseEngine .getSDKContext ().getExtensions ().getResourceBreaker ();
198
- PolarisCircuitBreaker polarisCircuitBreaker = (PolarisCircuitBreaker ) resourceBreaker ;
199
- Cache <Resource , Optional <ResourceCounters >> methodCache = polarisCircuitBreaker .getCountersCache ()
200
- .get (CircuitBreakerProto .Level .METHOD );
201
- Assert .assertEquals (0 , methodCache .size ());
202
- }
203
- }
204
-
205
- @ Test
206
- public void testFaultDetector () {
207
- Configuration configuration = TestUtils .configWithEnvAddress ();
208
- ConfigurationImpl configurationImpl = (ConfigurationImpl ) configuration ;
209
- configurationImpl .getConsumer ().getCircuitBreaker ().setCountersExpireInterval (5000 );
210
- try (CircuitBreakAPI circuitBreakAPI = CircuitBreakAPIFactory .createCircuitBreakAPIByConfig (configurationImpl )) {
211
- for (int i = 0 ; i < 50 ; i ++) {
212
- String method = "" ;
213
- if (i > 0 ) {
214
- method = "/test1/path/" + i ;
215
- }
216
- FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest (
217
- matchMethodDetectService , method );
218
- FunctionalDecorator decorator = circuitBreakAPI .makeFunctionalDecorator (makeDecoratorRequest );
219
- int finalI = i ;
220
- Consumer <Integer > integerConsumer = decorator .decorateConsumer (num -> {
221
- if (num % 2 == 0 ) {
222
- throw new IllegalArgumentException ("invoke failed" + finalI );
223
- }
224
- else {
225
- System .out .println ("invoke success" + finalI );
226
- }
227
- });
228
- integerConsumer .accept (1 );
229
161
}
230
162
BaseEngine baseEngine = (BaseEngine ) circuitBreakAPI ;
231
163
CircuitBreaker resourceBreaker = baseEngine .getSDKContext ().getExtensions ().getResourceBreaker ();
232
164
PolarisCircuitBreaker polarisCircuitBreaker = (PolarisCircuitBreaker ) resourceBreaker ;
233
- Map <ServiceKey , HealthCheckContainer > healthCheckCache = polarisCircuitBreaker .getHealthCheckCache ();
234
- Assert .assertEquals (1 , healthCheckCache .size ());
235
- HealthCheckContainer healthCheckContainer = healthCheckCache .get (matchMethodDetectService );
236
- Assert .assertNotNull (healthCheckContainer );
237
- Collection <ResourceHealthChecker > healthCheckerValues = healthCheckContainer .getHealthCheckerValues ();
238
- Assert .assertEquals (2 , healthCheckerValues .size ());
239
- for (ResourceHealthChecker resourceHealthChecker : healthCheckerValues ) {
240
- if (StringUtils .equals (resourceHealthChecker .getFaultDetectRule ().getId (), "fd1" )) {
241
- Assert .assertEquals (1 , resourceHealthChecker .getResources ().size ());
242
- }
243
- }
244
- Utils .sleepUninterrupted (20 * 1000 );
245
-
246
165
Cache <Resource , Optional <ResourceCounters >> methodCache = polarisCircuitBreaker .getCountersCache ()
247
166
.get (CircuitBreakerProto .Level .METHOD );
248
- Assert .assertEquals (0 , methodCache .size ());
167
+ Assert .assertEquals (1 , methodCache .size ());
249
168
}
250
169
}
251
170
@@ -273,7 +192,7 @@ public void testCircuitBreakerRuleChanged() throws IOException {
273
192
});
274
193
integerConsumer .accept (1 );
275
194
}
276
- CircuitBreakerProto .CircuitBreakerRule cbRule1 = loadCbRule ("circuitBreakerMethodRuleChanged.json" );
195
+ CircuitBreakerProto .CircuitBreakerRule cbRule1 = CbTestUtils . loadCbRule ("circuitBreakerMethodRuleChanged.json" );
277
196
CircuitBreakerProto .CircuitBreaker circuitBreaker = CircuitBreakerProto .CircuitBreaker .newBuilder ()
278
197
.addRules (cbRule1 ).setRevision (StringValue .newBuilder ().setValue ("444441" ).build ()).build ();
279
198
namingServer .getNamingService ().setCircuitBreaker (matchMethodDetectService , circuitBreaker );
@@ -304,101 +223,29 @@ public void testCircuitBreakerRuleChanged() throws IOException {
304
223
System .out .println ("invoke success" + finalI );
305
224
}
306
225
});
307
- try {
308
- integerConsumer .accept (1 );
309
- } catch (Exception e ) {
310
- if (!(e instanceof IllegalArgumentException )) {
311
- throw e ;
226
+ if (i == 0 ) {
227
+ try {
228
+ integerConsumer .accept (1 );
312
229
}
313
- System .out .println ("invoke 1 failed" + finalI );
314
- }
315
- Utils .sleepUninterrupted (1000 );
316
- try {
317
- integerConsumer .accept (2 );
318
- } catch (Exception e ) {
319
- if (!(e instanceof IllegalArgumentException )) {
320
- throw e ;
230
+ catch (Exception e ) {
231
+ if (!(e instanceof IllegalArgumentException )) {
232
+ throw e ;
233
+ }
321
234
}
322
- System .out .println ("invoke 2 failed" + finalI );
323
- }
324
- Utils .sleepUninterrupted (1000 );
325
- Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (3 ));
326
- }
327
- }
328
- }
329
-
330
- @ Test
331
- public void testFaultDetectRuleChanged () throws IOException {
332
- Configuration configuration = TestUtils .configWithEnvAddress ();
333
- ConfigurationImpl configurationImpl = (ConfigurationImpl ) configuration ;
334
- try (CircuitBreakAPI circuitBreakAPI = CircuitBreakAPIFactory .createCircuitBreakAPIByConfig (configurationImpl )) {
335
- for (int i = 0 ; i < 10 ; i ++) {
336
- String method = "" ;
337
- if (i < 9 ) {
338
- method = "/test1/path/" + i ;
339
- }
340
- FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest (
341
- matchMethodDetectService , method );
342
- FunctionalDecorator decorator = circuitBreakAPI .makeFunctionalDecorator (makeDecoratorRequest );
343
- int finalI = i ;
344
- Consumer <Integer > integerConsumer = decorator .decorateConsumer (num -> {
345
- if (num % 2 == 0 ) {
346
- throw new IllegalArgumentException ("invoke failed" + finalI );
235
+ try {
236
+ integerConsumer .accept (2 );
347
237
}
348
- else {
349
- System .out .println ("invoke success" + finalI );
238
+ catch (Exception e ) {
239
+ if (!(e instanceof IllegalArgumentException )) {
240
+ throw e ;
241
+ }
350
242
}
351
- });
352
- integerConsumer .accept (1 );
353
- }
354
- BaseEngine baseEngine = (BaseEngine ) circuitBreakAPI ;
355
- CircuitBreaker resourceBreaker = baseEngine .getSDKContext ().getExtensions ().getResourceBreaker ();
356
- PolarisCircuitBreaker polarisCircuitBreaker = (PolarisCircuitBreaker ) resourceBreaker ;
357
- Map <ServiceKey , HealthCheckContainer > healthCheckCache = polarisCircuitBreaker .getHealthCheckCache ();
358
- Assert .assertEquals (1 , healthCheckCache .size ());
359
- HealthCheckContainer healthCheckContainer = healthCheckCache .get (matchMethodDetectService );
360
- Assert .assertNotNull (healthCheckContainer );
361
- Collection <ResourceHealthChecker > healthCheckerValues = healthCheckContainer .getHealthCheckerValues ();
362
- Assert .assertEquals (2 , healthCheckerValues .size ());
363
- // for (ResourceHealthChecker resourceHealthChecker : healthCheckerValues) {
364
- // if (StringUtils.equals(resourceHealthChecker.getFaultDetectRule().getId(), "fd1")) {
365
- // Assert.assertEquals(1, resourceHealthChecker.getResources().size());
366
- // }
367
- // }
368
- FaultDetectorProto .FaultDetectRule rule1 = loadFdRule ("faultDetectMethodRuleChanged.json" );
369
- FaultDetectorProto .FaultDetector faultDetector = FaultDetectorProto .FaultDetector .newBuilder ()
370
- .addRules (rule1 ).setRevision ("33333" ).build ();
371
- namingServer .getNamingService ().setFaultDetector (matchMethodDetectService , faultDetector );
372
-
373
- Utils .sleepUninterrupted (20 * 1000 );
374
- healthCheckContainer = healthCheckCache .get (matchMethodDetectService );
375
- Assert .assertNull (healthCheckContainer );
376
- for (int i = 0 ; i < 3 ; i ++) {
377
- String method = "" ;
378
- if (i > 0 ) {
379
- method = "/test1/path/" + i ;
243
+ Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (3 ));
244
+ }
245
+ else {
246
+ Assert .assertThrows (CallAbortedException .class , () -> integerConsumer .accept (1 ));
380
247
}
381
- FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest (
382
- matchMethodDetectService , method );
383
- FunctionalDecorator decorator = circuitBreakAPI .makeFunctionalDecorator (makeDecoratorRequest );
384
- int finalI = i ;
385
- Consumer <Integer > integerConsumer = decorator .decorateConsumer (num -> {
386
- if (num % 2 == 0 ) {
387
- throw new IllegalArgumentException ("invoke failed" + finalI );
388
- }
389
- else {
390
- System .out .println ("invoke success" + finalI );
391
- }
392
- });
393
- integerConsumer .accept (1 );
394
248
}
395
- healthCheckContainer = healthCheckCache .get (matchMethodDetectService );
396
- Assert .assertNotNull (healthCheckContainer );
397
- healthCheckerValues = healthCheckContainer .getHealthCheckerValues ();
398
- Assert .assertEquals (1 , healthCheckerValues .size ());
399
- // for (ResourceHealthChecker resourceHealthChecker : healthCheckerValues) {
400
- // Assert.assertEquals(2, resourceHealthChecker.getResources().size());
401
- // }
402
249
}
403
250
}
404
251
0 commit comments