|
| 1 | +/* |
| 2 | + * Copyright (C) 2025-2025 Sermant Authors. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.sermant.flowcontrol.retry.client; |
| 18 | + |
| 19 | + |
| 20 | +import io.sermant.core.plugin.agent.entity.ExecuteContext; |
| 21 | +import io.sermant.core.plugin.config.PluginConfigManager; |
| 22 | +import io.sermant.core.plugin.service.PluginServiceManager; |
| 23 | +import io.sermant.core.service.ServiceManager; |
| 24 | +import io.sermant.core.service.xds.XdsCoreService; |
| 25 | +import io.sermant.core.service.xds.XdsFlowControlService; |
| 26 | +import io.sermant.core.service.xds.entity.XdsRequestCircuitBreakers; |
| 27 | +import io.sermant.flowcontrol.common.config.FlowControlConfig; |
| 28 | +import io.sermant.flowcontrol.common.entity.FlowControlResponse; |
| 29 | +import io.sermant.flowcontrol.common.entity.FlowControlResult; |
| 30 | +import io.sermant.flowcontrol.common.entity.FlowControlScenario; |
| 31 | +import io.sermant.flowcontrol.common.util.XdsThreadLocalUtil; |
| 32 | +import io.sermant.flowcontrol.common.xds.circuit.XdsCircuitBreakerManager; |
| 33 | +import io.sermant.flowcontrol.inject.ErrorCloseableHttpResponse; |
| 34 | +import io.sermant.flowcontrol.service.rest4j.XdsHttpFlowControlService; |
| 35 | + |
| 36 | +import org.apache.http.HttpStatus; |
| 37 | +import org.apache.http.client.methods.HttpGet; |
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.mockito.MockedStatic; |
| 43 | +import org.mockito.Mockito; |
| 44 | + |
| 45 | +import java.lang.reflect.Method; |
| 46 | +import java.net.URI; |
| 47 | +import java.util.Optional; |
| 48 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 49 | + |
| 50 | +import static org.mockito.ArgumentMatchers.any; |
| 51 | +import static org.mockito.Mockito.doAnswer; |
| 52 | + |
| 53 | +/** |
| 54 | + * HttpClient4xIntercepto test |
| 55 | + * |
| 56 | + * @author zhp |
| 57 | + * @since 2025-04-11 |
| 58 | + */ |
| 59 | +public class HttpClient4xInterceptorTest { |
| 60 | + private MockedStatic<PluginConfigManager> pluginConfigManagerMockedStatic; |
| 61 | + |
| 62 | + private MockedStatic<PluginServiceManager> pluginServiceManagerMockedStatic; |
| 63 | + |
| 64 | + private MockedStatic<ServiceManager> serviceManagerMockedStatic; |
| 65 | + |
| 66 | + private XdsHttpFlowControlService xdsHttpFlowControlService; |
| 67 | + |
| 68 | + private HttpGet httpRequest; |
| 69 | + |
| 70 | + private Method method = this.getClass().getMethod("getResult", null); |
| 71 | + |
| 72 | + private XdsFlowControlService xdsFlowControlService; |
| 73 | + |
| 74 | + public HttpClient4xInterceptorTest() throws NoSuchMethodException { |
| 75 | + } |
| 76 | + |
| 77 | + @After |
| 78 | + public void tearDown() { |
| 79 | + pluginConfigManagerMockedStatic.close(); |
| 80 | + pluginServiceManagerMockedStatic.close(); |
| 81 | + serviceManagerMockedStatic.close(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * preinitialization |
| 86 | + * |
| 87 | + * @throws Exception initialization failure thrown |
| 88 | + */ |
| 89 | + @Before |
| 90 | + public void before() throws Exception { |
| 91 | + pluginConfigManagerMockedStatic = Mockito |
| 92 | + .mockStatic(PluginConfigManager.class); |
| 93 | + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(FlowControlConfig.class)) |
| 94 | + .thenReturn(new FlowControlConfig()); |
| 95 | + pluginServiceManagerMockedStatic = Mockito.mockStatic(PluginServiceManager.class); |
| 96 | + xdsHttpFlowControlService = Mockito.mock(XdsHttpFlowControlService.class); |
| 97 | + pluginServiceManagerMockedStatic.when(() -> PluginServiceManager.getPluginService(XdsHttpFlowControlService.class)) |
| 98 | + .thenReturn(xdsHttpFlowControlService); |
| 99 | + serviceManagerMockedStatic = Mockito.mockStatic(ServiceManager.class); |
| 100 | + XdsCoreService xdsCoreService = Mockito.mock(XdsCoreService.class); |
| 101 | + serviceManagerMockedStatic.when(() -> ServiceManager.getService(XdsCoreService.class)).thenReturn(xdsCoreService); |
| 102 | + xdsFlowControlService = Mockito.mock(XdsFlowControlService.class); |
| 103 | + Mockito.when(xdsCoreService.getXdsFlowControlService()).thenReturn(xdsFlowControlService); |
| 104 | + method = Mockito.mock(Method.class); |
| 105 | + httpRequest = new HttpGet(); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void test() throws NoSuchMethodException { |
| 110 | + HttpClient4xInterceptor interceptor = new HttpClient4xInterceptor(); |
| 111 | + // test parameter type is incorrect |
| 112 | + ExecuteContext executeContext = buildErrorContext(); |
| 113 | + interceptor.doBefore(executeContext); |
| 114 | + Assert.assertNull(executeContext.getResult()); |
| 115 | + |
| 116 | + // test request URL does not meet the requirements |
| 117 | + executeContext = buildContext(); |
| 118 | + String resultMsg = "success"; |
| 119 | + int code = HttpStatus.SC_BAD_REQUEST; |
| 120 | + AtomicBoolean triggeredFlag = new AtomicBoolean(true); |
| 121 | + doAnswer(invocation -> { |
| 122 | + if (triggeredFlag.get()) { |
| 123 | + Object[] args = invocation.getArguments(); |
| 124 | + FlowControlResult flowControlResult = (FlowControlResult) args[1]; |
| 125 | + flowControlResult.setSkip(true); |
| 126 | + flowControlResult.setResponse(new FlowControlResponse(resultMsg, code)); |
| 127 | + } |
| 128 | + return null; |
| 129 | + }).when(xdsHttpFlowControlService).onBefore(any(), any()); |
| 130 | + interceptor.doBefore(executeContext); |
| 131 | + Assert.assertNull(executeContext.getResult()); |
| 132 | + |
| 133 | + // test triggered flow control rules, abort the request |
| 134 | + httpRequest.setURI(URI.create("http://provider:8080/path")); |
| 135 | + interceptor.doBefore(executeContext); |
| 136 | + Object result = executeContext.getResult(); |
| 137 | + Assert.assertTrue(result instanceof ErrorCloseableHttpResponse); |
| 138 | + ErrorCloseableHttpResponse response = (ErrorCloseableHttpResponse) result; |
| 139 | + Assert.assertEquals(code, response.getStatusLine().getStatusCode()); |
| 140 | + Assert.assertEquals(resultMsg, response.getStatusLine().getReasonPhrase()); |
| 141 | + |
| 142 | + // test trigger circuit breaker rules |
| 143 | + triggeredFlag.set(false); |
| 144 | + XdsRequestCircuitBreakers xdsRequestCircuitBreakers = new XdsRequestCircuitBreakers(); |
| 145 | + Mockito.when(xdsFlowControlService.getRequestCircuitBreakers(any(), any())) |
| 146 | + .thenReturn(Optional.of(xdsRequestCircuitBreakers)); |
| 147 | + xdsRequestCircuitBreakers.setMaxRequests(1); |
| 148 | + FlowControlScenario scenario = new FlowControlScenario(); |
| 149 | + scenario.setClusterName("test"); |
| 150 | + scenario.setServiceName("provider"); |
| 151 | + XdsCircuitBreakerManager.incrementActiveRequests(scenario.getServiceName(), scenario.getClusterName()); |
| 152 | + XdsThreadLocalUtil.setScenarioInfo(scenario); |
| 153 | + interceptor.doBefore(executeContext); |
| 154 | + result = executeContext.getResult(); |
| 155 | + Assert.assertTrue(result instanceof ErrorCloseableHttpResponse); |
| 156 | + response = (ErrorCloseableHttpResponse) result; |
| 157 | + Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode()); |
| 158 | + Assert.assertEquals("CircuitBreaker has forced open and deny any requests", |
| 159 | + response.getStatusLine().getReasonPhrase()); |
| 160 | + } |
| 161 | + |
| 162 | + private ExecuteContext buildContext() throws NoSuchMethodException { |
| 163 | + httpRequest.setURI(URI.create("http://127.0.0.1:8080/path")); |
| 164 | + return ExecuteContext.forMemberMethod(new HttpClient4xInterceptorTest(), method, new Object[]{null, httpRequest}, null, null); |
| 165 | + } |
| 166 | + |
| 167 | + private ExecuteContext buildErrorContext() throws NoSuchMethodException { |
| 168 | + return ExecuteContext.forMemberMethod(new HttpClient4xInterceptorTest(), method, new Object[]{null, ""}, null, null); |
| 169 | + } |
| 170 | + |
| 171 | + public String getResult(){ |
| 172 | + return "success"; |
| 173 | + } |
| 174 | +} |
0 commit comments