From b61be268a3c0ce8a364ac81f02fc8b9c6c3ce63a Mon Sep 17 00:00:00 2001 From: refactormachine <39365897+refactormachine@users.noreply.github.com> Date: Thu, 30 Aug 2018 18:57:23 +0300 Subject: [PATCH] Refactor test cases for Sentinel gRPC Adapter (#101) - Added a GrpcTestServer to abstract common server logic - Refactor with new added `GrpcTestServer` --- .../sentinel/adapter/grpc/GrpcTestServer.java | 33 +++++++++++++++++++ .../SentinelGrpcClientInterceptorTest.java | 27 ++------------- .../SentinelGrpcServerInterceptorTest.java | 27 ++------------- 3 files changed, 39 insertions(+), 48 deletions(-) create mode 100644 sentinel-adapter/sentinel-grpc-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/grpc/GrpcTestServer.java 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