Browse Source

Refactor test cases for Sentinel gRPC Adapter (#101)

- Added a GrpcTestServer to abstract common server logic
- Refactor with new added `GrpcTestServer`
master
refactormachine Eric Zhao 6 years ago
parent
commit
b61be268a3
3 changed files with 39 additions and 48 deletions
  1. +33
    -0
      sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java
  2. +3
    -24
      sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java
  3. +3
    -24
      sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java

+ 33
- 0
sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java View File

@@ -0,0 +1,33 @@
package com.alibaba.csp.sentinel.adapter.grpc;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

class GrpcTestServer {
private Server server;

GrpcTestServer() {
}

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) {
serverBuild.intercept(new SentinelGrpcServerInterceptor());
}
server = serverBuild.build();
server.start();
}

void stop() {
if (server != null) {
server.shutdown();
server = null;
}
}
}

+ 3
- 24
sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java View File

@@ -15,7 +15,6 @@
*/ */
package com.alibaba.csp.sentinel.adapter.grpc; package com.alibaba.csp.sentinel.adapter.grpc;


import java.io.IOException;
import java.util.Collections; import java.util.Collections;


import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.EntryType;
@@ -27,10 +26,7 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
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.Server;
import io.grpc.ServerBuilder;
import io.grpc.StatusRuntimeException; import io.grpc.StatusRuntimeException;
import org.junit.Test;


import static org.junit.Assert.*; import static org.junit.Assert.*;


@@ -43,8 +39,7 @@ public class SentinelGrpcClientInterceptorTest {


private final String resourceName = "com.alibaba.sentinel.examples.FooService/sayHello"; private final String resourceName = "com.alibaba.sentinel.examples.FooService/sayHello";
private final int threshold = 2; private final int threshold = 2;

private Server server;
private final GrpcTestServer server = new GrpcTestServer();


private void configureFlowRule() { private void configureFlowRule() {
FlowRule rule = new FlowRule() FlowRule rule = new FlowRule()
@@ -61,7 +56,7 @@ public class SentinelGrpcClientInterceptorTest {
final int port = 19328; final int port = 19328;


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


FooServiceClient client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor()); FooServiceClient client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor());
final int total = 8; final int total = 8;
@@ -81,7 +76,7 @@ public class SentinelGrpcClientInterceptorTest {
assertEquals(total - threshold, blockedQps); assertEquals(total - threshold, blockedQps);
assertEquals(threshold, passQps); assertEquals(threshold, passQps);


stopServer();
server.stop();
} }


private void sendRequest(FooServiceClient client) { private void sendRequest(FooServiceClient client) {
@@ -94,20 +89,4 @@ public class SentinelGrpcClientInterceptorTest {
} }
} }


private void prepareServer(int port) throws IOException {
if (server != null) {
throw new IllegalStateException("Server already running!");
}
server = ServerBuilder.forPort(port)
.addService(new FooServiceImpl())
.build();
server.start();
}

private void stopServer() {
if (server != null) {
server.shutdown();
server = null;
}
}
} }

+ 3
- 24
sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java View File

@@ -15,7 +15,6 @@
*/ */
package com.alibaba.csp.sentinel.adapter.grpc; package com.alibaba.csp.sentinel.adapter.grpc;


import java.io.IOException;
import java.util.Collections; import java.util.Collections;


import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.EntryType;
@@ -27,10 +26,7 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
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.Server;
import io.grpc.ServerBuilder;
import io.grpc.StatusRuntimeException; import io.grpc.StatusRuntimeException;
import org.junit.Test;


import static org.junit.Assert.*; import static org.junit.Assert.*;


@@ -43,8 +39,8 @@ public class SentinelGrpcServerInterceptorTest {


private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHello"; private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHello";
private final int threshold = 4; private final int threshold = 4;
private final GrpcTestServer server = new GrpcTestServer();


private Server server;
private FooServiceClient client; private FooServiceClient client;


private void configureFlowRule() { private void configureFlowRule() {
@@ -63,7 +59,7 @@ public class SentinelGrpcServerInterceptorTest {
client = new FooServiceClient("localhost", port); client = new FooServiceClient("localhost", port);


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


final int total = 8; final int total = 8;
for (int i = 0; i < total; i++) { for (int i = 0; i < total; i++) {
@@ -82,7 +78,7 @@ public class SentinelGrpcServerInterceptorTest {
assertEquals(total - threshold, blockedQps); assertEquals(total - threshold, blockedQps);
assertEquals(threshold, passQps); assertEquals(threshold, passQps);


stopServer();
server.stop();
} }


private void sendRequest() { private void sendRequest() {
@@ -94,21 +90,4 @@ public class SentinelGrpcServerInterceptorTest {
} }
} }


private void prepareServer(int port) throws IOException {
if (server != null) {
throw new IllegalStateException("Server already running!");
}
server = ServerBuilder.forPort(port)
.addService(new FooServiceImpl())
.intercept(new SentinelGrpcServerInterceptor())
.build();
server.start();
}

private void stopServer() {
if (server != null) {
server.shutdown();
server = null;
}
}
} }

Loading…
Cancel
Save