Skip to content

Commit 255b016

Browse files
optimize trpc-spring-cloud-gateway test (#126)
* feat: optimize trpc-spring-cloud-gateway test
1 parent 1ac1d0e commit 255b016

File tree

7 files changed

+46
-119
lines changed

7 files changed

+46
-119
lines changed

trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/autoconfigure/EnableTrpcRouting.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -11,19 +11,16 @@
1111

1212
package com.tencent.trpc.spring.cloud.gateway.autoconfigure;
1313

14-
import com.tencent.trpc.spring.cloud.gateway.TrpcGatewayApplication;
1514
import java.lang.annotation.Documented;
1615
import java.lang.annotation.ElementType;
1716
import java.lang.annotation.Inherited;
1817
import java.lang.annotation.Retention;
1918
import java.lang.annotation.RetentionPolicy;
2019
import java.lang.annotation.Target;
21-
import org.springframework.boot.builder.SpringApplicationBuilder;
2220
import org.springframework.context.annotation.Import;
2321

2422
/**
25-
* When using, you can add annotations to the {@link SpringApplicationBuilder} startup.
26-
* Example code: {@link TrpcGatewayApplication}
23+
* When using it, you need to add the `@EnableTrpcRouting` annotation to your Spring application's startup class.
2724
* The application will automatically load the `Bean` objects in {@link TrpcGatewayConfig} at startup.
2825
*/
2926
@Target({ElementType.TYPE})

trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/META-INF/spring.factories

Lines changed: 0 additions & 1 deletion
This file was deleted.

trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/application.yml

Lines changed: 0 additions & 56 deletions
This file was deleted.

trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/TrpcGatewayApplication.java renamed to trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TestSpringApplication.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
88
* please note that tRPC source code is licensed under the Apache 2.0 License,
99
* A copy of the Apache 2.0 License can be found in the LICENSE file.
1010
*/
1111

12-
package com.tencent.trpc.spring.cloud.gateway;
12+
package com.tencent.trpc.spring.cloud.gateway.filter;
1313

14+
import com.tencent.trpc.spring.boot.starters.annotation.EnableTRpc;
1415
import com.tencent.trpc.spring.cloud.gateway.autoconfigure.EnableTrpcRouting;
16+
import org.springframework.boot.SpringApplication;
1517
import org.springframework.boot.autoconfigure.SpringBootApplication;
16-
import org.springframework.boot.builder.SpringApplicationBuilder;
1718

1819
@EnableTrpcRouting
20+
@EnableTRpc
1921
@SpringBootApplication
20-
public class TrpcGatewayApplication {
22+
public class TestSpringApplication {
2123

2224
public static void main(String[] args) {
23-
new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(args);
25+
SpringApplication.run(TestSpringApplication.class, args);
2426
}
25-
26-
}
27+
}

trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertNotNull;
1616

17-
import com.tencent.trpc.spring.cloud.gateway.TrpcGatewayApplication;
1817
import java.io.IOException;
1918
import java.util.concurrent.TimeUnit;
2019
import okhttp3.MediaType;
@@ -25,23 +24,22 @@
2524
import okhttp3.ResponseBody;
2625
import org.json.JSONException;
2726
import org.json.JSONObject;
28-
import org.junit.jupiter.api.AfterEach;
29-
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.AfterAll;
28+
import org.junit.jupiter.api.BeforeAll;
3029
import org.junit.jupiter.api.Test;
31-
import org.springframework.boot.builder.SpringApplicationBuilder;
30+
import org.springframework.boot.SpringApplication;
3231
import org.springframework.context.ConfigurableApplicationContext;
3332

3433
public class TrpcRoutingFilterTest {
3534

36-
private final String requestBody = "{\"msg\":\"hello gateway!\",\"id\":\"\"}";
35+
private static final String REQUEST_BODY = "{\"msg\":\"hello gateway!\",\"id\":\"\"}";
36+
private static ConfigurableApplicationContext application;
37+
private static OkHttpClient httpClient;
3738

38-
private ConfigurableApplicationContext application;
39-
private OkHttpClient httpClient;
40-
41-
@BeforeEach
42-
void setUp() throws InterruptedException {
43-
application = new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(new String[0]);
44-
TimeUnit.SECONDS.sleep(5);
39+
@BeforeAll
40+
static void setUp() throws InterruptedException {
41+
application = SpringApplication.run(TestSpringApplication.class);
42+
Thread.sleep(3000);
4543

4644
httpClient = new OkHttpClient().newBuilder()
4745
.readTimeout(10, TimeUnit.SECONDS)
@@ -50,37 +48,25 @@ void setUp() throws InterruptedException {
5048
.build();
5149
}
5250

53-
@AfterEach
54-
void tearDown() {
51+
@AfterAll
52+
static void tearDown() {
5553
if (application != null) {
5654
application.close();
5755
}
5856
}
5957

6058
@Test
61-
void filter() {
62-
trpcTest();
63-
httpTest();
64-
}
65-
66-
private void httpTest() {
67-
try {
68-
JSONObject response = gateway(getHttpRequest());
69-
assertNotNull(response);
70-
assertEquals(requestBody, response.toString());
71-
} catch (JSONException | IOException e) {
72-
throw new AssertionError("httpTest failed", e);
73-
}
59+
void httpTest() throws JSONException, IOException {
60+
JSONObject response = gateway(getHttpRequest());
61+
assertNotNull(response);
62+
assertEquals(REQUEST_BODY, response.toString());
7463
}
7564

76-
private void trpcTest() {
77-
try {
78-
JSONObject response = gateway(getTRPCRequest(requestBody));
79-
assertNotNull(response);
80-
assertEquals(requestBody, response.toString());
81-
} catch (JSONException | IOException e) {
82-
throw new AssertionError("trpcTest failed", e);
83-
}
65+
@Test
66+
void trpcTest() throws JSONException, IOException {
67+
JSONObject response = gateway(getTRPCRequest(REQUEST_BODY));
68+
assertNotNull(response);
69+
assertEquals(REQUEST_BODY, response.toString());
8470
}
8571

8672
private JSONObject gateway(Request httpRequest) throws JSONException, IOException {

trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/application.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ spring:
4141
responseRewriter: "com.tencent.trpc.spring.cloud.gateway.rewriter.DefaultTrpcRequestRewriter"
4242

4343
trpc:
44+
server:
45+
local_ip: 127.0.0.1
46+
service:
47+
- name: Hello
48+
impls:
49+
- com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl
50+
port: 8090
51+
- name: HelloHttpService
52+
impls:
53+
- com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl
54+
port: 8091
55+
transporter: jetty
56+
protocol: http
57+
base_path: /test
58+
serialization: json
4459
client:
4560
service:
4661
- name: testHttpClient

trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/trpc_java.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)