Search in sources :

Example 1 with UrlResponseInfoImpl

use of org.chromium.net.impl.UrlResponseInfoImpl in project grpc-java by grpc.

the class CronetClientStreamTest method streamFailedAfterResponseHeaderReceived.

@Test
public void streamFailedAfterResponseHeaderReceived() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    // Receive response header
    UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
    callback.onResponseHeadersReceived(cronetStream, info);
    CronetException exception = mock(CronetException.class);
    callback.onFailed(cronetStream, info, exception);
    verify(transport).finishStream(eq(clientStream), isA(Status.class));
    // finishStream calls transportReportStatus.
    clientStream.transportState().transportReportStatus(Status.UNAVAILABLE, false, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(clientListener).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
    Status status = statusCaptor.getValue();
    assertEquals(Status.UNAVAILABLE.getCode(), status.getCode());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) UrlResponseInfo(org.chromium.net.UrlResponseInfo) CronetException(org.chromium.net.CronetException) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) Executor(java.util.concurrent.Executor) UrlResponseInfoImpl(org.chromium.net.impl.UrlResponseInfoImpl) RpcProgress(io.grpc.internal.ClientStreamListener.RpcProgress) Test(org.junit.Test)

Example 2 with UrlResponseInfoImpl

use of org.chromium.net.impl.UrlResponseInfoImpl in project grpc-java by grpc.

the class CronetClientStreamTest method reportTrailersWhenTrailersReceivedAfterReadClosed.

@Test
public void reportTrailersWhenTrailersReceivedAfterReadClosed() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    callback.onStreamReady(cronetStream);
    UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
    callback.onResponseHeadersReceived(cronetStream, info);
    // Receive cronet's endOfStream
    callback.onReadCompleted(cronetStream, null, ByteBuffer.allocate(0), true);
    verify(clientListener, times(0)).closed(isA(Status.class), isA(RpcProgress.class), isA(Metadata.class));
    // Receive trailer
    ((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(Status.UNAUTHENTICATED.getCode().value()));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(clientListener, times(1)).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
    Status status = statusCaptor.getValue();
    assertEquals(Status.UNAUTHENTICATED.getCode(), status.getCode());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) UrlResponseInfo(org.chromium.net.UrlResponseInfo) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) Executor(java.util.concurrent.Executor) UrlResponseInfoImpl(org.chromium.net.impl.UrlResponseInfoImpl) RpcProgress(io.grpc.internal.ClientStreamListener.RpcProgress) Test(org.junit.Test)

Example 3 with UrlResponseInfoImpl

use of org.chromium.net.impl.UrlResponseInfoImpl in project grpc-java by grpc.

the class CronetClientStreamTest method streamFailedAfterTrailerAndEndOfStreamReceived.

@Test
public void streamFailedAfterTrailerAndEndOfStreamReceived() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    // Receive response header
    UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
    callback.onResponseHeadersReceived(cronetStream, info);
    // Report trailer and endOfStream
    callback.onReadCompleted(cronetStream, null, ByteBuffer.allocate(0), true);
    ((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(0));
    CronetException exception = mock(CronetException.class);
    callback.onFailed(cronetStream, info, exception);
    verify(transport).finishStream(eq(clientStream), isA(Status.class));
    // finishStream calls transportReportStatus.
    clientStream.transportState().transportReportStatus(Status.UNAVAILABLE, false, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(clientListener).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
    Status status = statusCaptor.getValue();
    // Stream has already finished so OK status should be reported.
    assertEquals(Status.OK.getCode(), status.getCode());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) UrlResponseInfo(org.chromium.net.UrlResponseInfo) CronetException(org.chromium.net.CronetException) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) Executor(java.util.concurrent.Executor) UrlResponseInfoImpl(org.chromium.net.impl.UrlResponseInfoImpl) RpcProgress(io.grpc.internal.ClientStreamListener.RpcProgress) Test(org.junit.Test)

Example 4 with UrlResponseInfoImpl

use of org.chromium.net.impl.UrlResponseInfoImpl in project grpc-java by grpc.

the class CronetClientStreamTest method streamFailedAfterTrailerReceived.

@Test
public void streamFailedAfterTrailerReceived() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    // Receive response header
    UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
    callback.onResponseHeadersReceived(cronetStream, info);
    // Report trailer but not endOfStream.
    ((CronetClientStream.BidirectionalStreamCallback) callback).processTrailers(trailers(0));
    CronetException exception = mock(CronetException.class);
    callback.onFailed(cronetStream, info, exception);
    verify(transport).finishStream(eq(clientStream), isA(Status.class));
    // finishStream calls transportReportStatus.
    clientStream.transportState().transportReportStatus(Status.UNAVAILABLE, false, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(clientListener).closed(statusCaptor.capture(), isA(RpcProgress.class), isA(Metadata.class));
    Status status = statusCaptor.getValue();
    // Stream has already finished so OK status should be reported.
    assertEquals(Status.UNAVAILABLE.getCode(), status.getCode());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) UrlResponseInfo(org.chromium.net.UrlResponseInfo) CronetException(org.chromium.net.CronetException) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) Executor(java.util.concurrent.Executor) UrlResponseInfoImpl(org.chromium.net.impl.UrlResponseInfoImpl) RpcProgress(io.grpc.internal.ClientStreamListener.RpcProgress) Test(org.junit.Test)

Example 5 with UrlResponseInfoImpl

use of org.chromium.net.impl.UrlResponseInfoImpl in project grpc-java by grpc.

the class CronetClientStreamTest method read.

@Test
public void read() {
    ArgumentCaptor<BidirectionalStream.Callback> callbackCaptor = ArgumentCaptor.forClass(BidirectionalStream.Callback.class);
    verify(factory).newBidirectionalStreamBuilder(isA(String.class), callbackCaptor.capture(), isA(Executor.class));
    BidirectionalStream.Callback callback = callbackCaptor.getValue();
    // Read is not called until we receive the response header.
    verify(cronetStream, times(0)).read(isA(ByteBuffer.class));
    UrlResponseInfo info = new UrlResponseInfoImpl(new ArrayList<>(), 200, "", responseHeader("200"), false, "", "", 0);
    callback.onResponseHeadersReceived(cronetStream, info);
    verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    verify(clientListener).headersRead(metadataCaptor.capture());
    // Verify recevied headers.
    Metadata metadata = metadataCaptor.getValue();
    assertEquals("application/grpc", metadata.get(Metadata.Key.of("content-type", Metadata.ASCII_STRING_MARSHALLER)));
    assertEquals("test-value", metadata.get(Metadata.Key.of("test-key", Metadata.ASCII_STRING_MARSHALLER)));
    callback.onReadCompleted(cronetStream, info, createMessageFrame(new String("response1").getBytes(Charset.forName("UTF-8"))), false);
    // Haven't request any message, so no callback is called here.
    verify(clientListener, times(0)).messagesAvailable(isA(MessageProducer.class));
    verify(cronetStream, times(1)).read(isA(ByteBuffer.class));
    // Request one message
    clientStream.request(1);
    verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
    verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
    // BidirectionalStream.read will not be called again after receiving endOfStream(empty buffer).
    clientStream.request(1);
    callback.onReadCompleted(cronetStream, info, ByteBuffer.allocate(0), true);
    verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
    verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
}
Also used : Metadata(io.grpc.Metadata) UrlResponseInfo(org.chromium.net.UrlResponseInfo) ByteBuffer(java.nio.ByteBuffer) BidirectionalStream(org.chromium.net.BidirectionalStream) ExperimentalBidirectionalStream(org.chromium.net.ExperimentalBidirectionalStream) Executor(java.util.concurrent.Executor) UrlResponseInfoImpl(org.chromium.net.impl.UrlResponseInfoImpl) MessageProducer(io.grpc.internal.StreamListener.MessageProducer) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)8 Executor (java.util.concurrent.Executor)8 BidirectionalStream (org.chromium.net.BidirectionalStream)8 ExperimentalBidirectionalStream (org.chromium.net.ExperimentalBidirectionalStream)8 UrlResponseInfo (org.chromium.net.UrlResponseInfo)8 UrlResponseInfoImpl (org.chromium.net.impl.UrlResponseInfoImpl)8 Test (org.junit.Test)8 Status (io.grpc.Status)7 RpcProgress (io.grpc.internal.ClientStreamListener.RpcProgress)7 ByteBuffer (java.nio.ByteBuffer)3 CronetException (org.chromium.net.CronetException)3 MessageProducer (io.grpc.internal.StreamListener.MessageProducer)2 WritableBuffer (io.grpc.internal.WritableBuffer)1 Buffer (java.nio.Buffer)1