Java Code Examples for io.grpc.Status#withCause()

The following examples show how to use io.grpc.Status#withCause() . 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: 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 2
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void earlyServerClose_serverFailure() 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;

  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);
  assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
  assertEquals(status.getCode(), clientStreamStatus.getCode());
  assertEquals(status.getDescription(), clientStreamStatus.getDescription());
  assertNull(clientStreamStatus.getCause());
  assertTrue(clientStreamTracer1.getOutboundHeaders());
  assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
  assertSame(status, serverStreamTracer1.getStatus());
}
 
Example 3
Source File: InProcessTransport.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new status with the same code and description.
 * If includeCauseWithStatus is true, cause is also included.
 *
 * <p>For InProcess transport to behave in the same way as the other transports,
 * when exchanging statuses between client and server and vice versa,
 * the cause should be excluded from the status.
 * For easier debugging, the status may be optionally included.
 */
private static Status cleanStatus(Status status, boolean includeCauseWithStatus) {
  if (status == null) {
    return null;
  }
  Status clientStatus = Status
      .fromCodeValue(status.getCode().value())
      .withDescription(status.getDescription());
  if (includeCauseWithStatus) {
    clientStatus = clientStatus.withCause(status.getCause());
  }
  return clientStatus;
}
 
Example 4
Source File: AbstractTransportTest.java    From grpc-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);
  Metadata clientStreamTrailers =
      clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
  assertNotNull(clientStreamTrailers);
  checkClientStatus(status, clientStreamStatus);
  assertTrue(clientStreamTracer1.getOutboundHeaders());
  assertTrue(clientStreamTracer1.getInboundHeaders());
  assertSame(clientStreamTrailers, clientStreamTracer1.getInboundTrailers());
  assertSame(clientStreamStatus, clientStreamTracer1.getStatus());
  assertSame(status, serverStreamTracer1.getStatus());
}
 
Example 5
Source File: AbstractTransportTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void earlyServerClose_serverFailure() 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;

  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());
}
 
Example 6
Source File: AbstractTransportTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@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);
  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);
  assertEquals(status.getCode(), clientStreamStatus.getCode());
  assertEquals("Hellogoodbye", clientStreamStatus.getDescription());
  // Cause should not be transmitted to the client.
  assertNull(clientStreamStatus.getCause());
  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(clientStreamStatus, clientStreamTracer1.getStatus());
  assertSame(status, serverStreamTracer1.getStatus());
}
 
Example 7
Source File: AbstractTransportTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@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);
  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());
}