Skip to content

Commit 4c0f536

Browse files
committed
fixes #379
1 parent 28e4a41 commit 4c0f536

File tree

2 files changed

+161
-162
lines changed

2 files changed

+161
-162
lines changed

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

Lines changed: 138 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -31,193 +31,183 @@
3131
*/
3232
@RunWith(SpringRunner.class)
3333
@SpringBootTest(classes = {DemoApp.class, TheConfiguration.class},
34-
webEnvironment = WebEnvironment.NONE, properties = {"grpc.port=7778","grpc.shutdownGrace=-1"})
34+
webEnvironment = WebEnvironment.NONE, properties = {"grpc.port=7778", "grpc.shutdownGrace=-1"})
3535
@ActiveProfiles("disable-security")
36-
public class OrderedInterceptorsTest extends GrpcServerTestBase{
36+
public class OrderedInterceptorsTest extends GrpcServerTestBase {
3737

3838

39+
private static List<Integer> calledInterceptors = new ArrayList<>();
3940

40-
private static List<Integer> calledInterceptors = new ArrayList<>();
4141

42+
@Before
43+
public void setup() {
4244

43-
@Before
44-
public void setup() {
45+
calledInterceptors.clear();
46+
}
4547

46-
calledInterceptors.clear();
47-
}
48+
@Override
49+
protected GreeterGrpc.GreeterFutureStub beforeGreeting(GreeterGrpc.GreeterFutureStub stub) {
50+
Assert.assertEquals(7778, runningPort);
51+
Assert.assertEquals(getPort(), runningPort);
52+
return stub;
53+
}
4854

49-
@Override
50-
protected GreeterGrpc.GreeterFutureStub beforeGreeting(GreeterGrpc.GreeterFutureStub stub) {
51-
Assert.assertEquals(7778, runningPort);
52-
Assert.assertEquals(getPort(), runningPort);
53-
return stub;
54-
}
55+
@Override
56+
protected void afterGreeting() {
57+
assertThat(calledInterceptors).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 100,100);
58+
}
5559

56-
@Override
57-
protected void afterGreeting() {
58-
assertThat(calledInterceptors).containsExactly(1, 2, 3, 4,5,6, 7,8,10,10, 100);
59-
}
6060

61+
@TestConfiguration
62+
public static class TheConfiguration {
63+
static class OrderAwareInterceptor implements ServerInterceptor {
64+
private final int order;
6165

66+
public OrderAwareInterceptor(int order) {
67+
this.order = order;
68+
}
6269

63-
@TestConfiguration
64-
public static class TheConfiguration {
70+
@Override
71+
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
72+
calledInterceptors.add(order);
73+
return next.startCall(call, headers);
74+
}
75+
}
6576

77+
@Bean
78+
@GRpcGlobalInterceptor
79+
public ServerInterceptor mySixthInterceptor() {
80+
return new MySixthInterceptor();
81+
}
6682

67-
@Bean
68-
@GRpcGlobalInterceptor
69-
public ServerInterceptor mySixthInterceptor(){
70-
return new MySixthInterceptor();
71-
}
83+
class MySixthInterceptor implements ServerInterceptor, Ordered {
84+
@Override
85+
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
86+
calledInterceptors.add(getOrder());
87+
return next.startCall(call, headers);
88+
}
89+
90+
@Override
91+
public int getOrder() {
92+
return 6;
93+
}
94+
}
7295

73-
class MySixthInterceptor implements ServerInterceptor,Ordered {
74-
@Override
75-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
76-
calledInterceptors.add(getOrder());
77-
return next.startCall(call, headers);
78-
}
79-
80-
@Override
81-
public int getOrder() {
82-
return 6;
83-
}
84-
}
96+
@GRpcGlobalInterceptor
97+
@Order(2)
98+
static class SecondInterceptor extends OrderAwareInterceptor {
8599

86-
@GRpcGlobalInterceptor
87-
@Order(2)
88-
static class SecondInterceptor implements ServerInterceptor {
100+
public SecondInterceptor() {
101+
super(2);
102+
}
89103

90-
@Override
91-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
92-
ServerCallHandler<ReqT, RespT> next) {
93-
calledInterceptors.add(2);
94-
return next.startCall(call, headers);
95-
}
96-
}
104+
}
97105

98-
@GRpcGlobalInterceptor
99-
@Order(4)
100-
static class FourthInterceptor implements ServerInterceptor {
106+
@GRpcGlobalInterceptor
107+
@Order(4)
108+
static class FourthInterceptor extends OrderAwareInterceptor {
101109

102-
@Override
103-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
104-
ServerCallHandler<ReqT, RespT> next) {
105-
calledInterceptors.add(4);
106-
return next.startCall(call, headers);
107-
}
108-
}
110+
public FourthInterceptor() {
111+
super(4);
112+
}
109113

110-
@GRpcGlobalInterceptor
111-
@Order(3)
112-
static class ThirdInterceptor implements ServerInterceptor {
114+
}
113115

114-
@Override
115-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
116-
ServerCallHandler<ReqT, RespT> next) {
117-
calledInterceptors.add(3);
118-
return next.startCall(call, headers);
119-
}
120-
}
116+
@GRpcGlobalInterceptor
117+
@Order(3)
118+
static class ThirdInterceptor extends OrderAwareInterceptor {
121119

122-
@GRpcGlobalInterceptor
123-
@Order(1)
124-
static class FirstInterceptor implements ServerInterceptor {
120+
public ThirdInterceptor() {
121+
super(3);
122+
}
125123

126-
@Override
127-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
128-
ServerCallHandler<ReqT, RespT> next) {
129-
calledInterceptors.add(1);
130-
return next.startCall(call, headers);
131-
}
132-
}
124+
}
133125

134-
@GRpcGlobalInterceptor
135-
@Order // no value means lowest priority amongst all @Ordered, but higher priority than interceptors without the annotation
136-
static class DefaultOrderedInterceptor implements ServerInterceptor {
126+
@GRpcGlobalInterceptor
127+
@Order(1)
128+
static class FirstInterceptor extends OrderAwareInterceptor {
137129

138-
@Override
139-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
140-
ServerCallHandler<ReqT, RespT> next) {
141-
calledInterceptors.add(10);
142-
return next.startCall(call, headers);
143-
}
144-
}
130+
public FirstInterceptor() {
131+
super(1);
132+
}
145133

146-
// interceptors without any annotation will always be executed last, losing to any defined @Order
147-
@GRpcGlobalInterceptor
148-
static class UnorderedInterceptor implements ServerInterceptor {
134+
}
149135

150-
@Override
151-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
152-
ServerCallHandler<ReqT, RespT> next) {
153-
calledInterceptors.add(100);
154-
return next.startCall(call, headers);
155-
}
156-
}
136+
@GRpcGlobalInterceptor
137+
@Order
138+
// no value means lowest priority amongst all @Ordered, but higher priority than interceptors without the annotation
139+
static class DefaultOrderedInterceptor extends OrderAwareInterceptor {
157140

158-
@Bean
159-
@GRpcGlobalInterceptor
160-
@Order(7)
161-
public ServerInterceptor mySeventhInterceptor(){
162-
return new ServerInterceptor() {
163-
@Override
164-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
165-
calledInterceptors.add(7);
166-
return next.startCall(call, headers);
141+
public DefaultOrderedInterceptor() {
142+
super(10);
143+
}
167144

168145
}
169-
};
170-
}
171146

172-
@Bean
173-
@GRpcGlobalInterceptor
174-
@Order
175-
public ServerInterceptor myOrderedMethodFactoryBeanInterceptor(){
176-
return new ServerInterceptor() {
177-
@Override
178-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
179-
calledInterceptors.add(10);
180-
return next.startCall(call, headers);
147+
// interceptors without any annotation will always be executed last, losing to any defined @Order
148+
@GRpcGlobalInterceptor
149+
static class UnorderedInterceptor extends OrderAwareInterceptor {
150+
151+
public UnorderedInterceptor() {
152+
super(100);
153+
}
181154

182155
}
183-
};
184-
}
185156

186-
@Bean
187-
@GRpcGlobalInterceptor
188-
public ServerInterceptor myInterceptor(){
189-
return new MyInterceptor();
190-
}
157+
@Bean
158+
@GRpcGlobalInterceptor
159+
@Order(7)
160+
public ServerInterceptor mySeventhInterceptor() {
161+
return new OrderAwareInterceptor(7);
162+
}
163+
164+
@Bean
165+
@GRpcGlobalInterceptor
166+
@Order
167+
public ServerInterceptor myOrderedMethodFactoryBeanInterceptor() {
168+
return new OrderAwareInterceptor(10);
169+
}
191170

192-
class MyInterceptor implements ServerInterceptor,Ordered {
193-
@Override
194-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
195-
calledInterceptors.add(5);
196-
return next.startCall(call, headers);
197-
}
171+
@Bean
172+
@GRpcGlobalInterceptor
173+
public ServerInterceptor myUnOrderedMethodFactoryBeanInterceptor() {
174+
return new OrderAwareInterceptor(100);
175+
}
198176

199-
@Override
200-
public int getOrder() {
201-
return 5;
202-
}
203-
}
177+
@Bean
178+
@GRpcGlobalInterceptor
179+
public ServerInterceptor myInterceptor() {
180+
return new MyInterceptor();
181+
}
204182

183+
class MyInterceptor implements ServerInterceptor, Ordered {
184+
@Override
185+
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
186+
calledInterceptors.add(5);
187+
return next.startCall(call, headers);
188+
}
189+
190+
@Override
191+
public int getOrder() {
192+
return 5;
193+
}
194+
}
205195

206196

207-
@Bean
208-
@Order(8)
209-
@GRpcGlobalInterceptor
210-
public ServerInterceptor my8thInterceptor(){
211-
return new My8Interceptor();
212-
}
197+
@Bean
198+
@Order(8)
199+
@GRpcGlobalInterceptor
200+
public ServerInterceptor my8thInterceptor() {
201+
return new My8Interceptor();
202+
}
213203

214-
static class My8Interceptor implements ServerInterceptor {
215-
@Override
216-
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
217-
calledInterceptors.add(8);
218-
return next.startCall(call, headers);
219-
}
220-
}
204+
static class My8Interceptor implements ServerInterceptor {
205+
@Override
206+
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
207+
calledInterceptors.add(8);
208+
return next.startCall(call, headers);
209+
}
210+
}
221211

222-
}
212+
}
223213
}

0 commit comments

Comments
 (0)