|
|
@@ -1,3 +1,18 @@ |
|
|
|
/* |
|
|
|
* Copyright 1999-2018 Alibaba Group Holding Ltd. |
|
|
|
* |
|
|
|
* 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 com.alibaba.csp.sentinel.adapter.grpc; |
|
|
|
|
|
|
|
import com.alibaba.csp.sentinel.EntryType; |
|
|
@@ -9,6 +24,7 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; |
|
|
|
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; |
|
|
|
|
|
|
|
import io.grpc.StatusRuntimeException; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Test; |
|
|
@@ -21,36 +37,29 @@ import static org.junit.Assert.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author zhengzechao |
|
|
|
* @date 2018/12/7 |
|
|
|
* Email ooczzoo@gmail.com |
|
|
|
*/ |
|
|
|
public class SentinelGrpcServerInterceptorDegradeTest { |
|
|
|
|
|
|
|
private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHelloWithEx"; |
|
|
|
private final int threshold = 4; |
|
|
|
private final GrpcTestServer server = new GrpcTestServer(); |
|
|
|
private final int timeWindow = 10; |
|
|
|
private FooServiceClient client; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configureDegradeRule(int count) { |
|
|
|
DegradeRule rule = new DegradeRule() |
|
|
|
.setCount(count) |
|
|
|
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) |
|
|
|
.setResource(resourceName) |
|
|
|
.setLimitApp("default") |
|
|
|
.as(DegradeRule.class) |
|
|
|
.setTimeWindow(timeWindow); |
|
|
|
.setCount(count) |
|
|
|
.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) |
|
|
|
.setResource(resourceName) |
|
|
|
.setLimitApp("default") |
|
|
|
.as(DegradeRule.class) |
|
|
|
.setTimeWindow(timeWindow); |
|
|
|
DegradeRuleManager.loadRules(Collections.singletonList(rule)); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean sendRequest() { |
|
|
|
try { |
|
|
|
FooResponse response = client.anotherHelloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(666).build()); |
|
|
|
FooResponse response = client.anotherHelloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(666) |
|
|
|
.build()); |
|
|
|
System.out.println("Response: " + response); |
|
|
|
return true; |
|
|
|
} catch (StatusRuntimeException ex) { |
|
|
@@ -59,10 +68,6 @@ public class SentinelGrpcServerInterceptorDegradeTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testGrpcServerInterceptor_degrade_fail_threads() throws IOException, InterruptedException { |
|
|
|
final int port = 19349; |
|
|
@@ -91,7 +96,8 @@ public class SentinelGrpcServerInterceptorDegradeTest { |
|
|
|
|
|
|
|
private boolean sendErrorRequest() { |
|
|
|
try { |
|
|
|
FooResponse response = client.anotherHelloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(-1).build()); |
|
|
|
FooResponse response = client.anotherHelloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(-1) |
|
|
|
.build()); |
|
|
|
System.out.println("Response: " + response); |
|
|
|
return true; |
|
|
|
} catch (StatusRuntimeException ex) { |
|
|
@@ -100,7 +106,6 @@ public class SentinelGrpcServerInterceptorDegradeTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
public void cleanUp() { |
|
|
|
FlowRuleManager.loadRules(null); |
|
|
|