Skip to content

Commit

Permalink
update: modify unittest port
Browse files Browse the repository at this point in the history
  • Loading branch information
fengye404 committed Nov 24, 2024
1 parent ec80656 commit 8ba3740
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions labs/arthas-grpc-server/src/test/java/unittest/grpc/GrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GrpcTest {
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;
private ManagedChannel clientChannel;
Random random = new Random();
ExecutorService threadPool = Executors.newFixedThreadPool(10);

Expand All @@ -42,42 +42,42 @@ public class GrpcTest {
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();
});
grpcWebProxyStart.start();

clientChannel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();
}

@Test
public void testUnary() {
log.info("testUnary start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub stub = ArthasUnittestServiceGrpc.newBlockingStub(channel);

ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub stub = ArthasUnittestServiceGrpc.newBlockingStub(clientChannel);

try {
ArthasUnittest.ArthasUnittestRequest request = ArthasUnittest.ArthasUnittestRequest.newBuilder().setMessage("unaryInvoke").build();
ArthasUnittest.ArthasUnittestResponse res = stub.unary(request);
System.out.println(res.getMessage());
} finally {
channel.shutdownNow();
clientChannel.shutdownNow();
}
log.info("testUnary success!");
}

@Test
public void testUnarySum() throws InterruptedException {
log.info("testUnarySum start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub stub = ArthasUnittestServiceGrpc.newBlockingStub(channel);
ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub stub = ArthasUnittestServiceGrpc.newBlockingStub(clientChannel);
for (int i = 0; i < 10; i++) {
AtomicInteger sum = new AtomicInteger(0);
int finalId = i;
Expand All @@ -93,19 +93,16 @@ public void testUnarySum() throws InterruptedException {
System.out.println("id:" + finalId + ",sum:" + sum.get() + ",grpcSum:" + grpcSum);
Assert.assertEquals(sum.get(), grpcSum);
}
channel.shutdown();
clientChannel.shutdown();
log.info("testUnarySum success!");
}

// 用于测试客户端流
@Test
public void testClientStreamSum() throws Throwable {
log.info("testClientStreamSum start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(channel);
ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(clientChannel);

AtomicInteger sum = new AtomicInteger(0);
CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -136,19 +133,16 @@ public void onCompleted() {

clientStreamObserver.onCompleted();
latch.await(20,TimeUnit.SECONDS);
channel.shutdown();
clientChannel.shutdown();
log.info("testClientStreamSum success!");
}

// 用于测试请求数据隔离性
@Test
public void testDataIsolation() throws InterruptedException {
log.info("testDataIsolation start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(channel);
ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(clientChannel);
for (int i = 0; i < 10; i++) {
threadPool.submit(() -> {
AtomicInteger sum = new AtomicInteger(0);
Expand Down Expand Up @@ -189,7 +183,7 @@ public void onCompleted() {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
channel.shutdown();
clientChannel.shutdown();
});
}
Thread.sleep(10000L);
Expand All @@ -199,11 +193,8 @@ public void onCompleted() {
@Test
public void testServerStream() throws InterruptedException {
log.info("testServerStream start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(channel);
ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(clientChannel);

ArthasUnittest.ArthasUnittestRequest request = ArthasUnittest.ArthasUnittestRequest.newBuilder().setMessage("serverStream").build();

Expand All @@ -228,7 +219,7 @@ public void onCompleted() {
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
channel.shutdown();
clientChannel.shutdown();
}
log.info("testServerStream success!");
}
Expand All @@ -237,11 +228,8 @@ public void onCompleted() {
@Test
public void testBiStream() throws Throwable {
log.info("testBiStream start!");
ManagedChannel channel = ManagedChannelBuilder.forTarget(HOST_PORT)
.usePlaintext()
.build();

ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(channel);
ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = ArthasUnittestServiceGrpc.newStub(clientChannel);

CountDownLatch latch = new CountDownLatch(1);
StreamObserver<ArthasUnittest.ArthasUnittestRequest> biStreamObserver = stub.biStream(new StreamObserver<ArthasUnittest.ArthasUnittestResponse>() {
Expand Down Expand Up @@ -271,7 +259,7 @@ public void onCompleted() {
Thread.sleep(2000);
biStreamObserver.onCompleted();
latch.await(20, TimeUnit.SECONDS);
channel.shutdown();
clientChannel.shutdown();
log.info("testBiStream success!");
}

Expand Down

0 comments on commit 8ba3740

Please sign in to comment.