Skip to content

Commit

Permalink
fixes #379
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmlet committed Sep 27, 2023
1 parent 28e4a41 commit 4c0f536
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,193 +31,183 @@
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DemoApp.class, TheConfiguration.class},
webEnvironment = WebEnvironment.NONE, properties = {"grpc.port=7778","grpc.shutdownGrace=-1"})
webEnvironment = WebEnvironment.NONE, properties = {"grpc.port=7778", "grpc.shutdownGrace=-1"})
@ActiveProfiles("disable-security")
public class OrderedInterceptorsTest extends GrpcServerTestBase{
public class OrderedInterceptorsTest extends GrpcServerTestBase {


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

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

@Before
public void setup() {

@Before
public void setup() {
calledInterceptors.clear();
}

calledInterceptors.clear();
}
@Override
protected GreeterGrpc.GreeterFutureStub beforeGreeting(GreeterGrpc.GreeterFutureStub stub) {
Assert.assertEquals(7778, runningPort);
Assert.assertEquals(getPort(), runningPort);
return stub;
}

@Override
protected GreeterGrpc.GreeterFutureStub beforeGreeting(GreeterGrpc.GreeterFutureStub stub) {
Assert.assertEquals(7778, runningPort);
Assert.assertEquals(getPort(), runningPort);
return stub;
}
@Override
protected void afterGreeting() {
assertThat(calledInterceptors).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 100,100);
}

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

@TestConfiguration
public static class TheConfiguration {
static class OrderAwareInterceptor implements ServerInterceptor {
private final int order;

public OrderAwareInterceptor(int order) {
this.order = order;
}

@TestConfiguration
public static class TheConfiguration {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(order);
return next.startCall(call, headers);
}
}

@Bean
@GRpcGlobalInterceptor
public ServerInterceptor mySixthInterceptor() {
return new MySixthInterceptor();
}

@Bean
@GRpcGlobalInterceptor
public ServerInterceptor mySixthInterceptor(){
return new MySixthInterceptor();
}
class MySixthInterceptor implements ServerInterceptor, Ordered {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(getOrder());
return next.startCall(call, headers);
}

@Override
public int getOrder() {
return 6;
}
}

class MySixthInterceptor implements ServerInterceptor,Ordered {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(getOrder());
return next.startCall(call, headers);
}

@Override
public int getOrder() {
return 6;
}
}
@GRpcGlobalInterceptor
@Order(2)
static class SecondInterceptor extends OrderAwareInterceptor {

@GRpcGlobalInterceptor
@Order(2)
static class SecondInterceptor implements ServerInterceptor {
public SecondInterceptor() {
super(2);
}

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(2);
return next.startCall(call, headers);
}
}
}

@GRpcGlobalInterceptor
@Order(4)
static class FourthInterceptor implements ServerInterceptor {
@GRpcGlobalInterceptor
@Order(4)
static class FourthInterceptor extends OrderAwareInterceptor {

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(4);
return next.startCall(call, headers);
}
}
public FourthInterceptor() {
super(4);
}

@GRpcGlobalInterceptor
@Order(3)
static class ThirdInterceptor implements ServerInterceptor {
}

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(3);
return next.startCall(call, headers);
}
}
@GRpcGlobalInterceptor
@Order(3)
static class ThirdInterceptor extends OrderAwareInterceptor {

@GRpcGlobalInterceptor
@Order(1)
static class FirstInterceptor implements ServerInterceptor {
public ThirdInterceptor() {
super(3);
}

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(1);
return next.startCall(call, headers);
}
}
}

@GRpcGlobalInterceptor
@Order // no value means lowest priority amongst all @Ordered, but higher priority than interceptors without the annotation
static class DefaultOrderedInterceptor implements ServerInterceptor {
@GRpcGlobalInterceptor
@Order(1)
static class FirstInterceptor extends OrderAwareInterceptor {

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(10);
return next.startCall(call, headers);
}
}
public FirstInterceptor() {
super(1);
}

// interceptors without any annotation will always be executed last, losing to any defined @Order
@GRpcGlobalInterceptor
static class UnorderedInterceptor implements ServerInterceptor {
}

@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(100);
return next.startCall(call, headers);
}
}
@GRpcGlobalInterceptor
@Order
// no value means lowest priority amongst all @Ordered, but higher priority than interceptors without the annotation
static class DefaultOrderedInterceptor extends OrderAwareInterceptor {

@Bean
@GRpcGlobalInterceptor
@Order(7)
public ServerInterceptor mySeventhInterceptor(){
return new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(7);
return next.startCall(call, headers);
public DefaultOrderedInterceptor() {
super(10);
}

}
};
}

@Bean
@GRpcGlobalInterceptor
@Order
public ServerInterceptor myOrderedMethodFactoryBeanInterceptor(){
return new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(10);
return next.startCall(call, headers);
// interceptors without any annotation will always be executed last, losing to any defined @Order
@GRpcGlobalInterceptor
static class UnorderedInterceptor extends OrderAwareInterceptor {

public UnorderedInterceptor() {
super(100);
}

}
};
}

@Bean
@GRpcGlobalInterceptor
public ServerInterceptor myInterceptor(){
return new MyInterceptor();
}
@Bean
@GRpcGlobalInterceptor
@Order(7)
public ServerInterceptor mySeventhInterceptor() {
return new OrderAwareInterceptor(7);
}

@Bean
@GRpcGlobalInterceptor
@Order
public ServerInterceptor myOrderedMethodFactoryBeanInterceptor() {
return new OrderAwareInterceptor(10);
}

class MyInterceptor implements ServerInterceptor,Ordered {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(5);
return next.startCall(call, headers);
}
@Bean
@GRpcGlobalInterceptor
public ServerInterceptor myUnOrderedMethodFactoryBeanInterceptor() {
return new OrderAwareInterceptor(100);
}

@Override
public int getOrder() {
return 5;
}
}
@Bean
@GRpcGlobalInterceptor
public ServerInterceptor myInterceptor() {
return new MyInterceptor();
}

class MyInterceptor implements ServerInterceptor, Ordered {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(5);
return next.startCall(call, headers);
}

@Override
public int getOrder() {
return 5;
}
}


@Bean
@Order(8)
@GRpcGlobalInterceptor
public ServerInterceptor my8thInterceptor(){
return new My8Interceptor();
}
@Bean
@Order(8)
@GRpcGlobalInterceptor
public ServerInterceptor my8thInterceptor() {
return new My8Interceptor();
}

static class My8Interceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(8);
return next.startCall(call, headers);
}
}
static class My8Interceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
calledInterceptors.add(8);
return next.startCall(call, headers);
}
}

}
}
}

0 comments on commit 4c0f536

Please sign in to comment.