-
Notifications
You must be signed in to change notification settings - Fork 188
Fix the problem of flowcontrol plugin interceptor declaration error and failure to remove retried instances #1760
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
Open
hanbingleixue
wants to merge
1
commit into
sermant-io:develop
Choose a base branch
from
hanbingleixue:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...plugin/src/test/java/io/sermant/flowcontrol/retry/client/HttpClient4xInterceptorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * Copyright (C) 2025-2025 Sermant Authors. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.sermant.flowcontrol.retry.client; | ||
|
|
||
|
|
||
| import io.sermant.core.plugin.agent.entity.ExecuteContext; | ||
| import io.sermant.core.plugin.config.PluginConfigManager; | ||
| import io.sermant.core.plugin.service.PluginServiceManager; | ||
| import io.sermant.core.service.ServiceManager; | ||
| import io.sermant.core.service.xds.XdsCoreService; | ||
| import io.sermant.core.service.xds.XdsFlowControlService; | ||
| import io.sermant.core.service.xds.entity.XdsRequestCircuitBreakers; | ||
| import io.sermant.flowcontrol.common.config.FlowControlConfig; | ||
| import io.sermant.flowcontrol.common.entity.FlowControlResponse; | ||
| import io.sermant.flowcontrol.common.entity.FlowControlResult; | ||
| import io.sermant.flowcontrol.common.entity.FlowControlScenario; | ||
| import io.sermant.flowcontrol.common.util.XdsThreadLocalUtil; | ||
| import io.sermant.flowcontrol.common.xds.circuit.XdsCircuitBreakerManager; | ||
| import io.sermant.flowcontrol.inject.ErrorCloseableHttpResponse; | ||
| import io.sermant.flowcontrol.service.rest4j.XdsHttpFlowControlService; | ||
|
|
||
| import org.apache.http.HttpStatus; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.net.URI; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doAnswer; | ||
|
|
||
| /** | ||
| * HttpClient4xIntercepto test | ||
| * | ||
| * @author zhp | ||
| * @since 2025-04-11 | ||
| */ | ||
| public class HttpClient4xInterceptorTest { | ||
| private MockedStatic<PluginConfigManager> pluginConfigManagerMockedStatic; | ||
|
|
||
| private MockedStatic<PluginServiceManager> pluginServiceManagerMockedStatic; | ||
|
|
||
| private MockedStatic<ServiceManager> serviceManagerMockedStatic; | ||
|
|
||
| private XdsHttpFlowControlService xdsHttpFlowControlService; | ||
|
|
||
| private HttpGet httpRequest; | ||
|
|
||
| private Method method = this.getClass().getMethod("getResult", null); | ||
|
|
||
| private XdsFlowControlService xdsFlowControlService; | ||
|
|
||
| public HttpClient4xInterceptorTest() throws NoSuchMethodException { | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| pluginConfigManagerMockedStatic.close(); | ||
| pluginServiceManagerMockedStatic.close(); | ||
| serviceManagerMockedStatic.close(); | ||
| } | ||
|
|
||
| /** | ||
| * preinitialization | ||
| * | ||
| * @throws Exception initialization failure thrown | ||
| */ | ||
| @Before | ||
| public void before() throws Exception { | ||
| pluginConfigManagerMockedStatic = Mockito | ||
| .mockStatic(PluginConfigManager.class); | ||
| pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(FlowControlConfig.class)) | ||
| .thenReturn(new FlowControlConfig()); | ||
| pluginServiceManagerMockedStatic = Mockito.mockStatic(PluginServiceManager.class); | ||
| xdsHttpFlowControlService = Mockito.mock(XdsHttpFlowControlService.class); | ||
| pluginServiceManagerMockedStatic.when(() -> PluginServiceManager.getPluginService(XdsHttpFlowControlService.class)) | ||
| .thenReturn(xdsHttpFlowControlService); | ||
| serviceManagerMockedStatic = Mockito.mockStatic(ServiceManager.class); | ||
| XdsCoreService xdsCoreService = Mockito.mock(XdsCoreService.class); | ||
| serviceManagerMockedStatic.when(() -> ServiceManager.getService(XdsCoreService.class)).thenReturn(xdsCoreService); | ||
| xdsFlowControlService = Mockito.mock(XdsFlowControlService.class); | ||
| Mockito.when(xdsCoreService.getXdsFlowControlService()).thenReturn(xdsFlowControlService); | ||
| method = Mockito.mock(Method.class); | ||
| httpRequest = new HttpGet(); | ||
| } | ||
|
|
||
| @Test | ||
| public void test() throws NoSuchMethodException { | ||
| HttpClient4xInterceptor interceptor = new HttpClient4xInterceptor(); | ||
| // test parameter type is incorrect | ||
| ExecuteContext executeContext = buildErrorContext(); | ||
| interceptor.doBefore(executeContext); | ||
| Assert.assertNull(executeContext.getResult()); | ||
|
|
||
| // test request URL does not meet the requirements | ||
| executeContext = buildContext(); | ||
| String resultMsg = "success"; | ||
| int code = HttpStatus.SC_BAD_REQUEST; | ||
| AtomicBoolean triggeredFlag = new AtomicBoolean(true); | ||
| doAnswer(invocation -> { | ||
| if (triggeredFlag.get()) { | ||
| Object[] args = invocation.getArguments(); | ||
| FlowControlResult flowControlResult = (FlowControlResult) args[1]; | ||
| flowControlResult.setSkip(true); | ||
| flowControlResult.setResponse(new FlowControlResponse(resultMsg, code)); | ||
| } | ||
| return null; | ||
| }).when(xdsHttpFlowControlService).onBefore(any(), any()); | ||
| interceptor.doBefore(executeContext); | ||
| Assert.assertNull(executeContext.getResult()); | ||
|
|
||
| // test triggered flow control rules, abort the request | ||
| httpRequest.setURI(URI.create("http://provider:8080/path")); | ||
| interceptor.doBefore(executeContext); | ||
| Object result = executeContext.getResult(); | ||
| Assert.assertTrue(result instanceof ErrorCloseableHttpResponse); | ||
| ErrorCloseableHttpResponse response = (ErrorCloseableHttpResponse) result; | ||
| Assert.assertEquals(code, response.getStatusLine().getStatusCode()); | ||
| Assert.assertEquals(resultMsg, response.getStatusLine().getReasonPhrase()); | ||
|
|
||
| // test trigger circuit breaker rules | ||
| triggeredFlag.set(false); | ||
| XdsRequestCircuitBreakers xdsRequestCircuitBreakers = new XdsRequestCircuitBreakers(); | ||
| Mockito.when(xdsFlowControlService.getRequestCircuitBreakers(any(), any())) | ||
| .thenReturn(Optional.of(xdsRequestCircuitBreakers)); | ||
| xdsRequestCircuitBreakers.setMaxRequests(1); | ||
| FlowControlScenario scenario = new FlowControlScenario(); | ||
| scenario.setClusterName("test"); | ||
| scenario.setServiceName("provider"); | ||
| XdsCircuitBreakerManager.incrementActiveRequests(scenario.getServiceName(), scenario.getClusterName()); | ||
| XdsThreadLocalUtil.setScenarioInfo(scenario); | ||
| interceptor.doBefore(executeContext); | ||
| result = executeContext.getResult(); | ||
| Assert.assertTrue(result instanceof ErrorCloseableHttpResponse); | ||
| response = (ErrorCloseableHttpResponse) result; | ||
| Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode()); | ||
| Assert.assertEquals("CircuitBreaker has forced open and deny any requests", | ||
| response.getStatusLine().getReasonPhrase()); | ||
| } | ||
|
|
||
| private ExecuteContext buildContext() throws NoSuchMethodException { | ||
| httpRequest.setURI(URI.create("http://127.0.0.1:8080/path")); | ||
| return ExecuteContext.forMemberMethod(new HttpClient4xInterceptorTest(), method, new Object[]{null, httpRequest}, null, null); | ||
| } | ||
|
|
||
| private ExecuteContext buildErrorContext() throws NoSuchMethodException { | ||
| return ExecuteContext.forMemberMethod(new HttpClient4xInterceptorTest(), method, new Object[]{null, ""}, null, null); | ||
| } | ||
|
|
||
| public String getResult(){ | ||
| return "success"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't delete this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done