Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reactive sentinel circuit breaker tests #3054

Merged
merged 2 commits into from
Jan 6, 2023

Conversation

ruansheng8
Copy link
Collaborator

@ruansheng8 ruansheng8 commented Jan 6, 2023

Describe what this PR does / why we need it

修复 Sentinel reactive 单元测试偶发不通过的情况 , 具体异常见:
https://github.com/alibaba/spring-cloud-alibaba/actions/runs/3845399772/jobs/6549501288

单元测试调用slow() 接口 , 每次调用延迟80ms

		@GetMapping("/slow")
		public Mono<String> slow() {
			return Mono.just("slow").delayElement(Duration.ofMillis(80));
		}

统计规则:

				factory.configure(
						builder -> builder.rules(Collections
								.singletonList(new DegradeRule("slow_mono").setCount(50)
										.setSlowRatioThreshold(0.7).setMinRequestAmount(5)
										.setStatIntervalMs(30000).setTimeWindow(5))),
						"slow_mono");

Unit test code:

	@Test
	public void test() throws Exception {
		StepVerifier.create(service.normal()).expectNext("normal").verifyComplete();
                // 开始调用slow接口 , 每次调用消耗80ms+程序自身执行损耗
		StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
		StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
		StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
		StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
		StepVerifier.create(service.slow()).expectNext("slow").verifyComplete();
                // 5次损耗: `5 * (80 + 程序自身损耗) > 400ms`
		// 这里循环5次调用 , 前4次耗时 `5 * (80 + 程序自身损耗) > 400ms`
		for (int i = 0; i < 5; i++) {
                        // 第5次调用时 , 上面所有耗时为 `400ms + 400ms + 4000ms + (程序损耗) > 4800ms`
                        // 当程序损耗 > 200ms 时 , 由于熔断时间窗口为5s , 所以重新计算 , 此时接口返回`slow` ,不符合预期的`fallback`
			StepVerifier.create(service.slow()).expectNext("fallback").verifyComplete();
			Thread.sleep(1000);
		}

修复方案:
减少循环时的等待时间 (1000ms -> 900ms) , 循环结束后等待500ms (补偿前面循环的等待时间 , 因为后面的单元测试需要在新的时间窗口中进行统计)

Copy link
Collaborator

@steverao steverao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @ruansheng8 Please create a same PR to other main branch.

@steverao steverao merged commit 7a49103 into alibaba:2022.x Jan 6, 2023
@steverao steverao added the waiting for sync waiting for sync to other main branchs label Jan 6, 2023
ruansheng8 added a commit to ruansheng8/spring-cloud-alibaba that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
ruansheng8 added a commit to ruansheng8/spring-cloud-alibaba that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
steverao pushed a commit that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
steverao pushed a commit that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
ruansheng8 added a commit to ruansheng8/spring-cloud-alibaba that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
steverao pushed a commit that referenced this pull request Jan 6, 2023
* Fix reactive sentinel circuit breaker tests
ruansheng8 added a commit to ruansheng8/spring-cloud-alibaba that referenced this pull request Jan 30, 2023
* Fix reactive sentinel circuit breaker tests
@steverao steverao removed the waiting for sync waiting for sync to other main branchs label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants