Skip to content

Commit

Permalink
update: add more log for unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
fengye404 committed Nov 24, 2024
1 parent c58691e commit 133a148
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions labs/arthas-grpc-server/src/test/java/unittest/grpc/GrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import arthas.grpc.unittest.ArthasUnittest;
import arthas.grpc.unittest.ArthasUnittestServiceGrpc;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.taobao.arthas.grpc.server.ArthasGrpcServer;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
Expand All @@ -31,12 +32,18 @@ public class GrpcTest {
private static final int PORT = 9091;
private static final String HOST_PORT = HOST + ":" + PORT;
private static final String UNIT_TEST_GRPC_SERVICE_PACKAGE_NAME = "unittest.grpc.service.impl";
private static final Logger log = (Logger) LoggerFactory.getLogger(GrpcTest.class);
private ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub blockingStub = null;
Random random = new Random();
ExecutorService threadPool = Executors.newFixedThreadPool(10);


@Before
public void startServer() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger("ROOT");
rootLogger.setLevel(Level.INFO);

Thread grpcWebProxyStart = new Thread(() -> {
ArthasGrpcServer arthasGrpcServer = new ArthasGrpcServer(PORT, UNIT_TEST_GRPC_SERVICE_PACKAGE_NAME);
arthasGrpcServer.start();
Expand All @@ -46,6 +53,7 @@ public void startServer() {

@Test
public void testUnary() {
log.info("testUnary start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();
Expand All @@ -59,10 +67,12 @@ public void testUnary() {
} finally {
channel.shutdownNow();
}
log.info("testUnary success!");
}

@Test
public void testUnarySum() throws InterruptedException {
log.info("testUnarySum start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();
Expand All @@ -71,7 +81,7 @@ public void testUnarySum() throws InterruptedException {
for (int i = 0; i < 10; i++) {
AtomicInteger sum = new AtomicInteger(0);
int finalId = i;
for (int j = 0; j < 100; j++) {
for (int j = 0; j < 10; j++) {
int num = random.nextInt(101);
sum.addAndGet(num);
threadPool.submit(() -> {
Expand All @@ -84,11 +94,13 @@ public void testUnarySum() throws InterruptedException {
Assert.assertEquals(sum.get(), grpcSum);
}
channel.shutdown();
log.info("testUnarySum success!");
}

// 用于测试客户端流
@Test
public void testClientStreamSum() throws Throwable {
log.info("testClientStreamSum start!");
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9091)
.usePlaintext()
.build();
Expand Down Expand Up @@ -125,11 +137,13 @@ public void onCompleted() {
clientStreamObserver.onCompleted();
latch.await(20,TimeUnit.SECONDS);
channel.shutdown();
log.info("testClientStreamSum success!");
}

// 用于测试请求数据隔离性
@Test
public void testDataIsolation() throws InterruptedException {
log.info("testDataIsolation start!");
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9091)
.usePlaintext()
.build();
Expand Down Expand Up @@ -179,10 +193,12 @@ public void onCompleted() {
});
}
Thread.sleep(10000L);
log.info("testDataIsolation success!");
}

@Test
public void testServerStream() throws InterruptedException {
log.info("testServerStream start!");
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9091)
.usePlaintext()
.build();
Expand Down Expand Up @@ -214,11 +230,13 @@ public void onCompleted() {
} finally {
channel.shutdown();
}
log.info("testServerStream success!");
}

// 用于测试双向流
@Test
public void testBiStream() throws Throwable {
log.info("testBiStream start!");
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9091)
.usePlaintext()
.build();
Expand Down Expand Up @@ -254,6 +272,7 @@ public void onCompleted() {
biStreamObserver.onCompleted();
latch.await(20, TimeUnit.SECONDS);
channel.shutdown();
log.info("testBiStream success!");
}

private void addSum(ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub stub, int id, int num) {
Expand Down

0 comments on commit 133a148

Please sign in to comment.