Skip to content

Commit fc47852

Browse files
author
Miguel Gonzalez Sanchez
committed
add tests
1 parent 01f6d42 commit fc47852

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/recovery/GRpcRecoveryTest.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package org.lognet.springboot.grpc.recovery;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.hamcrest.Matchers.isA;
6+
import static org.hamcrest.Matchers.notNullValue;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.Mockito.never;
10+
import static org.mockito.Mockito.times;
11+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
12+
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.ExecutionException;
15+
import java.util.concurrent.TimeUnit;
16+
import java.util.concurrent.TimeoutException;
17+
318
import io.grpc.Metadata;
419
import io.grpc.Status;
520
import io.grpc.StatusRuntimeException;
@@ -15,25 +30,11 @@
1530
import org.springframework.boot.test.context.SpringBootTest;
1631
import org.springframework.boot.test.context.TestConfiguration;
1732
import org.springframework.boot.test.mock.mockito.SpyBean;
33+
import org.springframework.context.annotation.Bean;
1834
import org.springframework.context.annotation.Import;
1935
import org.springframework.test.context.ActiveProfiles;
2036
import org.springframework.test.context.junit4.SpringRunner;
2137

22-
import java.util.concurrent.CompletableFuture;
23-
import java.util.concurrent.ExecutionException;
24-
import java.util.concurrent.TimeUnit;
25-
import java.util.concurrent.TimeoutException;
26-
27-
import static org.hamcrest.MatcherAssert.assertThat;
28-
import static org.hamcrest.Matchers.is;
29-
import static org.hamcrest.Matchers.isA;
30-
import static org.hamcrest.Matchers.notNullValue;
31-
import static org.junit.jupiter.api.Assertions.assertThrows;
32-
import static org.mockito.ArgumentMatchers.any;
33-
import static org.mockito.Mockito.never;
34-
import static org.mockito.Mockito.times;
35-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
36-
3738
@RunWith(SpringRunner.class)
3839
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE)
3940
@ActiveProfiles({"disable-security"})
@@ -43,6 +44,7 @@ public class GRpcRecoveryTest extends GrpcServerTestBase {
4344
static class CheckedException extends Exception {
4445

4546
}
47+
4648
static class CheckedException1 extends Exception {
4749

4850
}
@@ -59,16 +61,25 @@ static class Exception1 extends RuntimeException {
5961

6062
}
6163

64+
@GRpcServiceAdvice
65+
static class BeanCustomErrorHandler {
66+
67+
@GRpcExceptionHandler
68+
public Status handleA(ExceptionA e, GRpcExceptionScope scope) {
69+
return Status.NOT_FOUND;
70+
}
71+
}
72+
6273
@TestConfiguration
6374
static class Cfg {
6475

76+
@Bean
77+
public BeanCustomErrorHandler otherErrorHandler() {
78+
return new BeanCustomErrorHandler();
79+
}
6580

6681
@GRpcServiceAdvice
6782
static class CustomErrorHandler {
68-
@GRpcExceptionHandler
69-
public Status handleA(ExceptionA e, GRpcExceptionScope scope) {
70-
return Status.NOT_FOUND;
71-
}
7283

7384
@GRpcExceptionHandler
7485
public Status handleB(ExceptionB e, GRpcExceptionScope scope) {
@@ -87,7 +98,6 @@ public Status handle(Exception e, GRpcExceptionScope scope) {
8798
}
8899
}
89100

90-
91101
@GRpcService
92102
static class CustomService extends CustomServiceGrpc.CustomServiceImplBase {
93103

@@ -150,12 +160,14 @@ public Status handleB(ExceptionB e, GRpcExceptionScope scope) {
150160
@SpyBean
151161
private Cfg.CustomErrorHandler handler;
152162

163+
@SpyBean
164+
private BeanCustomErrorHandler beanErrorHandler;
165+
153166

154167
@Test
155168
public void streamingServiceErrorHandlerTest() throws ExecutionException, InterruptedException, TimeoutException {
156169

157170

158-
159171
final CompletableFuture<Throwable> errorFuture = new CompletableFuture<>();
160172
final StreamObserver<Custom.CustomReply> reply = new StreamObserver<Custom.CustomReply>() {
161173

@@ -180,15 +192,12 @@ public void onCompleted() {
180192
requests.onCompleted();
181193

182194

183-
184-
185-
186195
final Throwable actual = errorFuture.get(20, TimeUnit.SECONDS);
187196
assertThat(actual, notNullValue());
188197
assertThat(actual, isA(StatusRuntimeException.class));
189-
assertThat(((StatusRuntimeException)actual).getStatus(), is(Status.RESOURCE_EXHAUSTED));
198+
assertThat(((StatusRuntimeException) actual).getStatus(), is(Status.RESOURCE_EXHAUSTED));
190199

191-
Mockito.verify(srv,times(1)).handle(any(CheckedException1.class),any());
200+
Mockito.verify(srv, times(1)).handle(any(CheckedException1.class), any());
192201

193202
}
194203

@@ -205,9 +214,8 @@ public void checkedExceptionHandlerTest() {
205214

206215
Mockito.verify(srv, never()).handleB(any(), any());
207216

208-
217+
Mockito.verify(beanErrorHandler, never()).handleA(any(), any());
209218
Mockito.verify(handler, never()).handle(any(), any());
210-
Mockito.verify(handler, never()).handleA(any(), any());
211219
Mockito.verify(handler, times(1)).handleCheckedException(any(CheckedException.class), any());
212220
Mockito.verify(handler, never()).handleB(any(), any());
213221

grpc-spring-boot-starter-gradle-plugin/src/main/resources/grpc-spring-boot.gradle

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,23 @@ sourceSets.configureEach{s->
4040

4141
protobuf {
4242
protoc {
43-
artifact = "com.google.protobuf:protoc:${grpcSpringBoot.protocVersion.get()}"
43+
44+
// for apple m1, please add protoc_platform=osx-x86_64 in $HOME/.gradle/gradle.properties
45+
if (project.hasProperty('protoc_platform')) {
46+
artifact = "com.google.protobuf:protoc:${grpcSpringBoot.protocVersion.get()}:${protoc_platform}"
47+
} else {
48+
artifact = "com.google.protobuf:protoc:${grpcSpringBoot.protocVersion.get()}"
49+
}
4450
}
51+
4552
plugins {
4653
grpc {
47-
artifact = "io.grpc:protoc-gen-grpc-java:${grpcSpringBoot.grpcVersion.get()}"
54+
// for apple m1, please add protoc_platform=osx-x86_64 in $HOME/.gradle/gradle.properties
55+
if (project.hasProperty('protoc_platform')) {
56+
artifact = "io.grpc:protoc-gen-grpc-java:${grpcSpringBoot.grpcVersion.get()}:${protoc_platform}"
57+
} else {
58+
artifact = "io.grpc:protoc-gen-grpc-java:${grpcSpringBoot.grpcVersion.get()}"
59+
}
4860
}
4961
}
5062

0 commit comments

Comments
 (0)