Skip to content

Commit f27ce70

Browse files
committed
core, binder: notify clientCancelled directly in transportReportStatus and add binder cancel test
1 parent c6e2f4a commit f27ce70

2 files changed

Lines changed: 31 additions & 20 deletions

File tree

binder/src/androidTest/java/io/grpc/binder/internal/BinderClientTransportTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ public void testMessageProducerClosedAfterStream_b169313545() throws Exception {
292292
streamListener.drainMessages();
293293
}
294294

295+
@Test
296+
public void testCancelStream_notifiesTracer() throws Exception {
297+
transport = new BinderClientTransportBuilder().build();
298+
startAndAwaitReady(transport, transportListener);
299+
300+
ClientStreamTracer mockTracer = org.mockito.Mockito.mock(ClientStreamTracer.class);
301+
ClientStream stream =
302+
transport.newStream(methodDesc, new Metadata(), CallOptions.DEFAULT, new ClientStreamTracer[]{mockTracer});
303+
304+
stream.start(streamListener);
305+
Status cancelStatus = Status.CANCELLED.withDescription("Client cancelled");
306+
stream.cancel(cancelStatus);
307+
308+
org.mockito.Mockito.verify(mockTracer, org.mockito.Mockito.timeout(5000)).cancelled(org.mockito.ArgumentMatchers.eq(cancelStatus));
309+
}
310+
295311
@Test
296312
public void testNewStreamBeforeTransportReadyFails() throws Exception {
297313
// Use a special SecurityPolicy that lets us act before the transport is setup/ready.

core/src/main/java/io/grpc/internal/AbstractClientStream.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ public final void halfClose() {
197197
@Override
198198
public final void cancel(Status reason) {
199199
Preconditions.checkArgument(!reason.isOk(), "Should not cancel with OK status");
200-
if (cancelled || transportState().isListenerClosed()) {
201-
return;
202-
}
203200
cancelled = true;
204201
abstractClientStreamSink().cancel(reason);
205202
}
@@ -254,9 +251,7 @@ protected TransportState(
254251
}
255252
}
256253

257-
protected final boolean isListenerClosed() {
258-
return listenerClosed;
259-
}
254+
260255

261256
private void setFullStreamDecompression(boolean fullStreamDecompression) {
262257
this.fullStreamDecompression = fullStreamDecompression;
@@ -398,16 +393,16 @@ protected void inboundTrailersReceived(Metadata trailers, Status status) {
398393
* method must be called from the transport thread.
399394
*
400395
* @param status the new status to set
401-
* @param stopDelivery if {@code true}, interrupts any further delivery of inbound messages that
396+
* @param cancelled if {@code true}, interrupts any further delivery of inbound messages that
402397
* may already be queued up in the deframer. If {@code false}, the listener will be
403398
* notified immediately after all currently completed messages in the deframer have been
404399
* delivered to the application.
405400
* @param trailers new instance of {@code Trailers}, either empty or those returned by the
406401
* server
407402
*/
408-
public final void transportReportStatus(final Status status, boolean stopDelivery,
403+
public final void transportReportStatus(final Status status, boolean cancelled,
409404
final Metadata trailers) {
410-
transportReportStatus(status, RpcProgress.PROCESSED, stopDelivery, trailers);
405+
transportReportStatus(status, RpcProgress.PROCESSED, cancelled, trailers);
411406
}
412407

413408
/**
@@ -418,7 +413,7 @@ public final void transportReportStatus(final Status status, boolean stopDeliver
418413
* @param rpcProgress RPC progress that the
419414
* {@link ClientStreamListener#closed(Status, RpcProgress, Metadata)}
420415
* will receive
421-
* @param stopDelivery if {@code true}, interrupts any further delivery of inbound messages that
416+
* @param cancelled if {@code true}, interrupts any further delivery of inbound messages that
422417
* may already be queued up in the deframer and overrides any previously queued status.
423418
* If {@code false}, the listener will be notified immediately after all currently
424419
* completed messages in the deframer have been delivered to the application.
@@ -428,30 +423,33 @@ public final void transportReportStatus(final Status status, boolean stopDeliver
428423
public final void transportReportStatus(
429424
final Status status,
430425
final RpcProgress rpcProgress,
431-
boolean stopDelivery,
426+
boolean cancelled,
432427
final Metadata trailers) {
433428
checkNotNull(status, "status");
434429
checkNotNull(trailers, "trailers");
435-
// If stopDelivery, we continue in case previous invocation is waiting for stall
436-
if (statusReported && !stopDelivery) {
430+
// If cancelled, we continue in case previous invocation is waiting for stall
431+
if (statusReported && !cancelled) {
437432
return;
438433
}
439434
statusReported = true;
440435
statusReportedIsOk = status.isOk();
436+
if (cancelled) {
437+
statsTraceCtx.clientCancelled(status);
438+
}
441439
onStreamDeallocated();
442440

443441
if (deframerClosed) {
444442
deframerClosedTask = null;
445-
closeListener(status, rpcProgress, trailers, stopDelivery);
443+
closeListener(status, rpcProgress, trailers);
446444
} else {
447445
deframerClosedTask =
448446
new Runnable() {
449447
@Override
450448
public void run() {
451-
closeListener(status, rpcProgress, trailers, stopDelivery);
449+
closeListener(status, rpcProgress, trailers);
452450
}
453451
};
454-
closeDeframer(stopDelivery);
452+
closeDeframer(cancelled);
455453
}
456454
}
457455

@@ -461,12 +459,9 @@ public void run() {
461459
* @throws IllegalStateException if the call has not yet been started.
462460
*/
463461
private void closeListener(
464-
Status status, RpcProgress rpcProgress, Metadata trailers, boolean stopDelivery) {
462+
Status status, RpcProgress rpcProgress, Metadata trailers) {
465463
if (!listenerClosed) {
466464
listenerClosed = true;
467-
if (stopDelivery) {
468-
statsTraceCtx.clientCancelled(status);
469-
}
470465
statsTraceCtx.streamClosed(status);
471466
if (getTransportTracer() != null) {
472467
getTransportTracer().reportStreamClosed(status.isOk());

0 commit comments

Comments
 (0)