Search in sources :

Example 26 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);
    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;
    clientStream.halfClose();
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
    serverStream.writeHeaders(new Metadata());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(any(Metadata.class));
    Status status = Status.OK.withDescription("Nice talking to you");
    serverStream.close(status, new Metadata());
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture());
    assertCodeEquals(Status.OK, statusCaptor.getValue());
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
    assertEquals(status.getCode(), statusCaptor.getValue().getCode());
    assertEquals(status.getDescription(), statusCaptor.getValue().getDescription());
}
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) Test(org.junit.Test)

Example 27 with Metadata

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

the class AbstractTransportTest method serverCancel.

@Test
public void serverCancel() 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.DEADLINE_EXCEEDED.withDescription("It was bound to happen").withCause(new Exception());
    serverStream.cancel(status);
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(Matchers.same(status));
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
    // Presently we can't sent much back to the client in this case. Verify that is the current
    // behavior for consistency between transports.
    assertCodeEquals(Status.CANCELLED, statusCaptor.getValue());
    // Cause should not be transmitted between server and client
    assertNull(statusCaptor.getValue().getCause());
    // Second cancellation shouldn't trigger additional callbacks
    reset(mockServerStreamListener);
    reset(mockClientStreamListener);
    serverStream.cancel(status);
    doPingPong(serverListener);
    verify(mockServerStreamListener, never()).closed(any(Status.class));
    verify(mockClientStreamListener, never()).closed(any(Status.class), any(Metadata.class));
}
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 28 with Metadata

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

the class AbstractTransportTest method transportInUse_clientCancel.

@Test
public void transportInUse_clientCancel() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    ClientStream stream1 = client.newStream(methodDescriptor, new Metadata());
    stream1.start(mockClientStreamListener);
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
    ClientStream stream2 = client.newStream(methodDescriptor, new Metadata());
    stream2.start(mockClientStreamListener);
    stream1.cancel(Status.CANCELLED);
    verify(mockClientTransportListener, never()).transportInUse(false);
    stream2.cancel(Status.CANCELLED);
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
    // Verify that the callback has been called only once for true and false respectively
    verify(mockClientTransportListener).transportInUse(true);
    verify(mockClientTransportListener).transportInUse(false);
}
Also used : Metadata(io.grpc.Metadata) ClientStream(io.grpc.internal.ClientStream) Test(org.junit.Test)

Example 29 with Metadata

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

the class AbstractTransportTest method openStreamPreventsTermination.

@Test
public void openStreamPreventsTermination() 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);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    client.shutdown();
    client = null;
    server.shutdown();
    serverTransport.shutdown();
    serverTransport = null;
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportShutdown(any(Status.class));
    assertTrue(serverListener.waitForShutdown(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    // A new server should be able to start listening, since the current server has given up
    // resources. There may be cases this is impossible in the future, but for now it is a useful
    // property.
    serverListener = new MockServerListener();
    server = newServer(server);
    server.start(serverListener);
    // Try to "flush" out any listener notifications on client and server. This also ensures that
    // the stream still functions.
    serverStream.writeHeaders(new Metadata());
    clientStream.halfClose();
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(any(Metadata.class));
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
    verify(mockClientTransportListener, never()).transportTerminated();
    verify(mockClientTransportListener, never()).transportInUse(false);
    assertFalse(serverTransportListener.isTerminated());
    clientStream.cancel(Status.CANCELLED);
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportTerminated();
    verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
    assertTrue(serverTransportListener.waitForTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS));
}
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) Test(org.junit.Test)

Example 30 with Metadata

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

the class AbstractTransportTest method basicStream.

@Test
@SuppressWarnings("deprecation")
public void basicStream() throws Exception {
    server.start(serverListener);
    client = newClientTransport(server);
    runIfNotNull(client.start(mockClientTransportListener));
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    serverTransport = serverTransportListener.transport;
    Metadata clientHeaders = new Metadata();
    clientHeaders.put(asciiKey, "client");
    clientHeaders.put(asciiKey, "dupvalue");
    clientHeaders.put(asciiKey, "dupvalue");
    clientHeaders.put(binaryKey, "äbinaryclient");
    Metadata clientHeadersCopy = new Metadata();
    clientHeadersCopy.merge(clientHeaders);
    ClientStream clientStream = client.newStream(methodDescriptor, clientHeaders);
    clientStream.start(mockClientStreamListener);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    assertEquals(methodDescriptor.getFullMethodName(), serverStreamCreation.method);
    assertEquals(Lists.newArrayList(clientHeadersCopy.getAll(asciiKey)), Lists.newArrayList(serverStreamCreation.headers.getAll(asciiKey)));
    assertEquals(Lists.newArrayList(clientHeadersCopy.getAll(binaryKey)), Lists.newArrayList(serverStreamCreation.headers.getAll(binaryKey)));
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
    assertEquals("additional attribute value", serverStream.getAttributes().get(ADDITIONAL_TRANSPORT_ATTR_KEY));
    assertNotNull(serverStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
    serverStream.request(1);
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).onReady();
    assertTrue(clientStream.isReady());
    clientStream.writeMessage(methodDescriptor.streamRequest("Hello!"));
    clientStream.flush();
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).messageRead(inputStreamCaptor.capture());
    assertEquals("Hello!", methodDescriptor.parseRequest(inputStreamCaptor.getValue()));
    inputStreamCaptor.getValue().close();
    clientStream.halfClose();
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
    Metadata serverHeaders = new Metadata();
    serverHeaders.put(asciiKey, "server");
    serverHeaders.put(asciiKey, "dupvalue");
    serverHeaders.put(asciiKey, "dupvalue");
    serverHeaders.put(binaryKey, "äbinaryserver");
    Metadata serverHeadersCopy = new Metadata();
    serverHeadersCopy.merge(serverHeaders);
    serverStream.writeHeaders(serverHeaders);
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(metadataCaptor.capture());
    assertEquals(Lists.newArrayList(serverHeadersCopy.getAll(asciiKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(asciiKey)));
    assertEquals(Lists.newArrayList(serverHeadersCopy.getAll(binaryKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(binaryKey)));
    clientStream.request(1);
    verify(mockServerStreamListener, timeout(TIMEOUT_MS)).onReady();
    assertTrue(serverStream.isReady());
    serverStream.writeMessage(methodDescriptor.streamResponse("Hi. Who are you?"));
    serverStream.flush();
    verify(mockClientStreamListener, timeout(TIMEOUT_MS)).messageRead(inputStreamCaptor.capture());
    assertEquals("Hi. Who are you?", methodDescriptor.parseResponse(inputStreamCaptor.getValue()));
    inputStreamCaptor.getValue().close();
    Status status = Status.OK.withDescription("That was normal");
    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(status.getDescription(), statusCaptor.getValue().getDescription());
    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) 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