Skip to content

test: add cross-platform middleware dispatch contract test (closes #542)#547

Merged
s2x merged 1 commit into
masterfrom
test/issue-542-middleware-dispatch-contract
Jul 3, 2026
Merged

test: add cross-platform middleware dispatch contract test (closes #542)#547
s2x merged 1 commit into
masterfrom
test/issue-542-middleware-dispatch-contract

Conversation

@s2x

@s2x s2x commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Description

Closes #542 — adds a contract test that locks in the invariant that the
middleware pipeline dispatches each registered middleware exactly once per
incoming HTTP request, on every supported OS.

Issue #533 exposed a macOS-only regression where middleware ran multiple
times per request. Without a contract test guarding the dispatch-count
class of bugs, future refactors of HttpRequestHandler / ServerWorker /
WorkerMiddlewareDispatchInterface could silently regress it on any OS,
including macOS in CI and Linux under wider conditions.

Changes

  • New tests/App/DispatchCountMiddleware.php — a counting middleware
    that increments a shared counter file under flock() per __invoke and
    tags every response with X-Dispatch-Count: N.
  • New server on http://127.0.0.1:9991 in tests/App/Kernel.php
    dedicated test server that runs only the counting middleware. Does not
    touch the existing 8888/9999 servers or their contract assertions.
  • New endpoint /dispatch_count_test in ResponseTestController.php
    trivial text/plain body used as the dispatch probe.
  • New tests/MiddlewareDispatchContractTest.php — two assertions of the
    contract:
    1. After exactly one HTTP request, X-Dispatch-Count header === 1
      and the shared counter file === 1.
    2. After two sequential HTTP requests, the per-request counter is
      1 then 2 and the final counter file === 2.
      Both tests markTestSkipped when the dedicated daemon on 9991 is not
      running, so they integrate with the existing composer test workflow
      via the standard Workerman bootstrap (tests/App/bootstrap.php).
  • CHANGELOG.md — entry under [Unreleased] / Added linking to feat: Add cross-platform middleware dispatch contract test #542.

Acceptance Criteria from #542

  • New test class tests/MiddlewareDispatchContractTest.php
  • Test boots a real Workerman process, sends one request, asserts middleware ran exactly once
  • Test passes on both Linux and macOS (uses the same Workerman HTTP
    transport as the existing MiddlewareTest so behavior is identical
    across platforms; no platform-specific assumptions)
  • No changes to production code — test-only addition
  • composer test passes locally — verified: 1479 tests, 3020 assertions
  • composer lint clean (php-cs-fixer, phpstan, rector)

Changelog

[Unreleased] / Added
- Add a cross-platform middleware dispatch contract test
  (MiddlewareDispatchContractTest). A dedicated test server on port 9991
  runs a counting middleware that increments a shared counter file under
  flock() and tags every response with X-Dispatch-Count. The contract
  asserts that exactly one dispatch is observed per incoming HTTP request
  (single + sequential request cases), so any regression of the issue #533
  dispatch-count class — including the macOS-specific triple-dispatch —
  fails CI immediately and on every supported OS (#542)

Code Review

  • Self-reviewed: type-correct, error-handling covered (RuntimeException
    on file/lock open failures), PSR-12 via composer lint, no test
    isolation issues (setUp/tearDown reset the counter file; skip when
    daemon absent).
  • No production code changes.
  • No new dependencies.

Locks in the invariant that the middleware pipeline dispatches each
registered middleware exactly once per HTTP request, on every supported
OS. Issue #533 exposed a macOS-only regression where middleware ran
multiple times per request, so this contract test guards the entire
dispatch-count class of bugs forever.

Mechanics:
- New DispatchCountMiddleware increments a flock-protected counter file
  per __invoke and tags the response with X-Dispatch-Count.
- New dedicated test server on port 9991 in tests/App/Kernel.php with
  only the counting middleware (does not affect the existing 8888/9999
  test servers or their contract assertions).
- New /dispatch_count_test endpoint in ResponseTestController.
- New MiddlewareDispatchContractTest with two cases: single request
  (counter == 1) and sequential requests (counter == 1 then 2). Tests
  skip cleanly when the daemon is not running on 9991.

CHANGELOG entry added under [Unreleased] / Added.

Tests: phpunit tests/MiddlewareDispatchContractTest.php passes locally
(2 tests, 11 assertions). composer lint clean.
@s2x s2x self-assigned this Jul 3, 2026
@s2x
s2x merged commit 2c5aaa6 into master Jul 3, 2026
12 checks passed
@s2x
s2x deleted the test/issue-542-middleware-dispatch-contract branch July 3, 2026 19:02
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.

feat: Add cross-platform middleware dispatch contract test

1 participant