Search in sources :

Example 1 with SinkWriterImpl

use of io.grpc.protobuf.services.BinlogHelper.SinkWriterImpl in project grpc-java by grpc.

the class BinlogHelperTest method logTrailer.

@Test
public void logTrailer() throws Exception {
    long seq = 1;
    InetAddress address = InetAddress.getByName("127.0.0.1");
    int port = 12345;
    InetSocketAddress peerAddress = new InetSocketAddress(address, port);
    long callId = 1000;
    Status statusDescription = Status.INTERNAL.withDescription("my description");
    GrpcLogEntry.Builder builder = metadataToProtoTestHelper(EventType.EVENT_TYPE_SERVER_TRAILER, nonEmptyMetadata, 10).toBuilder().setTimestamp(timestamp).setSequenceIdWithinCall(seq).setLogger(Logger.LOGGER_CLIENT).setCallId(callId).setPeer(BinlogHelper.socketToProto(peerAddress));
    builder.getTrailerBuilder().setStatusCode(Status.INTERNAL.getCode().value()).setStatusMessage("my description");
    GrpcLogEntry base = builder.build();
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base);
    }
    // logger is server
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_SERVER, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().clearPeer().setLogger(Logger.LOGGER_SERVER).build());
    }
    // peerAddress is null
    {
        sinkWriterImpl.logTrailer(seq, statusDescription, nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, /*peerAddress=*/
        null);
        verify(sink).write(base.toBuilder().clearPeer().build());
    }
    // status code is present but description is null
    {
        sinkWriterImpl.logTrailer(seq, // strip the description
        statusDescription.getCode().toStatus(), nonEmptyMetadata, Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base.toBuilder().setTrailer(base.getTrailer().toBuilder().clearStatusMessage()).build());
    }
    // status proto always logged if present (com.google.rpc.Status),
    {
        int zeroHeaderBytes = 0;
        SinkWriterImpl truncatingWriter = new SinkWriterImpl(sink, timeProvider, zeroHeaderBytes, MESSAGE_LIMIT);
        com.google.rpc.Status statusProto = com.google.rpc.Status.newBuilder().addDetails(Any.pack(StringValue.newBuilder().setValue("arbitrarypayload").build())).setCode(Status.INTERNAL.getCode().value()).setMessage("status detail string").build();
        StatusException statusException = StatusProto.toStatusException(statusProto, nonEmptyMetadata);
        truncatingWriter.logTrailer(seq, statusException.getStatus(), statusException.getTrailers(), Logger.LOGGER_CLIENT, callId, peerAddress);
        verify(sink).write(base.toBuilder().setTrailer(builder.getTrailerBuilder().setStatusMessage("status detail string").setStatusDetails(ByteString.copyFrom(statusProto.toByteArray())).setMetadata(io.grpc.binarylog.v1.Metadata.getDefaultInstance())).build());
    }
}
Also used : Status(io.grpc.Status) StatusException(io.grpc.StatusException) InetSocketAddress(java.net.InetSocketAddress) SinkWriterImpl(io.grpc.protobuf.services.BinlogHelper.SinkWriterImpl) GrpcLogEntry(io.grpc.binarylog.v1.GrpcLogEntry) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

Status (io.grpc.Status)1 StatusException (io.grpc.StatusException)1 GrpcLogEntry (io.grpc.binarylog.v1.GrpcLogEntry)1 SinkWriterImpl (io.grpc.protobuf.services.BinlogHelper.SinkWriterImpl)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Test (org.junit.Test)1