|
31 | 31 | */ |
32 | 32 | @RunWith(SpringRunner.class) |
33 | 33 | @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"}) |
35 | 35 | @ActiveProfiles("disable-security") |
36 | | -public class OrderedInterceptorsTest extends GrpcServerTestBase{ |
| 36 | +public class OrderedInterceptorsTest extends GrpcServerTestBase { |
37 | 37 |
|
38 | 38 |
|
| 39 | + private static List<Integer> calledInterceptors = new ArrayList<>(); |
39 | 40 |
|
40 | | - private static List<Integer> calledInterceptors = new ArrayList<>(); |
41 | 41 |
|
| 42 | + @Before |
| 43 | + public void setup() { |
42 | 44 |
|
43 | | - @Before |
44 | | - public void setup() { |
| 45 | + calledInterceptors.clear(); |
| 46 | + } |
45 | 47 |
|
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 | + } |
48 | 54 |
|
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 | + } |
55 | 59 |
|
56 | | - @Override |
57 | | - protected void afterGreeting() { |
58 | | - assertThat(calledInterceptors).containsExactly(1, 2, 3, 4,5,6, 7,8,10,10, 100); |
59 | | - } |
60 | 60 |
|
| 61 | + @TestConfiguration |
| 62 | + public static class TheConfiguration { |
| 63 | + static class OrderAwareInterceptor implements ServerInterceptor { |
| 64 | + private final int order; |
61 | 65 |
|
| 66 | + public OrderAwareInterceptor(int order) { |
| 67 | + this.order = order; |
| 68 | + } |
62 | 69 |
|
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 | + } |
65 | 76 |
|
| 77 | + @Bean |
| 78 | + @GRpcGlobalInterceptor |
| 79 | + public ServerInterceptor mySixthInterceptor() { |
| 80 | + return new MySixthInterceptor(); |
| 81 | + } |
66 | 82 |
|
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 | + } |
72 | 95 |
|
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 { |
85 | 99 |
|
86 | | - @GRpcGlobalInterceptor |
87 | | - @Order(2) |
88 | | - static class SecondInterceptor implements ServerInterceptor { |
| 100 | + public SecondInterceptor() { |
| 101 | + super(2); |
| 102 | + } |
89 | 103 |
|
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 | + } |
97 | 105 |
|
98 | | - @GRpcGlobalInterceptor |
99 | | - @Order(4) |
100 | | - static class FourthInterceptor implements ServerInterceptor { |
| 106 | + @GRpcGlobalInterceptor |
| 107 | + @Order(4) |
| 108 | + static class FourthInterceptor extends OrderAwareInterceptor { |
101 | 109 |
|
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 | + } |
109 | 113 |
|
110 | | - @GRpcGlobalInterceptor |
111 | | - @Order(3) |
112 | | - static class ThirdInterceptor implements ServerInterceptor { |
| 114 | + } |
113 | 115 |
|
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 { |
121 | 119 |
|
122 | | - @GRpcGlobalInterceptor |
123 | | - @Order(1) |
124 | | - static class FirstInterceptor implements ServerInterceptor { |
| 120 | + public ThirdInterceptor() { |
| 121 | + super(3); |
| 122 | + } |
125 | 123 |
|
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 | + } |
133 | 125 |
|
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 { |
137 | 129 |
|
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 | + } |
145 | 133 |
|
146 | | - // interceptors without any annotation will always be executed last, losing to any defined @Order |
147 | | - @GRpcGlobalInterceptor |
148 | | - static class UnorderedInterceptor implements ServerInterceptor { |
| 134 | + } |
149 | 135 |
|
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 { |
157 | 140 |
|
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 | + } |
167 | 144 |
|
168 | 145 | } |
169 | | - }; |
170 | | - } |
171 | 146 |
|
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 | + } |
181 | 154 |
|
182 | 155 | } |
183 | | - }; |
184 | | - } |
185 | 156 |
|
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 | + } |
191 | 170 |
|
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 | + } |
198 | 176 |
|
199 | | - @Override |
200 | | - public int getOrder() { |
201 | | - return 5; |
202 | | - } |
203 | | - } |
| 177 | + @Bean |
| 178 | + @GRpcGlobalInterceptor |
| 179 | + public ServerInterceptor myInterceptor() { |
| 180 | + return new MyInterceptor(); |
| 181 | + } |
204 | 182 |
|
| 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 | + } |
205 | 195 |
|
206 | 196 |
|
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 | + } |
213 | 203 |
|
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 | + } |
221 | 211 |
|
222 | | - } |
| 212 | + } |
223 | 213 | } |
0 commit comments