Java Code Examples for io.grpc.internal.ServerStream#writeHeaders()

The following examples show how to use io.grpc.internal.ServerStream#writeHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyClientTransportTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
public ServerTransportListener transportCreated(final ServerTransport transport) {
  transports.add((NettyServerTransport) transport);
  return new ServerTransportListener() {
    @Override
    public void streamCreated(ServerStream stream, String method, Metadata headers) {
      EchoServerStreamListener listener = new EchoServerStreamListener(stream, method, headers);
      stream.setListener(listener);
      stream.writeHeaders(new Metadata());
      stream.request(1);
      streamListeners.add(listener);
    }

    @Override
    public Attributes transportReady(Attributes transportAttrs) {
      serverTransportAttributesList.add(transportAttrs);
      return transportAttrs;
    }

    @Override
    public void transportTerminated() {}
  };
}
 
Example 2
Source File: NettyClientTransportTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public ServerTransportListener transportCreated(final ServerTransport transport) {
  transports.add((NettyServerTransport) transport);
  return new ServerTransportListener() {
    @Override
    public void streamCreated(ServerStream stream, String method, Metadata headers) {
      EchoServerStreamListener listener = new EchoServerStreamListener(stream, method, headers);
      stream.setListener(listener);
      stream.writeHeaders(new Metadata());
      stream.request(1);
      streamListeners.add(listener);
    }

    @Override
    public Attributes transportReady(Attributes transportAttrs) {
      serverTransportAttributesList.add(transportAttrs);
      return transportAttrs;
    }

    @Override
    public void transportTerminated() {}
  };
}
 
Example 3
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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);
  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);
  assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
  assertEquals(status.getCode(), clientStreamStatus.getCode());
  assertEquals(status.getDescription(), clientStreamStatus.getDescription());
  assertTrue(clientStreamTracer1.getOutboundHeaders());
  assertTrue(clientStreamTracer1.getInboundHeaders());
  assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
  assertSame(status, serverStreamTracer1.getStatus());
}
 
Example 4
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void earlyServerClose_withServerHeaders() 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);
  ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
  clientStream.start(clientStreamListener);
  StreamCreation serverStreamCreation
      = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
  ServerStream serverStream = serverStreamCreation.stream;
  ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;

  serverStream.writeHeaders(new Metadata());
  assertNotNull(clientStreamListener.headers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));

  Status strippedStatus = Status.OK.withDescription("Hello. Goodbye.");
  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);
  assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
  assertEquals(status.getCode(), clientStreamStatus.getCode());
  assertEquals("Hello. Goodbye.", clientStreamStatus.getDescription());
  assertNull(clientStreamStatus.getCause());
  assertTrue(clientStreamTracer1.getOutboundHeaders());
  assertTrue(clientStreamTracer1.getInboundHeaders());
  assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
  assertSame(status, serverStreamTracer1.getStatus());
}
 
Example 5
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void transportTracer_server_send_msg() throws Exception {
  server.start(serverListener);
  client = newClientTransport(server);
  startTransport(client, mockClientTransportListener);
  ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions);
  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;
  if (!haveTransportTracer()) {
    return;
  }

  TransportStats serverBefore = getTransportStats(serverTransportListener.transport);
  assertEquals(0, serverBefore.messagesSent);
  assertEquals(0, serverBefore.lastMessageSentTimeNanos);
  TransportStats clientBefore = getTransportStats(client);
  assertEquals(0, clientBefore.messagesReceived);
  assertEquals(0, clientBefore.lastMessageReceivedTimeNanos);

  clientStream.request(1);
  serverStream.writeHeaders(new Metadata());
  serverStream.writeMessage(methodDescriptor.streamResponse("response"));
  serverStream.flush();
  verifyMessageCountAndClose(clientStreamListener.messageQueue, 1);

  TransportStats serverAfter = getTransportStats(serverTransportListener.transport);
  assertEquals(1, serverAfter.messagesSent);
  assertEquals(fakeCurrentTimeNanos(), serverAfter.lastMessageSentTimeNanos);
  TransportStats clientAfter = getTransportStats(client);
  assertEquals(1, clientAfter.messagesReceived);
  assertEquals(fakeCurrentTimeNanos(), clientAfter.lastMessageReceivedTimeNanos);

  serverStream.close(Status.OK, new Metadata());
}
 
Example 6
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void openStreamPreventsTermination() 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);
  ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
  clientStream.start(clientStreamListener);
  verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
  StreamCreation serverStreamCreation
      = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
  ServerStream serverStream = serverStreamCreation.stream;
  ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;

  client.shutdown(Status.UNAVAILABLE);
  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, Arrays.asList(serverStreamTracerFactory));
  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();
  assertNotNull(clientStreamListener.headers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
  assertTrue(serverStreamListener.awaitHalfClosed(TIMEOUT_MS, TimeUnit.MILLISECONDS));

  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));
}