• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.grpc.functional.server;
18 
19 import static java.util.Arrays.asList;
20 
21 import com.google.common.collect.ConcurrentHashMultiset;
22 import com.google.common.collect.Multiset;
23 import com.google.protos.test.BaristaGrpc;
24 import dagger.Module;
25 import dagger.Provides;
26 import dagger.grpc.server.ForGrpcService;
27 import io.grpc.Metadata;
28 import io.grpc.ServerCall;
29 import io.grpc.ServerCall.Listener;
30 import io.grpc.ServerCallHandler;
31 import io.grpc.ServerInterceptor;
32 import java.util.List;
33 import javax.inject.Inject;
34 import javax.inject.Singleton;
35 
36 @Singleton
37 class CountingInterceptor implements ServerInterceptor {
38   private final Multiset<String> calls = ConcurrentHashMultiset.create();
39 
40   @Inject
CountingInterceptor()41   CountingInterceptor() {}
42 
43   @Override
interceptCall( ServerCall<RequestT, ResponseT> call, Metadata headers, ServerCallHandler<RequestT, ResponseT> next)44   public <RequestT, ResponseT> Listener<RequestT> interceptCall(
45       ServerCall<RequestT, ResponseT> call,
46       Metadata headers,
47       ServerCallHandler<RequestT, ResponseT> next) {
48     calls.add(call.getMethodDescriptor().getFullMethodName());
49     return next.startCall(call, headers);
50   }
51 
countCalls(String methodName)52   public int countCalls(String methodName) {
53     return calls.count(methodName);
54   }
55 
56   @Module
57   static class CountingInterceptorModule {
58     @Provides
59     @ForGrpcService(BaristaGrpc.class)
testServiceInterceptors( CountingInterceptor countingInterceptor)60     static List<? extends ServerInterceptor> testServiceInterceptors(
61         CountingInterceptor countingInterceptor) {
62       return asList(countingInterceptor);
63     }
64   }
65 }
66