Skip to content

Commit 86e544b

Browse files
authored
Stabilize Sentinel integrated rate-limit assertions (#6374)
The HTTP integrated Sentinel tests used back-to-back requests to prove one successful pass and one rate-limited response. On slow CI, sequential requests can cross Sentinel's one-second QPS window, leaving the expected limited response absent even though the gateway behavior is correct. This changes the trigger to concurrent requests and asserts over the observed result set, preserving the same behavior coverage while removing timing dependence from the test. Constraint: Failure appeared in the master push for #6361 in build (shenyu-integrated-test-http), after Sentinel fallback coverage was added. Rejected: Increase sleeps or timeouts | would make the timing window larger without making the assertion deterministic. Rejected: Change Sentinel production cleanup | the failing surface is the test trigger timing, and production cleanup changes would broaden risk unnecessarily. Confidence: medium Scope-risk: narrow Directive: Keep Sentinel rate-limit integration assertions concurrency-based; sequential requests can cross QPS windows on loaded runners. Tested: ./mvnw -f shenyu-integrated-test/pom.xml -pl shenyu-integrated-test-http -am -DskipTests test-compile Tested: git diff --check Not-tested: Full Docker integrated-test-http run locally; required CI images and supporting services were not prebuilt locally.
1 parent 48120c1 commit 86e544b

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

  • shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination

‎shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/SentinelPluginTest.java‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636
import java.io.IOException;
3737
import java.lang.reflect.Type;
3838
import java.util.Collections;
39+
import java.util.HashSet;
3940
import java.util.List;
4041
import java.util.Map;
42+
import java.util.Set;
43+
import java.util.concurrent.ExecutionException;
44+
import java.util.concurrent.Future;
4145

4246
import static org.hamcrest.CoreMatchers.is;
4347
import static org.hamcrest.MatcherAssert.assertThat;
44-
import static org.junit.jupiter.api.Assertions.assertEquals;
4548
import static org.junit.jupiter.api.Assertions.assertNotNull;
4649

4750
public final class SentinelPluginTest extends AbstractPluginDataInit {
@@ -57,34 +60,63 @@ public void setup() throws IOException {
5760
}
5861

5962
@Test
60-
public void test() throws IOException {
63+
public void test() throws IOException, ExecutionException, InterruptedException {
6164
String selectorAndRulesResult =
6265
initSelectorAndRules(PluginEnum.SENTINEL.getName(), "", buildSelectorConditionList(), buildRuleLocalDataList(null));
6366
assertThat(selectorAndRulesResult, is("success"));
6467

6568
Type returnType = new TypeToken<Map<String, Object>>() {
6669
}.getType();
67-
Map<String, Object> result = HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType);
68-
assertNotNull(result);
69-
assertEquals("pass", result.get("msg"));
70-
result = HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType);
71-
assertEquals("You have been restricted, please try again later!", result.get("message"));
70+
Future<Map<String, Object>> first = this.getService().submit(() -> HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType));
71+
Future<Map<String, Object>> second = this.getService().submit(() -> HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType));
72+
Map<String, Object> firstResult = first.get();
73+
Map<String, Object> secondResult = second.get();
74+
Set<String> messages = toMessages(firstResult, secondResult);
75+
assertThat(messages.contains("pass"), is(true));
76+
assertThat(messages.contains("You have been restricted, please try again later!"), is(true));
7277
}
7378

7479
@Test
75-
public void testFallbackUri() throws IOException {
80+
public void testFallbackUri() throws IOException, ExecutionException, InterruptedException {
7681
String selectorAndRulesResult =
7782
initSelectorAndRules(PluginEnum.SENTINEL.getName(), "", buildSelectorConditionList(), buildRuleLocalDataList(TEST_SENTINEL_FALLBACK_PATH));
7883
assertThat(selectorAndRulesResult, is("success"));
7984

8085
Type returnType = new TypeToken<Map<String, Object>>() {
8186
}.getType();
82-
Map<String, Object> result = HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType);
83-
assertNotNull(result);
84-
assertEquals("pass", result.get("msg"));
85-
Map<String, Object> fallbackRet = HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType);
86-
assertEquals(ShenyuResultEnum.SENTINEL_PLUGIN_FALLBACK.getCode(), ((Number) fallbackRet.get("code")).intValue());
87-
assertEquals(ShenyuResultEnum.SENTINEL_PLUGIN_FALLBACK.getMsg(), fallbackRet.get("message"));
87+
Future<Map<String, Object>> first = this.getService().submit(() -> HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType));
88+
Future<Map<String, Object>> second = this.getService().submit(() -> HttpHelper.INSTANCE.postGateway(TEST_SENTINEL_PATH, returnType));
89+
Map<String, Object> firstResult = first.get();
90+
Map<String, Object> secondResult = second.get();
91+
Set<String> messages = toMessages(firstResult, secondResult);
92+
Set<Integer> codes = toCodes(firstResult, secondResult);
93+
assertThat(messages.contains("pass"), is(true));
94+
assertThat(codes.contains(ShenyuResultEnum.SENTINEL_PLUGIN_FALLBACK.getCode()), is(true));
95+
assertThat(messages.contains(ShenyuResultEnum.SENTINEL_PLUGIN_FALLBACK.getMsg()), is(true));
96+
}
97+
98+
private static Set<String> toMessages(final Map<String, Object>... results) {
99+
Set<String> messages = new HashSet<>();
100+
for (Map<String, Object> result : results) {
101+
assertNotNull(result);
102+
Object msg = result.containsKey("msg") ? result.get("msg") : result.get("message");
103+
if (msg instanceof String) {
104+
messages.add((String) msg);
105+
}
106+
}
107+
return messages;
108+
}
109+
110+
private static Set<Integer> toCodes(final Map<String, Object>... results) {
111+
Set<Integer> codes = new HashSet<>();
112+
for (Map<String, Object> result : results) {
113+
assertNotNull(result);
114+
Object code = result.get("code");
115+
if (code instanceof Number) {
116+
codes.add(((Number) code).intValue());
117+
}
118+
}
119+
return codes;
88120
}
89121

90122
private static List<ConditionData> buildSelectorConditionList() {

0 commit comments

Comments
 (0)