@Test public void interceptors() throws Exception { final LinkedList<Context> capturedContexts = new LinkedList<Context>(); final Context.Key<String> key1 = Context.key("key1"); final Context.Key<String> key2 = Context.key("key2"); final Context.Key<String> key3 = Context.key("key3"); ServerInterceptor intercepter1 = new ServerInterceptor() { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { Context ctx = Context.current().withValue(key1, "value1"); Context origCtx = ctx.attach(); try { capturedContexts.add(ctx); return next.startCall(call, headers); } finally { ctx.detach(origCtx); } } }; ServerInterceptor intercepter2 = new ServerInterceptor() { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { Context ctx = Context.current().withValue(key2, "value2"); Context origCtx = ctx.attach(); try { capturedContexts.add(ctx); return next.startCall(call, headers); } finally { ctx.detach(origCtx); } } }; ServerCallHandler<String, Integer> callHandler = new ServerCallHandler<String, Integer>() { @Override public ServerCall.Listener<String> startCall( ServerCall<String, Integer> call, Metadata headers) { capturedContexts.add(Context.current().withValue(key3, "value3")); return callListener; } }; mutableFallbackRegistry.addService( ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", METHOD)) .addMethod(METHOD, callHandler).build()); builder.intercept(intercepter2); builder.intercept(intercepter1); createServer(); server.start(); ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport()); transportListener.transportReady(Attributes.EMPTY); Metadata requestHeaders = new Metadata(); StatsTraceContext statsTraceCtx = StatsTraceContext.newServerContext(streamTracerFactories, "Waiter/serve", requestHeaders); when(stream.statsTraceContext()).thenReturn(statsTraceCtx); transportListener.streamCreated(stream, "Waiter/serve", requestHeaders); assertEquals(1, executor.runDueTasks()); Context ctx1 = capturedContexts.poll(); assertEquals("value1", key1.get(ctx1)); assertNull(key2.get(ctx1)); assertNull(key3.get(ctx1)); Context ctx2 = capturedContexts.poll(); assertEquals("value1", key1.get(ctx2)); assertEquals("value2", key2.get(ctx2)); assertNull(key3.get(ctx2)); Context ctx3 = capturedContexts.poll(); assertEquals("value1", key1.get(ctx3)); assertEquals("value2", key2.get(ctx3)); assertEquals("value3", key3.get(ctx3)); assertTrue(capturedContexts.isEmpty()); }
@Override public ServerImpl start() throws IOException { synchronized (lock) { checkState(!started, "Already started"); checkState(!shutdown, "Shutting down"); transportServer.start(new ServerListenerImpl()); executor = Preconditions.checkNotNull(executorPool.getObject(), "executor"); started = true; new Thread(registerRunnable, SERVER_REGISTRY_THREAD_NAME).start(); return this; } }
ServerImpl extends io.grpc.Server implements InternalInstrumented<ServerStats> { @Override public ServerImpl start() throws IOException { synchronized (lock) { checkState(!started, "Already started"); checkState(!shutdown, "Shutting down"); transportServer.start(new ServerListenerImpl()); executor = Preconditions.checkNotNull(executorPool.getObject(), "executor"); started = true; new Thread(registerRunnable, SERVER_REGISTRY_THREAD_NAME).start(); return this; } } }
ServerImpl extends io.grpc.Server implements InternalInstrumented<ServerStats> { @Override public ServerImpl start() throws IOException { synchronized (lock) { checkState(!started, "Already started"); checkState(!shutdown, "Shutting down"); transportServer.start(new ServerListenerImpl()); executor = Preconditions.checkNotNull(executorPool.getObject(), "executor"); started = true; new Thread(registerRunnable, SERVER_REGISTRY_THREAD_NAME).start(); return this; } } ServerImpl(
      AbstractServerImplBuilder<?> builder,
      InternalServer transportServer,
      Context rootContext); }
ServerImpl extends io.grpc.Server implements InternalInstrumented<ServerStats> { @Override public ServerImpl start() throws IOException { synchronized (lock) { checkState(!started, "Already started"); checkState(!shutdown, "Shutting down"); transportServer.start(new ServerListenerImpl()); executor = Preconditions.checkNotNull(executorPool.getObject(), "executor"); started = true; new Thread(registerRunnable, SERVER_REGISTRY_THREAD_NAME).start(); return this; } } ServerImpl(
      AbstractServerImplBuilder<?> builder,
      InternalServer transportServer,
      Context rootContext); @Override ServerImpl start(); @Override int getPort(); @Override List<ServerServiceDefinition> getServices(); @Override List<ServerServiceDefinition> getImmutableServices(); @Override List<ServerServiceDefinition> getMutableServices(); @Override ServerImpl shutdown(); @Override ServerImpl shutdownNow(); @Override boolean isShutdown(); @Override boolean awaitTermination(long timeout, TimeUnit unit); @Override void awaitTermination(); @Override boolean isTerminated(); @Override InternalLogId getLogId(); @Override ListenableFuture<ServerStats> getStats(); @Override HandlerRegistry getRegistry(); @Override String toString(); }
ServerImpl extends io.grpc.Server implements InternalInstrumented<ServerStats> { @Override public ServerImpl start() throws IOException { synchronized (lock) { checkState(!started, "Already started"); checkState(!shutdown, "Shutting down"); transportServer.start(new ServerListenerImpl()); executor = Preconditions.checkNotNull(executorPool.getObject(), "executor"); started = true; new Thread(registerRunnable, SERVER_REGISTRY_THREAD_NAME).start(); return this; } } ServerImpl(
      AbstractServerImplBuilder<?> builder,
      InternalServer transportServer,
      Context rootContext); @Override ServerImpl start(); @Override int getPort(); @Override List<ServerServiceDefinition> getServices(); @Override List<ServerServiceDefinition> getImmutableServices(); @Override List<ServerServiceDefinition> getMutableServices(); @Override ServerImpl shutdown(); @Override ServerImpl shutdownNow(); @Override boolean isShutdown(); @Override boolean awaitTermination(long timeout, TimeUnit unit); @Override void awaitTermination(); @Override boolean isTerminated(); @Override InternalLogId getLogId(); @Override ListenableFuture<ServerStats> getStats(); @Override HandlerRegistry getRegistry(); @Override String toString();  }