Explorar el Código

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 hace 6 años
padre
commit
b61be268a3
Se han modificado 3 ficheros con 39 adiciones y 48 borrados
  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 Ver fichero

@@ -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 Ver fichero

@@ -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;
}
}
}

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

@@ -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;
}
}
}

Cargando…
Cancelar
Guardar