1
1
package org .lognet .springboot .grpc .recovery ;
2
2
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
+
3
18
import io .grpc .Metadata ;
4
19
import io .grpc .Status ;
5
20
import io .grpc .StatusRuntimeException ;
15
30
import org .springframework .boot .test .context .SpringBootTest ;
16
31
import org .springframework .boot .test .context .TestConfiguration ;
17
32
import org .springframework .boot .test .mock .mockito .SpyBean ;
33
+ import org .springframework .context .annotation .Bean ;
18
34
import org .springframework .context .annotation .Import ;
19
35
import org .springframework .test .context .ActiveProfiles ;
20
36
import org .springframework .test .context .junit4 .SpringRunner ;
21
37
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
-
37
38
@ RunWith (SpringRunner .class )
38
39
@ SpringBootTest (classes = {DemoApp .class }, webEnvironment = NONE )
39
40
@ ActiveProfiles ({"disable-security" })
@@ -43,6 +44,7 @@ public class GRpcRecoveryTest extends GrpcServerTestBase {
43
44
static class CheckedException extends Exception {
44
45
45
46
}
47
+
46
48
static class CheckedException1 extends Exception {
47
49
48
50
}
@@ -59,16 +61,25 @@ static class Exception1 extends RuntimeException {
59
61
60
62
}
61
63
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
+
62
73
@ TestConfiguration
63
74
static class Cfg {
64
75
76
+ @ Bean
77
+ public BeanCustomErrorHandler otherErrorHandler () {
78
+ return new BeanCustomErrorHandler ();
79
+ }
65
80
66
81
@ GRpcServiceAdvice
67
82
static class CustomErrorHandler {
68
- @ GRpcExceptionHandler
69
- public Status handleA (ExceptionA e , GRpcExceptionScope scope ) {
70
- return Status .NOT_FOUND ;
71
- }
72
83
73
84
@ GRpcExceptionHandler
74
85
public Status handleB (ExceptionB e , GRpcExceptionScope scope ) {
@@ -87,7 +98,6 @@ public Status handle(Exception e, GRpcExceptionScope scope) {
87
98
}
88
99
}
89
100
90
-
91
101
@ GRpcService
92
102
static class CustomService extends CustomServiceGrpc .CustomServiceImplBase {
93
103
@@ -150,12 +160,14 @@ public Status handleB(ExceptionB e, GRpcExceptionScope scope) {
150
160
@ SpyBean
151
161
private Cfg .CustomErrorHandler handler ;
152
162
163
+ @ SpyBean
164
+ private BeanCustomErrorHandler beanErrorHandler ;
165
+
153
166
154
167
@ Test
155
168
public void streamingServiceErrorHandlerTest () throws ExecutionException , InterruptedException , TimeoutException {
156
169
157
170
158
-
159
171
final CompletableFuture <Throwable > errorFuture = new CompletableFuture <>();
160
172
final StreamObserver <Custom .CustomReply > reply = new StreamObserver <Custom .CustomReply >() {
161
173
@@ -180,15 +192,12 @@ public void onCompleted() {
180
192
requests .onCompleted ();
181
193
182
194
183
-
184
-
185
-
186
195
final Throwable actual = errorFuture .get (20 , TimeUnit .SECONDS );
187
196
assertThat (actual , notNullValue ());
188
197
assertThat (actual , isA (StatusRuntimeException .class ));
189
- assertThat (((StatusRuntimeException )actual ).getStatus (), is (Status .RESOURCE_EXHAUSTED ));
198
+ assertThat (((StatusRuntimeException ) actual ).getStatus (), is (Status .RESOURCE_EXHAUSTED ));
190
199
191
- Mockito .verify (srv ,times (1 )).handle (any (CheckedException1 .class ),any ());
200
+ Mockito .verify (srv , times (1 )).handle (any (CheckedException1 .class ), any ());
192
201
193
202
}
194
203
@@ -205,9 +214,8 @@ public void checkedExceptionHandlerTest() {
205
214
206
215
Mockito .verify (srv , never ()).handleB (any (), any ());
207
216
208
-
217
+ Mockito . verify ( beanErrorHandler , never ()). handleA ( any (), any ());
209
218
Mockito .verify (handler , never ()).handle (any (), any ());
210
- Mockito .verify (handler , never ()).handleA (any (), any ());
211
219
Mockito .verify (handler , times (1 )).handleCheckedException (any (CheckedException .class ), any ());
212
220
Mockito .verify (handler , never ()).handleB (any (), any ());
213
221
0 commit comments