@Test
 public void writeHeadersShouldSendHeaders() throws Exception {
   Metadata headers = new Metadata();
   stream().writeHeaders(headers);
   verify(writeQueue)
       .enqueue(
           new SendResponseHeadersCommand(STREAM_ID, Utils.convertServerHeaders(headers), false),
           true);
 }
 @Test
 public void duplicateWriteHeadersShouldFail() throws Exception {
   Metadata headers = new Metadata();
   stream().writeHeaders(headers);
   verify(writeQueue)
       .enqueue(
           new SendResponseHeadersCommand(STREAM_ID, Utils.convertServerHeaders(headers), false),
           true);
   try {
     stream().writeHeaders(headers);
     fail("Can only write response headers once");
   } catch (IllegalStateException ise) {
     // Success
   }
 }
  @Override
  public ClientStream newStream(
      MethodDescriptor<?, ?> method, Metadata.Headers headers, ClientStreamListener listener) {
    Preconditions.checkNotNull(method, "method");
    Preconditions.checkNotNull(headers, "headers");
    Preconditions.checkNotNull(listener, "listener");

    // Create the stream.
    final NettyClientStream stream = new NettyClientStream(listener, channel, handler);

    // Convert the headers into Netty HTTP/2 headers.
    AsciiString defaultPath = new AsciiString("/" + method.getFullMethodName());
    Http2Headers http2Headers =
        Utils.convertClientHeaders(headers, negotiationHandler.scheme(), defaultPath, authority);

    ChannelFutureListener failureListener =
        new ChannelFutureListener() {
          @Override
          public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
              // Stream creation failed. Close the stream if not already closed.
              stream.transportReportStatus(
                  Status.fromThrowable(future.cause()), true, new Metadata());
            }
          }
        };

    // Write the command requesting the creation of the stream.
    handler
        .getWriteQueue()
        .enqueue(
            new CreateStreamCommand(http2Headers, stream),
            !method.getType().clientSendsOneMessage())
        .addListener(failureListener);
    return stream;
  }