Skip to content

Commit a05e6f2

Browse files
committed
test(plugin): query match cache with current generation
1 parent c1b297f commit a05e6f2

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/NegativeMatchCacheEligibilityTest.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void dynamicSelectorMissMustNotBeCached(final String paramType) {
8989
BaseDataCache.getInstance().cacheSelectData(dynamic);
9090

9191
execute(false);
92-
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, "/x"));
92+
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, selectorCacheKey()));
9393
assertNull(plugin.selector);
9494

9595
execute(true);
@@ -105,7 +105,7 @@ void dynamicRuleMissMustNotBeCached(final String paramType) {
105105
BaseDataCache.getInstance().cacheRuleData(dynamic);
106106

107107
execute(false);
108-
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x"));
108+
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey("selector")));
109109
assertNull(plugin.rule);
110110

111111
execute(true);
@@ -126,7 +126,9 @@ void ruleMissMustNotBeCachedAcrossDynamicSelectors() {
126126
BaseDataCache.getInstance().cacheRuleData(secondRule);
127127

128128
execute(true);
129-
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x"));
129+
RuleData firstRuleMiss = MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey("first"));
130+
assertNotNull(firstRuleMiss);
131+
assertNull(firstRuleMiss.getId());
130132

131133
execute(false);
132134
assertSame(secondRule, plugin.rule);
@@ -145,7 +147,7 @@ void timedSelectorMissMustNotBeCached(final String operator) {
145147

146148
clock.set(predicateFalse(operator, boundary));
147149
execute(false);
148-
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, "/x"));
150+
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, selectorCacheKey()));
149151
assertNull(plugin.selector);
150152

151153
clock.set(predicateTrue(operator, boundary));
@@ -169,7 +171,7 @@ void timedRuleMissMustNotBeCached(final String operator) {
169171

170172
clock.set(predicateFalse(operator, boundary));
171173
execute(false);
172-
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x"));
174+
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey("selector")));
173175
assertNull(plugin.rule);
174176

175177
clock.set(predicateTrue(operator, boundary));
@@ -184,7 +186,7 @@ void pathOnlySelectorMissStillCaches() {
184186

185187
execute(false);
186188

187-
SelectorData cached = MatchDataCache.getInstance().obtainSelectorData(PLUGIN, "/x");
189+
SelectorData cached = MatchDataCache.getInstance().obtainSelectorData(PLUGIN, selectorCacheKey());
188190
assertNotNull(cached);
189191
assertNull(cached.getId());
190192
}
@@ -196,7 +198,7 @@ void pathOnlyRuleMissStillCaches() {
196198

197199
execute(false);
198200

199-
RuleData cached = MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x");
201+
RuleData cached = MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey("selector"));
200202
assertNotNull(cached);
201203
assertNull(cached.getId());
202204
}
@@ -209,6 +211,14 @@ private LocalDateTime predicateTrue(final String operator, final LocalDateTime b
209211
return "TimeAfter".equals(operator) ? boundary.plusSeconds(1) : boundary.minusSeconds(1);
210212
}
211213

214+
private String selectorCacheKey() {
215+
return BaseDataCache.getInstance().getVersion() + ":/x";
216+
}
217+
218+
private String ruleCacheKey(final String selectorId) {
219+
return selectorId.length() + ":" + selectorId + selectorCacheKey();
220+
}
221+
212222
private void execute(final boolean dynamicPresent) {
213223
plugin.execute(MockServerWebExchange.from(MockServerHttpRequest.method(dynamicPresent ? HttpMethod.POST : HttpMethod.GET,
214224
dynamicPresent ? "/x?tenant=a" : "/x?tenant=b").header("X-Tenant", dynamicPresent ? "a" : "b")), exchange -> Mono.empty()).block();

‎shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/PositiveMatchCacheTest.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void genericSelectorMustNotShadowDynamicCompetitor(final String paramType) {
9393
assertSame(generic, plugin.selector);
9494
execute(true);
9595
assertSame(special, plugin.selector);
96-
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, "/x"));
96+
assertNull(MatchDataCache.getInstance().obtainSelectorData(PLUGIN, selectorCacheKey()));
9797
MatchDataCache.getInstance().cleanSelectorData();
9898
execute(true);
9999
assertSame(special, plugin.selector);
@@ -115,7 +115,7 @@ void genericRuleMustNotShadowDynamicCompetitor(final String paramType) {
115115
assertSame(generic, plugin.rule);
116116
execute(true);
117117
assertSame(special, plugin.rule);
118-
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x"));
118+
assertNull(MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey("selector")));
119119
MatchDataCache.getInstance().cleanRuleDataData();
120120
execute(true);
121121
assertSame(special, plugin.rule);
@@ -145,8 +145,8 @@ void pathOnlyCandidatesStillCacheWithDisabledDynamicCompetitors(final boolean in
145145
BaseDataCache.getInstance().cacheRuleData(disabledRule);
146146
}
147147
execute(false);
148-
assertSame(generic, MatchDataCache.getInstance().obtainSelectorData(PLUGIN, "/x"));
149-
assertSame(genericRule, MatchDataCache.getInstance().obtainRuleData(PLUGIN, "/x"));
148+
assertSame(generic, MatchDataCache.getInstance().obtainSelectorData(PLUGIN, selectorCacheKey()));
149+
assertSame(genericRule, MatchDataCache.getInstance().obtainRuleData(PLUGIN, ruleCacheKey(generic.getId())));
150150
execute(true);
151151
assertSame(generic, plugin.selector);
152152
assertSame(genericRule, plugin.rule);
@@ -224,6 +224,14 @@ private RuleData rule(final String id, final List<ConditionData> conditions) {
224224
.matchRestful(false).matchMode(0).sort(0).conditionDataList(conditions).build();
225225
}
226226

227+
private String selectorCacheKey() {
228+
return BaseDataCache.getInstance().getVersion() + ":/x";
229+
}
230+
231+
private String ruleCacheKey(final String selectorId) {
232+
return selectorId.length() + ":" + selectorId + selectorCacheKey();
233+
}
234+
227235
private ConditionData uriCondition() {
228236
return condition("uri", "/", "/x");
229237
}

0 commit comments

Comments
 (0)