Search in sources :

Example 96 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class OkHttpClientTransportTest method readStatus.

@Test
public void readStatus() throws Exception {
    initTransport();
    MockStreamListener listener = new MockStreamListener();
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    assertContainStream(3);
    frameHandler().headers(true, true, 3, 0, grpcResponseTrailers(), HeadersMode.HTTP_20_HEADERS);
    listener.waitUntilStreamClosed();
    assertEquals(Status.Code.OK, listener.status.getCode());
    shutdownAndVerify();
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 97 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class OkHttpClientTransportTest method stopNormally.

@Test
public void stopNormally() throws Exception {
    initTransport();
    MockStreamListener listener1 = new MockStreamListener();
    MockStreamListener listener2 = new MockStreamListener();
    OkHttpClientStream stream1 = clientTransport.newStream(method, new Metadata());
    stream1.start(listener1);
    OkHttpClientStream stream2 = clientTransport.newStream(method, new Metadata());
    stream2.start(listener2);
    assertEquals(2, activeStreamCount());
    clientTransport.shutdown();
    assertEquals(2, activeStreamCount());
    verify(transportListener).transportShutdown(isA(Status.class));
    stream1.cancel(Status.CANCELLED);
    stream2.cancel(Status.CANCELLED);
    listener1.waitUntilStreamClosed();
    listener2.waitUntilStreamClosed();
    assertEquals(0, activeStreamCount());
    assertEquals(Status.CANCELLED.getCode(), listener1.status.getCode());
    assertEquals(Status.CANCELLED.getCode(), listener2.status.getCode());
    verify(frameWriter, timeout(TIME_OUT_MS)).goAway(eq(0), eq(ErrorCode.NO_ERROR), (byte[]) any());
    verify(transportListener, timeout(TIME_OUT_MS)).transportTerminated();
    shutdownAndVerify();
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 98 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class OkHttpClientTransportTest method streamIdExhausted.

@Test
public void streamIdExhausted() throws Exception {
    int startId = Integer.MAX_VALUE - 2;
    initTransport(startId);
    MockStreamListener listener = new MockStreamListener();
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    stream.request(1);
    // New stream should be failed.
    assertNewStreamFail();
    // The alive stream should be functional, receives a message.
    frameHandler().headers(false, false, startId, 0, grpcResponseHeaders(), HeadersMode.HTTP_20_HEADERS);
    assertNotNull(listener.headers);
    String message = "hello";
    Buffer buffer = createMessageFrame(message);
    frameHandler().data(false, startId, buffer, (int) buffer.size());
    getStream(startId).cancel(Status.CANCELLED);
    // Receives the second message after be cancelled.
    buffer = createMessageFrame(message);
    frameHandler().data(false, startId, buffer, (int) buffer.size());
    listener.waitUntilStreamClosed();
    // Should only have the first message delivered.
    assertEquals(message, listener.messages.get(0));
    verify(frameWriter, timeout(TIME_OUT_MS)).rstStream(eq(startId), eq(ErrorCode.CANCEL));
    verify(transportListener).transportShutdown(isA(Status.class));
    verify(transportListener, timeout(TIME_OUT_MS)).transportTerminated();
    shutdownAndVerify();
}
Also used : Buffer(okio.Buffer) Status(io.grpc.Status) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 99 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class OkHttpClientTransportTest method pendingStreamCancelled.

@Test
public void pendingStreamCancelled() throws Exception {
    initTransport();
    setMaxConcurrentStreams(0);
    MockStreamListener listener = new MockStreamListener();
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    waitForStreamPending(1);
    stream.sendCancel(Status.CANCELLED);
    // The second cancel should be an no-op.
    stream.sendCancel(Status.UNKNOWN);
    listener.waitUntilStreamClosed();
    assertEquals(0, clientTransport.getPendingStreamSize());
    assertEquals(Status.CANCELLED.getCode(), listener.status.getCode());
    shutdownAndVerify();
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 100 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class OkHttpClientTransportTest method pendingStreamSucceedAfterShutdown.

@Test
public void pendingStreamSucceedAfterShutdown() throws Exception {
    initTransport();
    setMaxConcurrentStreams(0);
    final MockStreamListener listener = new MockStreamListener();
    // The second stream should be pending.
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    waitForStreamPending(1);
    clientTransport.shutdown();
    setMaxConcurrentStreams(1);
    verify(frameWriter, timeout(TIME_OUT_MS)).synStream(anyBoolean(), anyBoolean(), eq(3), anyInt(), anyListHeader());
    assertEquals(1, activeStreamCount());
    stream.sendCancel(Status.CANCELLED);
    shutdownAndVerify();
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)283 Test (org.junit.Test)229 Status (io.grpc.Status)78 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ClientStream (io.grpc.internal.ClientStream)20 InputStream (java.io.InputStream)19 Buffer (okio.Buffer)16 ClientCall (io.grpc.ClientCall)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 ServerStreamListener (io.grpc.internal.ServerStreamListener)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 ServerCall (io.grpc.ServerCall)12 StatsContext (com.google.instrumentation.stats.StatsContext)11 CallOptions (io.grpc.CallOptions)11 IOException (java.io.IOException)11 Context (io.grpc.Context)10 StatusRuntimeException (io.grpc.StatusRuntimeException)10 Matchers.anyString (org.mockito.Matchers.anyString)10 ServerStream (io.grpc.internal.ServerStream)9 ServiceDescriptor (io.grpc.ServiceDescriptor)8