AbstractServerStream extends AbstractStream<IdT> implements ServerStream { @Override public final void close(Status status, Metadata trailers) { Preconditions.checkNotNull(status, "status"); Preconditions.checkNotNull(trailers, "trailers"); if (outboundPhase(Phase.STATUS) != Phase.STATUS) { gracefulClose = true; stashedTrailers = trailers; writeStatusToTrailers(status); closeFramer(); } } protected  AbstractServerStream(WritableBufferAllocator bufferAllocator,
                                 int maxMessageSize); final void setListener(ServerStreamListener listener); @Override final void writeHeaders(Metadata headers); @Override final void writeMessage(InputStream message); @Override final void close(Status status, Metadata trailers); void inboundDataReceived(ReadableBuffer frame, boolean endOfStream); void complete(); final void abortStream(Status status, boolean notifyClient); @Override boolean isClosed(); @Override Attributes attributes();  }
@Test public void close_failsOnNullStatus() { thrown.expect(NullPointerException.class); defaultStream.close(null, new Metadata()); }
@Test public void close_failsOnNullMetadata() { thrown.expect(NullPointerException.class); defaultStream.close(Status.INTERNAL, null); }
@Test public void close_sendsTrailers() { final AtomicReference<Metadata> capturedTrailers = new AtomicReference<Metadata>(null); AbstractServerStreamBase stream = new AbstractServerStreamBase(allocator, MAX_MESSAGE_SIZE) { @Override protected void sendTrailers(Metadata trailers, boolean headersSent) { capturedTrailers.set(trailers); } }; Metadata trailers = new Metadata(); stream.close(Status.INTERNAL, trailers); assertSame(trailers, capturedTrailers.get()); }
@Test public void close_sendTrailersClearsReservedFields() { final AtomicReference<Metadata> capturedTrailers = new AtomicReference<Metadata>(null); AbstractServerStreamBase stream = new AbstractServerStreamBase(allocator, MAX_MESSAGE_SIZE) { @Override protected void sendTrailers(Metadata trailers, boolean headersSent) { capturedTrailers.set(trailers); } }; Metadata trailers = new Metadata(); trailers.put(Status.CODE_KEY, Status.OK); trailers.put(Status.MESSAGE_KEY, "Everything's super."); stream.close(Status.INTERNAL.withDescription("bad"), trailers); assertEquals(Status.Code.INTERNAL, capturedTrailers.get().get(Status.CODE_KEY).getCode()); assertEquals("bad", capturedTrailers.get().get(Status.MESSAGE_KEY)); }