io.grpc.Context.CancellationListener Java Examples

The following examples show how to use io.grpc.Context.CancellationListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void watch(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  final ServerSideCall call = new ServerSideCall(request, responseObserver);
  Context.current().addListener(
      new CancellationListener() {
        @Override
        public void cancelled(Context ctx) {
          call.cancelled = true;
        }
      }, MoreExecutors.directExecutor());
  calls.add(call);
}
 
Example #2
Source File: OrcaOobUtilTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void streamCoreMetrics(
    OrcaLoadReportRequest request, StreamObserver<OrcaLoadReport> responseObserver) {
  final ServerSideCall call = new ServerSideCall(request, responseObserver);
  Context.current()
      .addListener(
          new CancellationListener() {
            @Override
            public void cancelled(Context ctx) {
              call.cancelled = true;
            }
          },
          MoreExecutors.directExecutor());
  calls.add(call);
}
 
Example #3
Source File: HealthCheckingLoadBalancerFactoryTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void watch(HealthCheckRequest request,
    StreamObserver<HealthCheckResponse> responseObserver) {
  final ServerSideCall call = new ServerSideCall(request, responseObserver);
  Context.current().addListener(
      new CancellationListener() {
        @Override
        public void cancelled(Context ctx) {
          call.cancelled = true;
        }
      }, MoreExecutors.directExecutor());
  calls.add(call);
}
 
Example #4
Source File: LoadReportClientTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
  MockitoAnnotations.initMocks(this);
  mockLoadReportingService = mock(LoadReportingServiceGrpc.LoadReportingServiceImplBase.class,
      delegatesTo(
          new LoadReportingServiceGrpc.LoadReportingServiceImplBase() {
            @Override
            public StreamObserver<LoadStatsRequest> streamLoadStats(
                final StreamObserver<LoadStatsResponse> responseObserver) {
              assertThat(callEnded.get()).isTrue();  // ensure previous call was ended
              callEnded.set(false);
              Context.current().addListener(
                  new CancellationListener() {
                    @Override
                    public void cancelled(Context context) {
                      callEnded.set(true);
                    }
                  }, MoreExecutors.directExecutor());
              StreamObserver<LoadStatsRequest> requestObserver =
                  mock(StreamObserver.class);
              lrsRequestObservers.add(requestObserver);
              return requestObserver;
            }
          }
      ));
  cleanupRule.register(InProcessServerBuilder.forName("fakeLoadReportingServer").directExecutor()
      .addService(mockLoadReportingService).build().start());
  channel = cleanupRule.register(
      InProcessChannelBuilder.forName("fakeLoadReportingServer").directExecutor().build());
  when(backoffPolicyProvider.get()).thenReturn(backoffPolicy1, backoffPolicy2);
  when(backoffPolicy1.nextBackoffNanos())
      .thenReturn(TimeUnit.SECONDS.toNanos(1L), TimeUnit.SECONDS.toNanos(10L));
  when(backoffPolicy2.nextBackoffNanos())
      .thenReturn(TimeUnit.SECONDS.toNanos(1L), TimeUnit.SECONDS.toNanos(10L));
  lrsClient =
      new LoadReportClient(
          logId,
          TARGET_NAME,
          channel,
          NODE,
          syncContext,
          fakeClock.getScheduledExecutorService(),
          backoffPolicyProvider,
          fakeClock.getStopwatchSupplier());
  lrsClient.startLoadReporting(callback);
}