-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: add unit test and error process
- Loading branch information
Showing
8 changed files
with
153 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
arthas-grpc-server/src/main/java/com/taobao/arthas/grpc/server/handler/ErrorRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.taobao.arthas.grpc.server.handler; | ||
|
||
import com.taobao.arthas.grpc.server.protobuf.annotation.ProtobufClass; | ||
|
||
/** | ||
* @author: FengYe | ||
* @date: 2024/9/23 23:58 | ||
* @description: ErrorRes | ||
*/ | ||
@ProtobufClass | ||
public class ErrorRes { | ||
private String errorMsg; | ||
|
||
public String getErrorMsg() { | ||
return errorMsg; | ||
} | ||
|
||
public void setErrorMsg(String errorMsg) { | ||
this.errorMsg = errorMsg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
102 changes: 100 additions & 2 deletions
102
arthas-grpc-server/src/test/java/unittest/grpc/GrpcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,108 @@ | ||
package unittest.grpc; | ||
|
||
import arthas.unittest.ArthasUnittest; | ||
import arthas.unittest.ArthasUnittestServiceGrpc; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.stub.StreamObserver; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.Disabled; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* @author: FengYe | ||
* @date: 2024/9/19 22:20 | ||
* @description: grpc.GrpcTest | ||
* @date: 2024/9/24 00:17 | ||
* @description: GrpcUnaryTest | ||
*/ | ||
public class GrpcTest { | ||
private static final String target = "localhost:9090"; | ||
private ArthasUnittestServiceGrpc.ArthasUnittestServiceBlockingStub blockingStub = null; | ||
private ArthasUnittestServiceGrpc.ArthasUnittestServiceStub stub = null; | ||
|
||
@Disabled("跳过启动测试") | ||
@Test | ||
public void testUnary() { | ||
ManagedChannel channel = ManagedChannelBuilder.forTarget(target) | ||
.usePlaintext() | ||
.build(); | ||
|
||
blockingStub = ArthasUnittestServiceGrpc.newBlockingStub(channel); | ||
|
||
try { | ||
trace("trace"); | ||
} finally { | ||
channel.shutdownNow(); | ||
} | ||
} | ||
|
||
@Disabled("跳过启动测试") | ||
@Test | ||
public void testStream() { | ||
ManagedChannel channel = ManagedChannelBuilder.forTarget(target) | ||
.usePlaintext() | ||
.build(); | ||
|
||
stub = ArthasUnittestServiceGrpc.newStub(channel); | ||
|
||
try { | ||
watch("watch1", "watch2", "watch3"); | ||
} finally { | ||
channel.shutdownNow(); | ||
} | ||
} | ||
|
||
private void trace(String name) { | ||
ArthasUnittest.ArthasUnittestRequest request = ArthasUnittest.ArthasUnittestRequest.newBuilder().setMessage(name).build(); | ||
try { | ||
ArthasUnittest.ArthasUnittestResponse res = blockingStub.trace(request); | ||
System.out.println(res.getMessage()); | ||
} catch (StatusRuntimeException e) { | ||
e.printStackTrace(); | ||
System.out.println("RPC failed: " + e.getStatus()); | ||
} | ||
} | ||
|
||
private void watch(String... names) { | ||
// 使用 CountDownLatch 来等待所有响应 | ||
CountDownLatch finishLatch = new CountDownLatch(1); | ||
|
||
StreamObserver<ArthasUnittest.ArthasUnittestRequest> watch = stub.watch(new StreamObserver<ArthasUnittest.ArthasUnittestResponse>() { | ||
@Override | ||
public void onNext(ArthasUnittest.ArthasUnittestResponse value) { | ||
System.out.println("watch: " + value.getMessage()); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
|
||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
System.out.println("Finished sending watch."); | ||
} | ||
}); | ||
|
||
|
||
try { | ||
for (String name : names) { | ||
ArthasUnittest.ArthasUnittestRequest request = ArthasUnittest.ArthasUnittestRequest.newBuilder().setMessage(name).build(); | ||
Thread.sleep(1000L); | ||
watch.onNext(request); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
watch.onCompleted(); | ||
} | ||
|
||
// 等待服务器的响应 | ||
try { | ||
finishLatch.await(); // 等待完成 | ||
} catch (InterruptedException e) { | ||
System.out.println("Client interrupted: " + e.getMessage()); | ||
} | ||
} | ||
} |