Class StreamedResponse

java.lang.Object
com.jsgalactic.axiom.test.StreamedResponse
All Implemented Interfaces:
AutoCloseable

public final class StreamedResponse extends Object implements AutoCloseable
A response whose body is read piece by piece, returned by TestClient.stream(com.jsgalactic.axiom.http.Request). It plays the client of a streamed response (see Response.stream) without a network, and makes the timing of the exchange deterministic: the handler's writer and this reader meet at a bounded hand-off.

Backpressure. The handler can be at most one write ahead of the reader. Its next write blocks until next() has taken the previous chunk, so a test that does not read holds the handler exactly where a slow client would, with no timing involved, and a test that reads one chunk at a time sees every write as its own chunk, in order. A blocked write is still bounded by the request deadline.

Ending. next() returns empty once the body has ended, whether it completed or failed; completion() then says which. close() plays a client that disconnects: the handler's writes throw StreamAbortedException with StreamAbortedException.Reason.CLIENT_DISCONNECTED, it is interrupted if it is blocked elsewhere, and its admission slot is released when it returns. A response that was not streamed is delivered as a single chunk. Thread-safe; closing is idempotent.

  • Method Details

    • status

      public int status()
      Returns the status of the response head.
      Returns:
      the status
    • headers

      public Map<String,String> headers()
      Returns the headers of the response head.
      Returns:
      an immutable, case-insensitive map
    • next

      public Optional<byte[]> next() throws InterruptedException
      Waits for the next chunk. Each write of the handler is one chunk, and chunks are never joined or split.
      Returns:
      the chunk, or empty once the body has ended
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • nextText

      public Optional<String> nextText() throws InterruptedException
      Waits for the next chunk and decodes it as UTF-8.
      Returns:
      the chunk as text, or empty once the body has ended
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • readAll

      public byte[] readAll() throws InterruptedException
      Reads the rest of the body, which makes sense for a body that ends. A body that never ends is bounded by its byte cap and the request deadline.
      Returns:
      the remaining bytes
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • readAllText

      public String readAllText() throws InterruptedException
      Reads the rest of the body as UTF-8 text.
      Returns:
      the remaining text
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • completion

      public CompletableFuture<Void> completion()
      Returns the outcome of the request. It completes normally when the body returned without the stream being aborted, and exceptionally with what ended it otherwise: the exception the handler threw (including a StreamAbortedException), a deadline, or a cancellation after close(). In the listener the same outcomes close the connection without a final chunk.
      Returns:
      the outcome
    • close

      public void close()
      Disconnects: the handler can no longer write, and is interrupted if it is not writing. Chunks that were not read are dropped.
      Specified by:
      close in interface AutoCloseable
    • beginShutdown

      public void beginShutdown()
      Plays the listener closing: the handler's writer reports shutdownRequested() and runs the actions registered with onShutdown, once. Writes keep working, as they do during the listener's grace period, so a body that ends normally is seen to complete, with its final chunk. Use close() afterwards to play the end of the grace period instead. Idempotent.