Skip to content

Commit

Permalink
Update test cases for Sentinel web filter and gRPC adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Nov 8, 2018
1 parent 3de817b commit 695fa9d
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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 io.grpc.Server;
Expand All @@ -6,18 +21,18 @@
import java.io.IOException;

class GrpcTestServer {

private Server server;

GrpcTestServer() {
}
GrpcTestServer() {}

void start(int port, boolean shouldintercept) throws IOException {
void start(int port, boolean shouldIntercept) throws IOException {
if (server != null) {
throw new IllegalStateException("Server already running!");
}
ServerBuilder<?> serverBuild = ServerBuilder.forPort(port)
.addService(new FooServiceImpl());
if (shouldintercept) {
.addService(new FooServiceImpl());
if (shouldIntercept) {
serverBuild.intercept(new SentinelGrpcServerInterceptor());
}
server = serverBuild.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;

import io.grpc.StatusRuntimeException;
import org.junit.After;
import org.junit.Test;

import static org.junit.Assert.*;

Expand All @@ -41,52 +43,54 @@ public class SentinelGrpcClientInterceptorTest {
private final int threshold = 2;
private final GrpcTestServer server = new GrpcTestServer();

private void configureFlowRule() {
private void configureFlowRule(int count) {
FlowRule rule = new FlowRule()
.setCount(threshold)
.setCount(count)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setResource(resourceName)
.setLimitApp("default")
.as(FlowRule.class);
FlowRuleManager.loadRules(Collections.singletonList(rule));
}

//@Test
@Test
public void testGrpcClientInterceptor() throws Exception {
final int port = 19328;

configureFlowRule();
configureFlowRule(threshold);
server.start(port, false);

FooServiceClient client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor());
final int total = 8;
for (int i = 0; i < total; i++) {
sendRequest(client);
}

assertTrue(sendRequest(client));
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.OUT);
assertNotNull(clusterNode);
assertEquals(1, clusterNode.passQps());

assertEquals((total - threshold) / 2, clusterNode.blockRequest());
assertEquals(total / 2, clusterNode.totalRequest());
// Not allowed to pass.
configureFlowRule(0);

long totalQps = clusterNode.totalQps();
long passQps = clusterNode.passQps();
long blockQps = clusterNode.blockQps();
assertEquals(total, totalQps);
assertEquals(total - threshold, blockQps);
assertEquals(threshold, passQps);
// The second request will be blocked.
assertFalse(sendRequest(client));
assertEquals(1, clusterNode.blockQps());

server.stop();
}

private void sendRequest(FooServiceClient client) {
private boolean sendRequest(FooServiceClient client) {
try {
FooResponse response = client.sayHello(FooRequest.newBuilder().setName("Sentinel").setId(666).build());
System.out.println(ClusterBuilderSlot.getClusterNode(resourceName, EntryType.OUT).avgRt());
System.out.println("Response: " + response);
return true;
} catch (StatusRuntimeException ex) {
System.out.println("Blocked, cause: " + ex.getMessage());
return false;
}
}

@After
public void cleanUp() {
FlowRuleManager.loadRules(null);
ClusterBuilderSlot.getClusterNodeMap().clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;

import io.grpc.StatusRuntimeException;
import org.junit.After;
import org.junit.Test;

import static org.junit.Assert.*;

Expand All @@ -43,51 +45,54 @@ public class SentinelGrpcServerInterceptorTest {

private FooServiceClient client;

private void configureFlowRule() {
private void configureFlowRule(int count) {
FlowRule rule = new FlowRule()
.setCount(threshold)
.setCount(count)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setResource(resourceName)
.setLimitApp("default")
.as(FlowRule.class);
FlowRuleManager.loadRules(Collections.singletonList(rule));
}

//@Test
@Test
public void testGrpcServerInterceptor() throws Exception {
final int port = 19329;
client = new FooServiceClient("localhost", port);

configureFlowRule();
configureFlowRule(threshold);
server.start(port, true);

final int total = 8;
for (int i = 0; i < total; i++) {
sendRequest();
}
assertTrue(sendRequest());
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.IN);
assertNotNull(clusterNode);
assertEquals(1, clusterNode.passQps());

assertEquals((total - threshold) / 2, clusterNode.blockRequest());
assertEquals(total / 2, clusterNode.totalRequest());
// Not allowed to pass.
configureFlowRule(0);

long totalQps = clusterNode.totalQps();
long passQps = clusterNode.passQps();
long blockQps = clusterNode.blockQps();
assertEquals(total, totalQps);
assertEquals(total - threshold, blockQps);
assertEquals(threshold, passQps);
// The second request will be blocked.
assertFalse(sendRequest());
assertEquals(1, clusterNode.blockQps());

server.stop();
}

private void sendRequest() {
private boolean sendRequest() {
try {
FooResponse response = client.anotherHello(FooRequest.newBuilder().setName("Sentinel").setId(666).build());
System.out.println("Response: " + response);
return true;
} catch (StatusRuntimeException ex) {
System.out.println("Blocked, cause: " + ex.getMessage());
return false;
}
}


@After
public void cleanUp() {
FlowRuleManager.loadRules(null);
ClusterBuilderSlot.getClusterNodeMap().clear();
}
}
18 changes: 18 additions & 0 deletions sentinel-adapter/sentinel-web-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@
<version>${servlet.api.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.17.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.17.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public static RequestOriginParser getRequestOriginParser() {
return requestOriginParser;
}

public static void setRequestOriginParser(
RequestOriginParser requestOriginParser) {
public static void setRequestOriginParser(RequestOriginParser requestOriginParser) {
WebCallbackManager.requestOriginParser = requestOriginParser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void blockRequest(HttpServletRequest request, HttpServletResponse
url.append("?").append(request.getQueryString());
}

if (StringUtil.isEmpty(WebServletConfig.getBlockPage())) {
if (StringUtil.isBlank(WebServletConfig.getBlockPage())) {
writeDefaultBlockedPage(response);
} else {
String redirectUrl = WebServletConfig.getBlockPage() + "?http_referer=" + url.toString();
Expand All @@ -75,7 +75,7 @@ public static void blockRequest(HttpServletRequest request, HttpServletResponse

private static void writeDefaultBlockedPage(HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.println("Blocked by Sentinel (flow limiting)");
out.print(DEFAULT_BLOCK_MSG);
out.flush();
out.close();
}
Expand Down Expand Up @@ -183,5 +183,7 @@ private static int indexOfSlash(char[] chars, int beginIndex, boolean slash) {
return i;
}

public static final String DEFAULT_BLOCK_MSG = "Blocked by Sentinel (flow limiting)";

private FilterUtil() {}
}
Loading

0 comments on commit 695fa9d

Please sign in to comment.