diff --git a/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java new file mode 100644 index 00000000..48b5f48c --- /dev/null +++ b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java @@ -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; + } + } +} \ No newline at end of file diff --git a/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java index b7acc649..61fa180e 100755 --- a/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java +++ b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcClientInterceptorTest.java @@ -15,7 +15,6 @@ */ package com.alibaba.csp.sentinel.adapter.grpc; -import java.io.IOException; import java.util.Collections; 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.clusterbuilder.ClusterBuilderSlot; -import io.grpc.Server; -import io.grpc.ServerBuilder; import io.grpc.StatusRuntimeException; -import org.junit.Test; import static org.junit.Assert.*; @@ -43,8 +39,7 @@ public class SentinelGrpcClientInterceptorTest { private final String resourceName = "com.alibaba.sentinel.examples.FooService/sayHello"; private final int threshold = 2; - - private Server server; + private final GrpcTestServer server = new GrpcTestServer(); private void configureFlowRule() { FlowRule rule = new FlowRule() @@ -61,7 +56,7 @@ public class SentinelGrpcClientInterceptorTest { final int port = 19328; configureFlowRule(); - prepareServer(port); + server.start(port, false); FooServiceClient client = new FooServiceClient("localhost", port, new SentinelGrpcClientInterceptor()); final int total = 8; @@ -81,7 +76,7 @@ public class SentinelGrpcClientInterceptorTest { assertEquals(total - threshold, blockedQps); assertEquals(threshold, passQps); - stopServer(); + server.stop(); } 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; - } - } } \ No newline at end of file diff --git a/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java index 5be16eee..d924742c 100755 --- a/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java +++ b/sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/SentinelGrpcServerInterceptorTest.java @@ -15,7 +15,6 @@ */ package com.alibaba.csp.sentinel.adapter.grpc; -import java.io.IOException; import java.util.Collections; 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.clusterbuilder.ClusterBuilderSlot; -import io.grpc.Server; -import io.grpc.ServerBuilder; import io.grpc.StatusRuntimeException; -import org.junit.Test; import static org.junit.Assert.*; @@ -43,8 +39,8 @@ public class SentinelGrpcServerInterceptorTest { private final String resourceName = "com.alibaba.sentinel.examples.FooService/anotherHello"; private final int threshold = 4; + private final GrpcTestServer server = new GrpcTestServer(); - private Server server; private FooServiceClient client; private void configureFlowRule() { @@ -63,7 +59,7 @@ public class SentinelGrpcServerInterceptorTest { client = new FooServiceClient("localhost", port); configureFlowRule(); - prepareServer(port); + server.start(port, true); final int total = 8; for (int i = 0; i < total; i++) { @@ -82,7 +78,7 @@ public class SentinelGrpcServerInterceptorTest { assertEquals(total - threshold, blockedQps); assertEquals(threshold, passQps); - stopServer(); + server.stop(); } 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; - } - } } \ No newline at end of file