Skip to content

test(websocket): expand websocket_server.cpp coverage#1072

Merged
kcenon merged 2 commits into
developfrom
test/issue-1067-websocket-server-coverage
Apr 27, 2026
Merged

test(websocket): expand websocket_server.cpp coverage#1072
kcenon merged 2 commits into
developfrom
test/issue-1067-websocket-server-coverage

Conversation

@kcenon

@kcenon kcenon commented Apr 27, 2026

Copy link
Copy Markdown
Owner

What

Summary

Append 33 unit tests to tests/test_messaging_ws_server.cpp exercising further public-API surfaces of messaging_ws_server from src/http/websocket_server.cpp. Complements the existing tests/unit/websocket_server_branch_test.cpp (added in #1059) by covering different angles.

Change Type

  • Test (no production behavior change)

Affected Components

  • tests/test_messaging_ws_server.cpp — +388 LOC, +33 test cases (no source/build changes)

Why

Part of #1067 (and the parent epic Part of #953) — push narrow test-expansion across worst-coverage protocol files.

Pre-PR baseline (2026-04-26 lcov): src/http/websocket_server.cpp 39.9% line / 19.7% branch. Existing tests cover the never-started state machine; this PR adds tests for ws_message data helpers, ws_close_code enum invariants, constructor variants, interface-pointer default queries, lookup edge cases, broadcast under registered callbacks, and lambda-capture semantics of the interface callback adapters.

Where

Test category Tests added Targets
ws_message::as_text / as_binary 4 message struct used by on_message dispatch
ws_close_code enum invariants (RFC 6455) 6 enum values used by close(uint16_t, string_view)
Constructor variations 5 c-string, std::string, string_view, substring, ref stability
Default queries via interface pointer 3 i_websocket_server::is_running / connection_count / stop
get_connection lookup edge cases 3 empty / 2048-char / IPv4-style ids
get_all_connections stability 1 empty-vector across calls
Broadcast with callbacks but no peers 2 text + binary do not invoke callbacks
Interface-callback lambda capture 4 connection / disconnection / error capture, all-five null replace
Multi-instance state isolation 2 32 sequential, two same-id
wait_for_stop on never-started 1 returns immediately
stop_server idempotency 2 many calls + via interface pointer

How

Implementation Approach

  • Append tests to existing tests/test_messaging_ws_server.cpp (no parallel suite)
  • Reuse the existing MessagingWsServerTest GTest fixture
  • Add only <atomic>, <chrono>, <cstdint>, <limits>, <memory>, <string>, <string_view>, <system_error>, <utility>, <vector>, and internal/websocket/websocket_protocol.h to the test-file includes
  • No changes to production source, build files, or CMake

Coverage Scope (Honest Assessment)

This PR does not reach the >=80% line / >=70% branch acceptance criteria of #1067. The remaining gap is concentrated in the post-handshake code (do_start_impl success path past tcp::acceptor construction, do_accept async completion, handle_new_connection TCP+WebSocket wrapping, on_message / on_close / on_error reachable only after a live HTTP/1.1 upgrade handshake). Those paths require an in-process WebSocket loopback fixture that does not currently exist in the test tree.

Building the loopback fixture is a larger, separate piece of work (TCP loopback + complete WebSocket HTTP/1.1 upgrade + frame I/O) tracked under #953. This PR therefore uses Part of #1067 rather than Closes #1067 and the issue stays open.

Test Plan

Breaking Changes

None — test-only addition.

Rollback Plan

Revert this single commit; no schema, ABI, or build surface affected.

Part of #1067
Part of #953

Append 33 unit tests to tests/test_messaging_ws_server.cpp targeting
public-API surfaces of messaging_ws_server reachable without a live
WebSocket peer. Complements tests/unit/websocket_server_branch_test.cpp
(added in #1059) by exercising additional angles:

- ws_message struct as_text() / as_binary() round-trip and reference
  semantics used by invoke_message_callback() routing
- ws_close_code enum value invariants per RFC 6455 Section 7.4 used
  by ws_connection::close(uint16_t, string_view) and on_close()
- Constructor variations (c-string, std::string, string_view,
  substring string_view) and server_id() reference stability
- Default-state queries via interfaces::i_websocket_server pointer
  (is_running, connection_count, stop)
- get_connection() lookup for empty / 2048-char / IPv4-style ids on
  never-started server returning nullptr
- get_all_connections() empty-vector stability on never-started server
- broadcast_text / broadcast_binary with registered text / binary
  callbacks but no peers does not invoke the callbacks
- Interface callback adapters: lambda capture is stored not invoked
  immediately for connection / disconnection / error
- All five interface callbacks replaceable with default-constructed
  std::function (covers the empty-function adapter branch)
- Multi-instance state isolation across 32 sequential constructions
- Two servers with the same id are independent instances
- wait_for_stop on never-started server returns under one second
- stop_server idempotency across 10 calls and via interface pointer

These tests are hermetic and rely solely on state reachable without
a live io_context loop or TCP peer. The acceptance criteria of #1067
(line >= 80% / branch >= 70%) cannot be met from unit tests alone
because the post-handshake frame I/O path requires an in-process
WebSocket loopback fixture that does not exist yet; the gap is
tracked under #953.

Part of #1067
Part of #953
i_network_component::is_running() is declared protected in the base
interface; only derived class messaging_ws_server publishes it
publicly. Calling is_running() through std::shared_ptr<i_websocket_server>
fails to compile with 'is protected within this context'.

Replace the failing test with an interface-pointer stop()/connection_count()
idempotency variant that uses only public surface.
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Report

Metric Value
Line Coverage 67.0%
Branch Coverage 33.2%
Target 80% lines / 70% branches
Coverage Details

Full HTML report is available as a build artifact.

@kcenon kcenon merged commit 700cc3b into develop Apr 27, 2026
9 checks passed
@kcenon kcenon deleted the test/issue-1067-websocket-server-coverage branch April 27, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants