Fix test failure in sentinel-grpc-adapter
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
2682bd1aae
commit
e71d036794
|
|
@ -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;
|
package com.alibaba.csp.sentinel.adapter.grpc;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.EntryType;
|
import com.alibaba.csp.sentinel.EntryType;
|
||||||
|
|
@ -9,6 +24,7 @@ import com.alibaba.csp.sentinel.adapter.grpc.gen.FooRequest;
|
||||||
import com.alibaba.csp.sentinel.adapter.grpc.gen.FooResponse;
|
import com.alibaba.csp.sentinel.adapter.grpc.gen.FooResponse;
|
||||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
|
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
|
||||||
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -20,19 +36,13 @@ import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhengzechao
|
* @author zhengzechao
|
||||||
* @date 2018/12/7
|
|
||||||
* Email ooczzoo@gmail.com
|
|
||||||
*/
|
*/
|
||||||
public class SentinelGrpcClientInterceptorDegradeTest {
|
public class SentinelGrpcClientInterceptorDegradeTest {
|
||||||
|
|
||||||
private final String resourceName = "com.alibaba.sentinel.examples.FooService/helloWithEx";
|
private final String resourceName = "com.alibaba.sentinel.examples.FooService/helloWithEx";
|
||||||
private final int threshold = 2;
|
|
||||||
private final GrpcTestServer server = new GrpcTestServer();
|
private final GrpcTestServer server = new GrpcTestServer();
|
||||||
private final int timeWindow = 10;
|
private final int timeWindow = 10;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void configureDegradeRule(int count) {
|
private void configureDegradeRule(int count) {
|
||||||
DegradeRule rule = new DegradeRule()
|
DegradeRule rule = new DegradeRule()
|
||||||
.setCount(count)
|
.setCount(count)
|
||||||
|
|
@ -42,11 +52,8 @@ public class SentinelGrpcClientInterceptorDegradeTest {
|
||||||
.as(DegradeRule.class)
|
.as(DegradeRule.class)
|
||||||
.setTimeWindow(timeWindow);
|
.setTimeWindow(timeWindow);
|
||||||
DegradeRuleManager.loadRules(Collections.singletonList(rule));
|
DegradeRuleManager.loadRules(Collections.singletonList(rule));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean sendRequest(FooServiceClient client) {
|
private boolean sendRequest(FooServiceClient client) {
|
||||||
try {
|
try {
|
||||||
FooResponse response = client.helloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(666).build());
|
FooResponse response = client.helloWithEx(FooRequest.newBuilder().setName("Sentinel").setId(666).build());
|
||||||
|
|
@ -58,7 +65,6 @@ public class SentinelGrpcClientInterceptorDegradeTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGrpcClientInterceptor_degrade() throws IOException {
|
public void testGrpcClientInterceptor_degrade() throws IOException {
|
||||||
final int port = 19316;
|
final int port = 19316;
|
||||||
|
|
@ -71,7 +77,7 @@ public class SentinelGrpcClientInterceptorDegradeTest {
|
||||||
assertFalse(sendErrorRequest(client));
|
assertFalse(sendErrorRequest(client));
|
||||||
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.OUT);
|
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.OUT);
|
||||||
assertNotNull(clusterNode);
|
assertNotNull(clusterNode);
|
||||||
assertEquals(1, clusterNode.exceptionQps());
|
assertEquals(1, clusterNode.exceptionQps(), 0.01);
|
||||||
// The second request will be blocked.
|
// The second request will be blocked.
|
||||||
assertFalse(sendRequest(client));
|
assertFalse(sendRequest(client));
|
||||||
assertEquals(1, clusterNode.blockRequest());
|
assertEquals(1, clusterNode.blockRequest());
|
||||||
|
|
|
||||||
|
|
@ -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;
|
package com.alibaba.csp.sentinel.adapter.grpc;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.EntryType;
|
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.degrade.DegradeRuleManager;
|
||||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
|
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
|
||||||
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -21,19 +37,14 @@ import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhengzechao
|
* @author zhengzechao
|
||||||
* @date 2018/12/7
|
|
||||||
* Email ooczzoo@gmail.com
|
|
||||||
*/
|
*/
|
||||||
public class SentinelGrpcServerInterceptorDegradeTest {
|
public class SentinelGrpcServerInterceptorDegradeTest {
|
||||||
|
|
||||||
private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHelloWithEx";
|
private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHelloWithEx";
|
||||||
private final int threshold = 4;
|
|
||||||
private final GrpcTestServer server = new GrpcTestServer();
|
private final GrpcTestServer server = new GrpcTestServer();
|
||||||
private final int timeWindow = 10;
|
private final int timeWindow = 10;
|
||||||
private FooServiceClient client;
|
private FooServiceClient client;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void configureDegradeRule(int count) {
|
private void configureDegradeRule(int count) {
|
||||||
DegradeRule rule = new DegradeRule()
|
DegradeRule rule = new DegradeRule()
|
||||||
.setCount(count)
|
.setCount(count)
|
||||||
|
|
@ -43,14 +54,12 @@ public class SentinelGrpcServerInterceptorDegradeTest {
|
||||||
.as(DegradeRule.class)
|
.as(DegradeRule.class)
|
||||||
.setTimeWindow(timeWindow);
|
.setTimeWindow(timeWindow);
|
||||||
DegradeRuleManager.loadRules(Collections.singletonList(rule));
|
DegradeRuleManager.loadRules(Collections.singletonList(rule));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean sendRequest() {
|
private boolean sendRequest() {
|
||||||
try {
|
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);
|
System.out.println("Response: " + response);
|
||||||
return true;
|
return true;
|
||||||
} catch (StatusRuntimeException ex) {
|
} catch (StatusRuntimeException ex) {
|
||||||
|
|
@ -59,10 +68,6 @@ public class SentinelGrpcServerInterceptorDegradeTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGrpcServerInterceptor_degrade_fail_threads() throws IOException, InterruptedException {
|
public void testGrpcServerInterceptor_degrade_fail_threads() throws IOException, InterruptedException {
|
||||||
final int port = 19349;
|
final int port = 19349;
|
||||||
|
|
@ -91,7 +96,8 @@ public class SentinelGrpcServerInterceptorDegradeTest {
|
||||||
|
|
||||||
private boolean sendErrorRequest() {
|
private boolean sendErrorRequest() {
|
||||||
try {
|
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);
|
System.out.println("Response: " + response);
|
||||||
return true;
|
return true;
|
||||||
} catch (StatusRuntimeException ex) {
|
} catch (StatusRuntimeException ex) {
|
||||||
|
|
@ -100,7 +106,6 @@ public class SentinelGrpcServerInterceptorDegradeTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
FlowRuleManager.loadRules(null);
|
FlowRuleManager.loadRules(null);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue