Skip to content

Commit

Permalink
Initialize the environment before testing
Browse files Browse the repository at this point in the history
  • Loading branch information
x956 committed Sep 21, 2023
1 parent e3543bf commit cee1837
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 78 deletions.
8 changes: 1 addition & 7 deletions arthas-grpc-web-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ from: https://github.com/grpc/grpc-web/tree/1.4.2/src/connector

工程导入IDE之后,进入test目录

1. 启动 com.taobao.arthas.grpcweb.proxy.server.startGrpcTest
2. 启动 com.taobao.arthas.grpcweb.proxy.server.startGrpcWebProxyTest
3. 在 com.taobao.arthas.grpcweb.proxy.server.GrpcWebProxyServerTest 发送请求

demo
1. cd browser-test ,启动 http server,比如 `python -m http.server`
2. 访问上面启动的 http server,比如 http://127.0.0.1:8000/echotest.html
在 com.taobao.arthas.grpcweb.proxy.server.GrpcWebProxyServerTest 启动测试

也可以用原项目的相关工程来测试

Expand Down
6 changes: 6 additions & 0 deletions arthas-grpc-web-proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
<version>4.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.taobao.arthas.grpcweb.proxy.server;

import grpc.gateway.testing.Echo;
import helloworld.Helloworld;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -11,6 +10,8 @@
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.taobao.arthas.common.SocketUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -21,13 +22,34 @@

public class GrpcWebProxyServerTest {

//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
private int GRPC_WEB_PROXY_PORT;
private int GRPC_PORT;
private String hostName;
private CloseableHttpClient httpClient;
@Before
public void startServer(){
GRPC_WEB_PROXY_PORT = SocketUtils.findAvailableTcpPort();
GRPC_PORT = SocketUtils.findAvailableTcpPort();
// 启动grpc服务
Thread grpcStart = new Thread(() -> {
startGrpcTest startGrpcTest = new startGrpcTest(GRPC_PORT);
startGrpcTest.startGrpcService();
});
grpcStart.start();
// 启动grpc-web-proxy服务
Thread grpcWebProxyStart = new Thread(() -> {
startGrpcWebProxyTest startGrpcWebProxyTest = new startGrpcWebProxyTest(GRPC_WEB_PROXY_PORT,GRPC_PORT);
startGrpcWebProxyTest.startGrpcWebProxy();
});
grpcWebProxyStart.start();
hostName = "http://127.0.0.1:" + GRPC_WEB_PROXY_PORT;
httpClient = HttpClients.createDefault();
}

@Test
public void simpleReqTest() {
// 单个response
String url = "http://127.0.0.1:8080/grpc.gateway.testing.EchoService/Echo";
String url = hostName +"/grpc.gateway.testing.EchoService/Echo";

String requestStr = "hello world!!!";
Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage(requestStr).build();
Expand Down Expand Up @@ -77,7 +99,7 @@ public void simpleReqTest() {
@Test
public void streamReqTest() {
// stream response
String url = "http://127.0.0.1:8080/grpc.gateway.testing.EchoService/ServerStreamingEcho";
String url = hostName + "/grpc.gateway.testing.EchoService/ServerStreamingEcho";
String requestStr = "hello world!!!";
Echo.ServerStreamingEchoRequest request = Echo.ServerStreamingEchoRequest.newBuilder().setMessage(requestStr)
.setMessageCount(5)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import com.taobao.arthas.grpcweb.proxy.server.grpcService.GreeterService;
import com.taobao.arthas.grpcweb.proxy.server.grpcService.HelloImpl;
import io.grpc.BindableService;
import io.grpc.Grpc;
import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

public class startGrpcTest {

static final int GRPC_PORT = 50051;
private int GRPC_PORT;

public static void main(String[] args) {
public startGrpcTest(int grpcPort){
this.GRPC_PORT = grpcPort;
}

public void startGrpcService(){
try {
Server grpcServer = ServerBuilder.forPort(GRPC_PORT).addService((BindableService) new GreeterService())
.addService((BindableService) new HelloImpl()).addService(new EchoImpl()).build();
Expand All @@ -25,5 +30,4 @@ public static void main(String[] args) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

public class startGrpcWebProxyTest {

static final int GRPC_WEB_PORT = 8080;
private int GRPC_WEB_PROXY_PORT;

public static void main(String[] args) {
GrpcWebProxyServer grpcWebProxyServer = new GrpcWebProxyServer(GRPC_WEB_PORT, 50051);
private int GRPC_PORT;


public startGrpcWebProxyTest(int grpcWebPort, int grpcPort){
this.GRPC_WEB_PROXY_PORT = grpcWebPort;
this.GRPC_PORT = grpcPort;
}

public void startGrpcWebProxy(){
GrpcWebProxyServer grpcWebProxyServer = new GrpcWebProxyServer(GRPC_WEB_PROXY_PORT, GRPC_PORT);
grpcWebProxyServer.start();
}
}

0 comments on commit cee1837

Please sign in to comment.