Search in sources :

Example 21 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractTransportTest method earlyServerClose_noServerHeaders.

@Test
public void earlyServerClose_noServerHeaders() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
    clientStream.start(mockClientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    Status status = Status.OK.withDescription("Hellogoodbye").withCause(new Exception());
    Metadata trailers = new Metadata();
    trailers.put(asciiKey, "trailers");
    trailers.put(asciiKey, "dupvalue");
    trailers.put(asciiKey, "dupvalue");
    trailers.put(binaryKey, "äbinarytrailers");
    serverStream.close(status, trailers);
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture());
    assertCodeEquals(Status.OK, statusCaptor.getValue());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), metadataCaptor.capture());
    assertEquals(status.getCode(), statusCaptor.getValue().getCode());
    assertEquals("Hellogoodbye", statusCaptor.getValue().getDescription());
    // Cause should not be transmitted to the client.
    assertNull(statusCaptor.getValue().getCause());
    assertEquals(Lists.newArrayList(trailers.getAll(asciiKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(asciiKey)));
    assertEquals(Lists.newArrayList(trailers.getAll(binaryKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(binaryKey)));
}
Also used : Status(io.grpc.Status) ServerStreamListener(io.grpc.internal.ServerStreamListener) Metadata(io.grpc.Metadata) ServerStream(io.grpc.internal.ServerStream) ClientStream(io.grpc.internal.ClientStream) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 22 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractTransportTest method shutdownNowKillsServerStream.

@Test
public void shutdownNowKillsServerStream() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
    clientStream.start(mockClientStreamListener);
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    serverTransport.shutdownNow(Status.UNKNOWN.withDescription("test shutdownNow"));
    serverTransport = null;
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportShutdown(any(Status.class));
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportTerminated();
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
    assertTrue(serverTransportListener.waitForTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertTrue(serverTransportListener.isTerminated());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class), any(Metadata.class));
    // Generally will be same status provided to shutdownNow, but InProcessTransport can't
    // differentiate between client and server shutdownNow. The status is not really used on
    // server-side, so we don't care much.
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class));
}
Also used : Status(io.grpc.Status) ServerStreamListener(io.grpc.internal.ServerStreamListener) Metadata(io.grpc.Metadata) ClientStream(io.grpc.internal.ClientStream) Test(org.junit.Test)

Example 23 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractTransportTest method shutdownNowKillsClientStream.

@Test
public void shutdownNowKillsClientStream() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
    clientStream.start(mockClientStreamListener);
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    Status status = Status.UNKNOWN.withDescription("test shutdownNow");
    client.shutdownNow(status);
    client = null;
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportShutdown(any(Status.class));
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportTerminated();
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
    assertTrue(serverTransportListener.waitForTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertTrue(serverTransportListener.isTerminated());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(same(status), any(Metadata.class));
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class));
}
Also used : Status(io.grpc.Status) ServerStreamListener(io.grpc.internal.ServerStreamListener) Metadata(io.grpc.Metadata) ClientStream(io.grpc.internal.ClientStream) Test(org.junit.Test)

Example 24 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractTransportTest method clientCancelFromWithinMessageRead.

@Test
public void clientCancelFromWithinMessageRead() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    final SettableFuture<Boolean> closedCalled = SettableFuture.create();
    final ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
    clientStream.start(new ClientStreamListener() {

        @Override
        public void headersRead(Metadata headers) {
        }

        @Override
        public void closed(Status status, Metadata trailers) {
            assertEquals(Status.CANCELLED.getCode(), status.getCode());
            assertEquals("nevermind", status.getDescription());
            closedCalled.set(true);
        }

        @Override
        public void messageRead(InputStream message) {
            assertEquals("foo", methodDescriptor.parseResponse(message));
            clientStream.cancel(Status.CANCELLED.withDescription("nevermind"));
        }

        @Override
        public void onReady() {
        }
    });
    clientStream.halfClose();
    clientStream.request(1);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertEquals(methodDescriptor.getFullMethodName(), serverStreamCreation.method);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).onReady();
    assertTrue(serverStream.isReady());
    serverStream.writeHeaders(new Metadata());
    serverStream.writeMessage(methodDescriptor.streamRequest("foo"));
    serverStream.flush();
    // Block until closedCalled was set.
    closedCalled.get(5, TimeUnit.SECONDS);
    serverStream.close(Status.OK, new Metadata());
}
Also used : Status(io.grpc.Status) ClientStreamListener(io.grpc.internal.ClientStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ServerStreamListener(io.grpc.internal.ServerStreamListener) Metadata(io.grpc.Metadata) ServerStream(io.grpc.internal.ServerStream) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) ClientStream(io.grpc.internal.ClientStream) Test(org.junit.Test)

Example 25 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractTransportTest method doPingPong.

/**
   * Helper that simply does an RPC. It can be used similar to a sleep for negative testing: to give
   * time for actions _not_ to happen. Since it is based on doing an actual RPC with actual
   * callbacks, it generally provides plenty of time for Runnables to execute. But it is also faster
   * on faster machines and more reliable on slower machines.
   */
private void doPingPong(MockServerListener serverListener) throws InterruptedException {
    ManagedClientTransport client = newClientTransport(server);
    runIfNotNull(client.start(mock(ManagedClientTransport.Listener.class)));
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
    ClientStreamListener mockClientStreamListener = mock(ClientStreamListener.class);
    clientStream.start(mockClientStreamListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    serverStream.close(Status.OK, new Metadata());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class), any(Metadata.class));
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class));
    client.shutdown();
}
Also used : Status(io.grpc.Status) ManagedClientTransport(io.grpc.internal.ManagedClientTransport) ClientStreamListener(io.grpc.internal.ClientStreamListener) ServerStreamListener(io.grpc.internal.ServerStreamListener) Metadata(io.grpc.Metadata) ServerStream(io.grpc.internal.ServerStream) ClientStream(io.grpc.internal.ClientStream)

Aggregations

Metadata (io.grpc.Metadata)701 Test (org.junit.Test)559 Status (io.grpc.Status)190 CallOptions (io.grpc.CallOptions)56 ClientStreamTracer (io.grpc.ClientStreamTracer)51 ServerCall (io.grpc.ServerCall)48 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)44 InOrder (org.mockito.InOrder)41 Subchannel (io.grpc.LoadBalancer.Subchannel)40 ByteArrayInputStream (java.io.ByteArrayInputStream)40 InputStream (java.io.InputStream)38 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 IOException (java.io.IOException)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 Context (io.grpc.Context)31 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)31 InternalMetadata (io.grpc.InternalMetadata)30 MethodDescriptor (io.grpc.MethodDescriptor)30 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)29 ManagedChannel (io.grpc.ManagedChannel)27