use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelGrpcServerInterceptorTest method testGrpcServerInterceptor.
@Test
public void testGrpcServerInterceptor() throws Exception {
final int port = 19329;
server.start(port, true);
client = new FooServiceClient("localhost", port);
configureFlowRule(Integer.MAX_VALUE);
assertTrue(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(666).build()));
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(resourceName, EntryType.IN);
assertNotNull(clusterNode);
assertEquals(1, clusterNode.totalPass());
// Not allowed to pass.
configureFlowRule(0);
// The second request will be blocked.
assertFalse(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(666).build()));
assertEquals(1, clusterNode.blockRequest());
configureFlowRule(Integer.MAX_VALUE);
assertFalse(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(-1).build()));
assertEquals(1, clusterNode.totalException());
configureFlowRule(Integer.MAX_VALUE);
assertTrue(sendRequest(FooRequest.newBuilder().setName("Sentinel").setId(-2).build()));
assertTrue(clusterNode.avgRt() >= 1000);
server.stop();
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class SentinelSofaRpcProviderFilterTest method verifyInvocationStructure.
/**
* Verify Sentinel invocation structure in memory:
* EntranceNode(methodResourceName)
* --InterfaceNode(interfaceResourceName)
* ----MethodNode(methodResourceName)
*/
private void verifyInvocationStructure(String applicationName, String interfaceResourceName, String methodResourceName) {
Context context = ContextUtil.getContext();
assertNotNull(context);
assertEquals(methodResourceName, context.getName());
assertEquals(applicationName, context.getOrigin());
DefaultNode entranceNode = context.getEntranceNode();
ResourceWrapper entranceResource = entranceNode.getId();
assertEquals(methodResourceName, entranceResource.getName());
assertSame(EntryType.IN, entranceResource.getEntryType());
// As SphU.entry(interfaceResourceName, EntryType.IN);
Set<Node> childList = entranceNode.getChildList();
assertEquals(1, childList.size());
DefaultNode interfaceNode = (DefaultNode) childList.iterator().next();
ResourceWrapper interfaceResource = interfaceNode.getId();
assertEquals(interfaceResourceName, interfaceResource.getName());
assertSame(EntryType.IN, interfaceResource.getEntryType());
// As SphU.entry(methodResourceName, EntryType.IN, 1, methodArguments);
childList = interfaceNode.getChildList();
assertEquals(1, childList.size());
DefaultNode methodNode = (DefaultNode) childList.iterator().next();
ResourceWrapper methodResource = methodNode.getId();
assertEquals(methodResourceName, methodResource.getName());
assertSame(EntryType.IN, methodResource.getEntryType());
// Verify curEntry
Entry curEntry = context.getCurEntry();
assertSame(methodNode, curEntry.getCurNode());
assertSame(interfaceNode, curEntry.getLastNode());
// As context origin is not "", originNode should be created
assertNotNull(curEntry.getOriginNode());
// Verify clusterNode
ClusterNode methodClusterNode = methodNode.getClusterNode();
ClusterNode interfaceClusterNode = interfaceNode.getClusterNode();
// Different resource->Different ProcessorSlot->Different ClusterNode
assertNotSame(methodClusterNode, interfaceClusterNode);
// As context origin is not "", the StatisticNode should be created in originCountMap of ClusterNode
Map<String, StatisticNode> methodOriginCountMap = methodClusterNode.getOriginCountMap();
assertEquals(1, methodOriginCountMap.size());
assertTrue(methodOriginCountMap.containsKey(applicationName));
Map<String, StatisticNode> interfaceOriginCountMap = interfaceClusterNode.getOriginCountMap();
assertEquals(1, interfaceOriginCountMap.size());
assertTrue(interfaceOriginCountMap.containsKey(applicationName));
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ProviderFilterTest method testExceptionMapper.
@Test
public void testExceptionMapper() {
String url = "/test/ex";
String resourceName = "GET:" + url;
Response response = given().get(url);
response.then().statusCode(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).body(equalTo("test exception mapper"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ProviderFilterTest method testGetHello.
@Test
public void testGetHello() {
String url = "/test/hello";
String resourceName = "GET:" + url;
Response response = given().get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
String context = "";
for (Node n : Constants.ROOT.getChildList()) {
if (n instanceof EntranceNode) {
String id = ((EntranceNode) n).getId().getName();
if (url.equals(id)) {
context = ((EntranceNode) n).getId().getName();
}
}
}
assertEquals("", context);
}
use of com.alibaba.csp.sentinel.node.ClusterNode in project Sentinel by alibaba.
the class ProviderFilterTest method testUrlPathParam.
@Test
public void testUrlPathParam() {
String url = "/test/hello/{name}";
String resourceName = "GET:" + url;
String url1 = "/test/hello/abc";
Response response1 = given().get(url1);
response1.then().statusCode(200).body(equalTo("Hello abc !"));
String url2 = "/test/hello/def";
Response response2 = given().get(url2);
response2.then().statusCode(200).body(equalTo("Hello def !"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(2, cn.passQps(), 0.01);
assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url1));
assertNull(ClusterBuilderSlot.getClusterNode("GET:" + url2));
}
Aggregations