Search in sources :

Example 76 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);
    startTransport(client, mockClientTransportListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
    clientStream.start(clientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;
    Status strippedStatus = Status.OK.withDescription("Hellogoodbye");
    Status status = strippedStatus.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);
    assertCodeEquals(Status.OK, serverStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    Status clientStreamStatus = clientStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    Metadata clientStreamTrailers = clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    checkClientStatus(status, clientStreamStatus);
    assertEquals(Lists.newArrayList(trailers.getAll(asciiKey)), Lists.newArrayList(clientStreamTrailers.getAll(asciiKey)));
    assertEquals(Lists.newArrayList(trailers.getAll(binaryKey)), Lists.newArrayList(clientStreamTrailers.getAll(binaryKey)));
    assertTrue(clientStreamTracer1.getOutboundHeaders());
    assertSame(clientStreamTrailers, clientStreamTracer1.getInboundTrailers());
    assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
    assertSame(status, serverStreamTracer1.getStatus());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 77 with Metadata

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

the class AbstractTransportTest method earlyServerClose_serverFailure_withClientCancelOnListenerClosed.

@Test
public void earlyServerClose_serverFailure_withClientCancelOnListenerClosed() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    final ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase() {

        @Override
        public void closed(Status status, RpcProgress rpcProgress, Metadata trailers) {
            super.closed(status, rpcProgress, trailers);
            // This simulates the blocking calls which can trigger clientStream.cancel().
            clientStream.cancel(Status.CANCELLED.withCause(status.asRuntimeException()));
        }
    };
    clientStream.start(clientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;
    Status strippedStatus = Status.INTERNAL.withDescription("I'm not listening");
    Status status = strippedStatus.withCause(new Exception());
    serverStream.close(status, new Metadata());
    assertCodeEquals(Status.OK, serverStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    Status clientStreamStatus = clientStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    Metadata clientStreamTrailers = clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertNotNull(clientStreamTrailers);
    checkClientStatus(status, clientStreamStatus);
    assertTrue(clientStreamTracer1.getOutboundHeaders());
    assertSame(clientStreamTrailers, clientStreamTracer1.getInboundTrailers());
    assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
    assertSame(status, serverStreamTracer1.getStatus());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 78 with Metadata

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

the class AbstractTransportTest method socketStats.

@Test
public void socketStats() throws Exception {
    server.start(serverListener);
    ManagedClientTransport client = newClientTransport(server);
    startTransport(client, mockClientTransportListener);
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
    clientStream.start(clientStreamListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    SocketAddress serverAddress = clientStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
    SocketAddress clientAddress = serverStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
    SocketStats clientSocketStats = client.getStats().get();
    assertEquals("clientLocal " + clientStream.getAttributes(), clientAddress, clientSocketStats.local);
    assertEquals("clientRemote " + clientStream.getAttributes(), serverAddress, clientSocketStats.remote);
    // very basic sanity check that socket options are populated
    assertNotNull(clientSocketStats.socketOptions.lingerSeconds);
    assertTrue(clientSocketStats.socketOptions.others.containsKey("SO_SNDBUF"));
    SocketStats serverSocketStats = serverTransportListener.transport.getStats().get();
    assertEquals("serverLocal " + serverStream.getAttributes(), serverAddress, serverSocketStats.local);
    assertEquals("serverRemote " + serverStream.getAttributes(), clientAddress, serverSocketStats.remote);
    // very basic sanity check that socket options are populated
    assertNotNull(serverSocketStats.socketOptions.lingerSeconds);
    assertTrue(serverSocketStats.socketOptions.others.containsKey("SO_SNDBUF"));
}
Also used : Metadata(io.grpc.Metadata) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SocketStats(io.grpc.InternalChannelz.SocketStats) Test(org.junit.Test)

Example 79 with Metadata

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

the class AbstractTransportTest method zeroMessageStream.

@Test
public void zeroMessageStream() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    startTransport(client, mockClientTransportListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
    clientStream.start(clientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;
    clientStream.halfClose();
    assertTrue(serverStreamListener.awaitHalfClosed(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    serverStream.writeHeaders(new Metadata());
    assertNotNull(clientStreamListener.headers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    Status status = Status.OK.withDescription("Nice talking to you");
    serverStream.close(status, new Metadata());
    assertCodeEquals(Status.OK, serverStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    Status clientStreamStatus = clientStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    Metadata clientStreamTrailers = clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertNotNull(clientStreamTrailers);
    assertEquals(status.getCode(), clientStreamStatus.getCode());
    assertEquals(status.getDescription(), clientStreamStatus.getDescription());
    assertTrue(clientStreamTracer1.getOutboundHeaders());
    assertTrue(clientStreamTracer1.getInboundHeaders());
    assertSame(clientStreamTrailers, clientStreamTracer1.getInboundTrailers());
    assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
    assertSame(status, serverStreamTracer1.getStatus());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 80 with Metadata

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

the class AbstractTransportTest method clientCancel.

@Test
public void clientCancel() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    startTransport(client, mockClientTransportListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, tracers);
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
    clientStream.start(clientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;
    Status status = Status.CANCELLED.withDescription("Nevermind").withCause(new Exception());
    clientStream.cancel(status);
    assertEquals(status, clientStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    Status serverStatus = serverStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertNotEquals(Status.Code.OK, serverStatus.getCode());
    // Cause should not be transmitted between client and server by default
    assertNull(serverStatus.getCause());
    clientStream.cancel(status);
    assertTrue(clientStreamTracer1.getOutboundHeaders());
    assertNull(clientStreamTracer1.getInboundTrailers());
    assertSame(status, clientStreamTracer1.getStatus());
    assertSame(serverStatus, serverStreamTracer1.getStatus());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

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