|
24 | 24 | import org.apache.shenyu.common.dto.RuleData; |
25 | 25 | import org.apache.shenyu.common.dto.SelectorData; |
26 | 26 | import org.apache.shenyu.common.enums.SelectorTypeEnum; |
27 | | -import org.apache.shenyu.plugin.api.ShenyuPlugin; |
28 | 27 | import org.apache.shenyu.plugin.api.ShenyuPluginChain; |
29 | 28 | import org.apache.shenyu.plugin.api.context.ShenyuContext; |
30 | 29 | import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; |
|
41 | 40 | import reactor.core.publisher.Mono; |
42 | 41 | import reactor.test.StepVerifier; |
43 | 42 |
|
44 | | -import java.lang.reflect.Field; |
45 | | -import java.lang.reflect.Method; |
46 | | -import java.lang.reflect.Modifier; |
47 | 43 | import java.util.ArrayList; |
48 | 44 | import java.util.Collections; |
49 | 45 | import java.util.List; |
50 | | -import java.util.Objects; |
51 | 46 |
|
52 | 47 | import static org.mockito.ArgumentMatchers.any; |
53 | 48 | import static org.junit.jupiter.api.Assertions.assertEquals; |
@@ -260,7 +255,29 @@ public void fullFlowWithoutEnabledRuleReportsNoRule() { |
260 | 255 | .conditionDataList(Collections.singletonList(conditionData)).build()); |
261 | 256 | StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
262 | 257 | assertEquals(1, events.size()); |
263 | | - assertEquals(SelectorRuleMatchObservation.Decision.NO_RULE, events.get(0).getDecision()); |
| 258 | + assertEquals(SelectorRuleMatchObservation.Decision.FULL_FLOW_NO_ENABLED_RULE, events.get(0).getDecision()); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void negativeRuleCacheHitReportsNoRuleFromCache() { |
| 263 | + mockShenyuConfig(true); |
| 264 | + BaseDataCache.getInstance().cachePluginData(pluginData); |
| 265 | + BaseDataCache.getInstance().cacheSelectData(selectorData); |
| 266 | + // the rule list carries only non-matching, path-only, uniquely decidable rules, |
| 267 | + // so the first request evaluates live and warms the negative rule entry |
| 268 | + BaseDataCache.getInstance().cacheRuleData(RuleData.builder().id("r2").pluginName("SHENYU").selectorId("s1") |
| 269 | + .enabled(true).logged(true).matchRestful(false).matchMode(0).sort(1) |
| 270 | + .conditionDataList(Collections.singletonList(unmatchedCondition())).build()); |
| 271 | + StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
| 272 | + assertEquals(1, events.size()); |
| 273 | + assertEquals(SelectorRuleMatchObservation.Decision.RULE_UNMATCHED, events.get(0).getDecision()); |
| 274 | + events.clear(); |
| 275 | + // the second request is served by the negative rule cache entry |
| 276 | + StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
| 277 | + assertEquals(1, events.size()); |
| 278 | + SelectorRuleMatchObservation observation = events.get(0); |
| 279 | + assertEquals(SelectorRuleMatchObservation.Decision.NO_RULE, observation.getDecision()); |
| 280 | + assertEquals(SelectorRuleMatchObservation.CacheResolution.NEGATIVE_HIT, observation.getRuleCache()); |
264 | 281 | } |
265 | 282 |
|
266 | 283 | @Test |
@@ -300,49 +317,28 @@ public void throwingObserverDoesNotAffectRequest() { |
300 | 317 | } |
301 | 318 |
|
302 | 319 | @Test |
303 | | - public void unregisteredObserverProducesNoEventsAndNoFailure() { |
| 320 | + public void registrationTokenUnregistersOnlyItsOwnObserver() throws Exception { |
304 | 321 | seedSuccessfulConfig(); |
305 | | - SelectorRuleMatchObserverHolder.unregister(); |
306 | | - assertFalse(SelectorRuleMatchObserverHolder.isInstrumented()); |
| 322 | + AutoCloseable token = SelectorRuleMatchObserverHolder.register(events::add); |
| 323 | + SelectorRuleMatchObserver later = observation -> { }; |
| 324 | + final AutoCloseable laterToken = SelectorRuleMatchObserverHolder.register(later); |
| 325 | + token.close(); |
| 326 | + // the later registration must survive closing an earlier token |
307 | 327 | StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
308 | 328 | verify(testShenyuPlugin).doExecute(exchange, shenyuPluginChain, selectorData, ruleData); |
309 | | - assertTrue(events.isEmpty()); |
310 | | - } |
311 | | - |
312 | | - @Test |
313 | | - public void outerSkipProducesNoEventBecauseExecuteIsNeverReached() { |
314 | | - seedSuccessfulConfig(); |
315 | | - // emulate the chain: skip is checked before execute and a skipped plugin never matches |
316 | | - SkippingPlugin skippingPlugin = new SkippingPlugin(); |
317 | | - if (!skippingPlugin.skip(exchange)) { |
318 | | - skippingPlugin.execute(exchange, shenyuPluginChain).subscribe(); |
319 | | - } |
320 | | - assertTrue(events.isEmpty(), "a skipped plugin must not fabricate a matching event"); |
321 | | - } |
322 | | - |
323 | | - @Test |
324 | | - public void pluginsOutsideAbstractShenyuPluginProduceNoEvents() throws Exception { |
325 | | - seedSuccessfulConfig(); |
326 | | - // a direct ShenyuPlugin implementation has no selector/rule decision to report |
327 | | - ShenyuPlugin plainPlugin = new PlainPlugin(); |
328 | | - StepVerifier.create(plainPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
329 | | - assertTrue(events.isEmpty(), "unsupported plugins must not get fabricated events"); |
| 329 | + assertTrue(events.isEmpty(), "the later observer discards, but stays registered"); |
| 330 | + laterToken.close(); |
| 331 | + StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
| 332 | + verify(testShenyuPlugin, org.mockito.Mockito.times(2)).doExecute(exchange, shenyuPluginChain, selectorData, ruleData); |
330 | 333 | } |
331 | 334 |
|
332 | 335 | @Test |
333 | | - public void observationIsImmutableAndFreeOfRequestData() { |
| 336 | + public void unregisteredObserverProducesNoEventsAndNoFailure() { |
334 | 337 | seedSuccessfulConfig(); |
| 338 | + SelectorRuleMatchObserverHolder.unregister(); |
335 | 339 | StepVerifier.create(testShenyuPlugin.execute(exchange, shenyuPluginChain)).verifyComplete(); |
336 | | - for (Method method : SelectorRuleMatchObservation.class.getDeclaredMethods()) { |
337 | | - assertFalse(method.getName().startsWith("set"), "observations must expose no setters"); |
338 | | - } |
339 | | - for (Field field : SelectorRuleMatchObservation.class.getDeclaredFields()) { |
340 | | - assertTrue(Modifier.isFinal(field.getModifiers()), field.getName() + " must be final"); |
341 | | - } |
342 | | - SelectorRuleMatchObservation observation = events.get(0); |
343 | | - // no field can carry the request path: the record only holds ids and enums |
344 | | - assertFalse(Objects.nonNull(observation.getSelectorId()) && observation.getSelectorId().contains("SHENYU/SHENYU")); |
345 | | - assertEquals("SHENYU", observation.getPluginName()); |
| 340 | + verify(testShenyuPlugin).doExecute(exchange, shenyuPluginChain, selectorData, ruleData); |
| 341 | + assertTrue(events.isEmpty()); |
346 | 342 | } |
347 | 343 |
|
348 | 344 | private void seedSuccessfulConfig() { |
@@ -383,48 +379,6 @@ private void mockShenyuConfig(final boolean cacheEnabled) { |
383 | 379 | SpringBeanUtils.getInstance().setApplicationContext(context); |
384 | 380 | } |
385 | 381 |
|
386 | | - static class SkippingPlugin extends AbstractShenyuPlugin { |
387 | | - |
388 | | - @Override |
389 | | - public boolean skip(final ServerWebExchange serverExchange) { |
390 | | - return true; |
391 | | - } |
392 | | - |
393 | | - @Override |
394 | | - protected Mono<Void> doExecute(final ServerWebExchange serverExchange, final ShenyuPluginChain pluginChain, |
395 | | - final SelectorData selector, final RuleData rule) { |
396 | | - return Mono.empty(); |
397 | | - } |
398 | | - |
399 | | - @Override |
400 | | - public int getOrder() { |
401 | | - return 0; |
402 | | - } |
403 | | - |
404 | | - @Override |
405 | | - public String named() { |
406 | | - return "SHENYU"; |
407 | | - } |
408 | | - } |
409 | | - |
410 | | - static class PlainPlugin implements ShenyuPlugin { |
411 | | - |
412 | | - @Override |
413 | | - public Mono<Void> execute(final ServerWebExchange serverExchange, final ShenyuPluginChain pluginChain) { |
414 | | - return Mono.empty(); |
415 | | - } |
416 | | - |
417 | | - @Override |
418 | | - public int getOrder() { |
419 | | - return 0; |
420 | | - } |
421 | | - |
422 | | - @Override |
423 | | - public String named() { |
424 | | - return "PLAIN"; |
425 | | - } |
426 | | - } |
427 | | - |
428 | 382 | static class TestShenyuPlugin extends AbstractShenyuPlugin { |
429 | 383 |
|
430 | 384 | @Override |
|
0 commit comments