|
| 1 | +# README — HTTP SSE tests |
| 2 | + |
| 3 | +**Location:** `trpc/server/http_sse/test` |
| 4 | + |
| 5 | +This document describes two unit tests that exercise the server-side SSE stream writer and a simple SSE-capable service handler. It explains what the tests do, how to build & run them with Bazel, expected outputs, troubleshooting tips and suggestions for extension. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Files in this folder |
| 10 | + |
| 11 | +* **`http_sse_stream_parser_test.cc`** |
| 12 | + Tests `trpc::stream::SseStreamWriter` output at the *wire* level and validates that the client-side parser (`trpc::http::sse::SseParser`) can decode the chunked HTTP body and produce correct SSE events. |
| 13 | + |
| 14 | +* **`http_sse_service_test.cc`** |
| 15 | + Tests `HttpSseService` handler behavior by directly invoking a stream-capable handler (`DummySseHandler`) that uses `SseStreamWriter` to emit SSE payloads. Verifies the handler returns success and the server context remains healthy. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +# What each test does (high level) |
| 20 | + |
| 21 | +## `http_sse_stream_parser_test.cc` |
| 22 | + |
| 23 | +1. Creates a `MockServerContext` that captures bytes passed to `SendResponse(NoncontiguousBuffer)`. |
| 24 | +2. Creates `trpc::stream::SseStreamWriter` bound to the mock context. |
| 25 | +3. Writes header, writes SSE event(s) (via `WriteEvent` or `WriteBuffer`), and calls `WriteDone`. |
| 26 | +4. From captured bytes: finds header/body separator, decodes HTTP **chunked** body into concatenated payload(s). |
| 27 | +5. Uses `trpc::http::sse::SseParser::ParseEvents` to parse SSE text into `SseEvent` objects. |
| 28 | +6. Asserts `id`, `event_type` and `data` match expected values. |
| 29 | + |
| 30 | +**Purpose:** verifies `SseStreamWriter` produces a valid HTTP header + chunked body where the chunked payload is properly formatted SSE text and is parsable by the SSE parser. |
| 31 | + |
| 32 | +## `http_sse_service_test.cc` |
| 33 | + |
| 34 | +1. Initializes codec and serialization subsystems required by the framework. |
| 35 | +2. Builds a mock `ServerContext` (via test helpers) and constructs a `DummySseHandler` that: |
| 36 | + |
| 37 | + * marks the response as streaming (`rsp->EnableStream(ctx)`), |
| 38 | + * writes header and SSE events using `SseStreamWriter`, |
| 39 | + * finishes with `WriteDone`. |
| 40 | +3. Calls `handler->Get(...)` directly and asserts `Status::OK()` and that `ServerContext` has no stream-reset condition. |
| 41 | + |
| 42 | +**Purpose:** verifies that inside a ServerContext and handler, `SseStreamWriter` can be used and completes without framework-level failures,test ` http_sse_service.cc` . |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +# Build & run (Bazel) |
| 47 | + |
| 48 | +From the repository root, build and run the tests: |
| 49 | + |
| 50 | +### Run the stream parser test |
| 51 | + |
| 52 | +```bash |
| 53 | +bazel build //trpc/server/http_sse/test:http_sse_stream_parser_test |
| 54 | +bazel test //trpc/server/http_sse/test:http_sse_stream_parser_test --test_output=all |
| 55 | +``` |
| 56 | + |
| 57 | +### Run the service test |
| 58 | + |
| 59 | +```bash |
| 60 | +bazel build //trpc/server/http_sse/test:http_sse_service_test |
| 61 | +bazel test //trpc/server/http_sse/test:http_sse_service_test --test_output=all |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +# Expected output |
| 67 | +When tests succeed, Bazel will show output similar to: |
| 68 | + |
| 69 | +``` |
| 70 | +==================== Test output for //trpc/server/http_sse/test:http_sse_stream_parser_test: |
| 71 | +Running main() from gmock_main.cc |
| 72 | +[==========] Running 2 tests from 1 test suite. |
| 73 | +[----------] Global test environment set-up. |
| 74 | +[----------] 2 tests from SseStreamWriter_SseParser_Test |
| 75 | +[ RUN ] SseStreamWriter_SseParser_Test.WriteEventAndClientParse |
| 76 | +[ OK ] SseStreamWriter_SseParser_Test.WriteEventAndClientParse (2 ms) |
| 77 | +[ RUN ] SseStreamWriter_SseParser_Test.WriteBufferAndClientParse |
| 78 | +[ OK ] SseStreamWriter_SseParser_Test.WriteBufferAndClientParse (0 ms) |
| 79 | +[----------] 2 tests from SseStreamWriter_SseParser_Test (2 ms total) |
| 80 | +
|
| 81 | +[----------] Global test environment tear-down |
| 82 | +[==========] 2 tests from 1 test suite ran. (2 ms total) |
| 83 | +[ PASSED ] 2 tests. |
| 84 | +================================================================================ |
| 85 | +
|
| 86 | +==================== Test output for //trpc/server/http_sse/test:http_sse_service_test: |
| 87 | +Running main() from gmock_main.cc |
| 88 | +[==========] Running 1 test from 1 test suite. |
| 89 | +[----------] Global test environment set-up. |
| 90 | +[----------] 1 test from HttpSseServiceTest |
| 91 | +[ RUN ] HttpSseServiceTest.DirectHandlerInvoke_WritesSseEvents |
| 92 | +[ OK ] HttpSseServiceTest.DirectHandlerInvoke_WritesSseEvents (2 ms) |
| 93 | +[----------] 1 test from HttpSseServiceTest (2 ms total) |
| 94 | +
|
| 95 | +[----------] Global test environment tear-down |
| 96 | +[==========] 1 test from 1 test suite ran. (2 ms total) |
| 97 | +[ PASSED ] 1 test. |
| 98 | +================================================================================ |
| 99 | +
|
0 commit comments