Java Code Examples for io.grpc.ClientCall#cancel()

The following examples show how to use io.grpc.ClientCall#cancel() . 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: ClientAuthInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyServiceUri() throws IOException {
  ClientCall<String, Integer> interceptedCall;

  doReturn("example.com:443").when(channel).authority();
  interceptedCall = interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  interceptedCall.start(listener, new Metadata());
  verify(credentials).getRequestMetadata(URI.create("https://example.com/a.service"));
  interceptedCall.cancel("Cancel for test", null);

  doReturn("example.com:123").when(channel).authority();
  interceptedCall = interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  interceptedCall.start(listener, new Metadata());
  verify(credentials).getRequestMetadata(URI.create("https://example.com:123/a.service"));
  interceptedCall.cancel("Cancel for test", null);
}
 
Example 2
Source File: ManagedChannelImplIdlenessTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void delayedTransportHoldsOffIdleness() throws Exception {
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  assertTrue(channel.inUseStateAggregator.isInUse());

  // As long as the delayed transport is in-use (by the pending RPC), the channel won't go idle.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
  assertTrue(channel.inUseStateAggregator.isInUse());

  // Cancelling the only RPC will reset the in-use state.
  assertEquals(0, executor.numPendingTasks());
  call.cancel("In test", null);
  assertEquals(1, executor.runDueTasks());
  assertFalse(channel.inUseStateAggregator.isInUse());
  // And allow the channel to go idle.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  timer.forwardTime(1, TimeUnit.SECONDS);
  verify(mockLoadBalancer).shutdown();
}
 
Example 3
Source File: ClientAuthInterceptorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyServiceUri() throws IOException {
  ClientCall<String, Integer> interceptedCall;

  doReturn("example.com:443").when(channel).authority();
  interceptedCall = interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  interceptedCall.start(listener, new Metadata());
  verify(credentials).getRequestMetadata(URI.create("https://example.com/a.service"));
  interceptedCall.cancel("Cancel for test", null);

  doReturn("example.com:123").when(channel).authority();
  interceptedCall = interceptor.interceptCall(descriptor, CallOptions.DEFAULT, channel);
  interceptedCall.start(listener, new Metadata());
  verify(credentials).getRequestMetadata(URI.create("https://example.com:123/a.service"));
  interceptedCall.cancel("Cancel for test", null);
}
 
Example 4
Source File: ManagedChannelImplIdlenessTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void delayedTransportHoldsOffIdleness() throws Exception {
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  assertTrue(channel.inUseStateAggregator.isInUse());

  // As long as the delayed transport is in-use (by the pending RPC), the channel won't go idle.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
  assertTrue(channel.inUseStateAggregator.isInUse());

  // Cancelling the only RPC will reset the in-use state.
  assertEquals(0, executor.numPendingTasks());
  call.cancel("In test", null);
  assertEquals(1, executor.runDueTasks());
  assertFalse(channel.inUseStateAggregator.isInUse());
  // And allow the channel to go idle.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  timer.forwardTime(1, TimeUnit.SECONDS);
  verify(mockLoadBalancer).shutdown();
}
 
Example 5
Source File: ManagedChannelImplIdlenessTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void newCallRefreshesIdlenessTimer() throws Exception {
  // First call to exit the initial idleness, then immediately cancel the call.
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  call.cancel("For testing", null);

  // Verify that we have exited the idle mode
  verify(mockLoadBalancerFactory).newLoadBalancer(any(Helper.class));
  assertFalse(channel.inUseStateAggregator.isInUse());

  // Move closer to idleness, but not yet.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // A new call would refresh the timer
  call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  call.cancel("For testing", null);
  assertFalse(channel.inUseStateAggregator.isInUse());

  // ... so that passing the same length of time will not trigger idle mode
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // ... until the time since last call has reached the timeout
  timer.forwardTime(1, TimeUnit.SECONDS);
  verify(mockLoadBalancer).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // Drain the app executor, which runs the call listeners
  verify(mockCallListener, never()).onClose(any(Status.class), any(Metadata.class));
  assertEquals(2, executor.runDueTasks());
  verify(mockCallListener, times(2)).onClose(any(Status.class), any(Metadata.class));
}
 
Example 6
Source File: MirrorConsensusTopicQuery.java    From hedera-sdk-java with Apache License 2.0 5 votes vote down vote up
public MirrorSubscriptionHandle subscribe(
    MirrorClient mirrorClient,
    Consumer<MirrorConsensusTopicResponse> onNext,
    Consumer<Throwable> onError)
{
    final ClientCall<ConsensusTopicQuery, ConsensusTopicResponse> call =
        mirrorClient.channel.newCall(ConsensusServiceGrpc.getSubscribeTopicMethod(), CallOptions.DEFAULT);

    final MirrorSubscriptionHandle subscriptionHandle = new MirrorSubscriptionHandle(() -> {
        call.cancel("unsubscribed", null);
    });

    ClientCalls.asyncServerStreamingCall(call, builder.build(), new StreamObserver<ConsensusTopicResponse>() {
        @Override
        public void onNext(ConsensusTopicResponse consensusTopicResponse) {
            onNext.accept(new MirrorConsensusTopicResponse(consensusTopicResponse));
        }

        @Override
        public void onError(Throwable throwable) {
            onError.accept(throwable);
        }

        @Override
        public void onCompleted() {
            // Do nothing
        }
    });

    return subscriptionHandle;
}
 
Example 7
Source File: ManagedChannelImplIdlenessTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void newCallRefreshesIdlenessTimer() throws Exception {
  // First call to exit the initial idleness, then immediately cancel the call.
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  call.cancel("For testing", null);

  // Verify that we have exited the idle mode
  verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class));
  assertFalse(channel.inUseStateAggregator.isInUse());

  // Move closer to idleness, but not yet.
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // A new call would refresh the timer
  call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  call.cancel("For testing", null);
  assertFalse(channel.inUseStateAggregator.isInUse());

  // ... so that passing the same length of time will not trigger idle mode
  timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
  verify(mockLoadBalancer, never()).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // ... until the time since last call has reached the timeout
  timer.forwardTime(1, TimeUnit.SECONDS);
  verify(mockLoadBalancer).shutdown();
  assertFalse(channel.inUseStateAggregator.isInUse());

  // Drain the app executor, which runs the call listeners
  verify(mockCallListener, never()).onClose(any(Status.class), any(Metadata.class));
  assertEquals(2, executor.runDueTasks());
  verify(mockCallListener, times(2)).onClose(any(Status.class), any(Metadata.class));
}