|
| 1 | +/* |
| 2 | + * Copyright 1999-2022 Alibaba Group Holding Ltd. |
| 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 | + * https://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 | +package com.alibaba.csp.sentinel.transport.command; |
| 17 | + |
| 18 | +import com.alibaba.csp.sentinel.command.CommandHandler; |
| 19 | +import com.alibaba.csp.sentinel.command.CommandHandlerProvider; |
| 20 | +import com.alibaba.csp.sentinel.command.CommandRequest; |
| 21 | +import com.alibaba.csp.sentinel.command.CommandResponse; |
| 22 | +import com.alibaba.csp.sentinel.command.handler.InterceptingCommandHandler; |
| 23 | +import com.alibaba.csp.sentinel.slots.block.RuleConstant; |
| 24 | +import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; |
| 25 | +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; |
| 26 | +import com.alibaba.csp.sentinel.transport.util.HttpCommandUtils; |
| 27 | +import com.alibaba.csp.sentinel.util.AssertUtil; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author icodening |
| 36 | + * @date 2022.03.23 |
| 37 | + */ |
| 38 | +@SuppressWarnings("all") |
| 39 | +public class InterceptingCommandHandlerTest { |
| 40 | + |
| 41 | + private Map<String, CommandHandler> commandHandlerMap = CommandHandlerProvider.getInstance().namedHandlers(); |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() throws Exception { |
| 45 | + FlowRule flowRule = new FlowRule(); |
| 46 | + flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS) |
| 47 | + .setClusterMode(false) |
| 48 | + .setCount(1) |
| 49 | + .setResource("/test"); |
| 50 | + FlowRuleManager.loadRules(Collections.singletonList(flowRule)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testInterceptCommand() { |
| 55 | + CommandHandler getRulesHandler = commandHandlerMap.get("getRules"); |
| 56 | + AssertUtil.assertState(getRulesHandler instanceof InterceptingCommandHandler, "getRulesHandler should be an InterceptingCommandHandler"); |
| 57 | + |
| 58 | + CommandHandler basicInfoHandler = commandHandlerMap.get("basicInfo"); |
| 59 | + AssertUtil.assertState(!(basicInfoHandler instanceof InterceptingCommandHandler), "basicInfoHandler should not be an InterceptingCommandHandler"); |
| 60 | + |
| 61 | + CommandRequest getRulesCommandRequest = new CommandRequest(); |
| 62 | + getRulesCommandRequest.addMetadata(HttpCommandUtils.REQUEST_TARGET, "getRules"); |
| 63 | + getRulesCommandRequest.addParam("type", "flow"); |
| 64 | + CommandResponse getRulesResponse = getRulesHandler.handle(getRulesCommandRequest); |
| 65 | + System.out.println(getRulesResponse.getResult()); |
| 66 | + } |
| 67 | +} |
0 commit comments