Class StreamedResponse
- All Implemented Interfaces:
AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionvoidPlays the listener closing: the handler's writer reportsshutdownRequested()and runs the actions registered withonShutdown, once.voidclose()Disconnects: the handler can no longer write, and is interrupted if it is not writing.Returns the outcome of the request.headers()Returns the headers of the response head.Optional<byte[]> next()Waits for the next chunk.nextText()Waits for the next chunk and decodes it as UTF-8.byte[]readAll()Reads the rest of the body, which makes sense for a body that ends.Reads the rest of the body as UTF-8 text.intstatus()Returns the status of the response head.
-
Method Details
-
status
public int status()Returns the status of the response head.- Returns:
- the status
-
headers
Returns the headers of the response head.- Returns:
- an immutable, case-insensitive map
-
next
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
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
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
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
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 aStreamAbortedException), a deadline, or a cancellation afterclose(). 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:
closein interfaceAutoCloseable
-
beginShutdown
public void beginShutdown()Plays the listener closing: the handler's writer reportsshutdownRequested()and runs the actions registered withonShutdown, 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. Useclose()afterwards to play the end of the grace period instead. Idempotent.
-