Skip to content

Add unit tests and code coverage for the message path#13

Merged
cjimti merged 1 commit into
masterfrom
12-add-tests-and-coverage
Jun 16, 2026
Merged

Add unit tests and code coverage for the message path#13
cjimti merged 1 commit into
masterfrom
12-add-tests-and-coverage

Conversation

@cjimti

@cjimti cjimti commented Jun 16, 2026

Copy link
Copy Markdown
Member

Closes #12.

Establishes real test coverage on rtbeat's core logic, which previously had none (config_test.go was an empty stub; beater had no tests). make verify passes; no production behavior change.

Production change (behavior-preserving)

  • Extracted the rxtx MessageBatch[]beat.Event logic out of the gin handler closure into buildEvents(), and the POST /in handler into inHandler(). Metric updates are injected as onBatch/onMessages callbacks so the handler is testable without touching the global prometheus registry; an eventPublisher interface lets tests fake the libbeat client.
  • Incidental fix: clientIp is now read synchronously from the request instead of from the publish goroutine, where gin may already have recycled the *gin.Context.

Tests

  • beater: table-driven buildEvents (field mapping for type/rxtxMsg/clientIp, Private index, the leading-empty-event quirk locked down as a regression guard) and httptest coverage of POST /in (200/500, metric callbacks, clientIp propagation, no-publish-on-error).
  • config: DefaultConfig values, Unpack overrides, empty-keeps-defaults.

Coverage

  • buildEvents and inHandler are 100% covered; beater package total 27.1% (the remainder — New/Run/Stop — needs a live beat pipeline / integration test).
  • README gains a codecov badge.

Notes

Per #12, the leading-empty-event publishing quirk is asserted, not changed — altering it is a separate decision. Adversarial review confirmed behavioral equivalence and that the c.ClientIP() move is a latent-bug fix.

Establish real test coverage on rtbeat's core logic, which previously
had none (config_test.go was an empty stub; beater had no tests).

Production change (behavior-preserving):
- Extract the rxtx MessageBatch -> []beat.Event logic out of the gin
  handler closure into buildEvents(), and the POST /in handler into
  inHandler(). Metric updates are injected as onBatch/onMessages
  callbacks so the handler is testable without the global prometheus
  registry. An eventPublisher interface lets tests fake the libbeat
  client.
- Incidental fix: clientIp is now read synchronously from the request
  instead of from the publish goroutine, where gin may already have
  recycled the context.

Tests:
- beater: table-driven buildEvents (field mapping, Private index, the
  leading-empty-event quirk locked down as a regression guard) and
  httptest coverage of POST /in (200/500, metric callbacks, clientIp
  propagation, no-publish-on-error).
- config: DefaultConfig values, Unpack overrides, empty-keeps-defaults.

buildEvents and inHandler are 100% covered. README gains a codecov
badge. make verify passes; no production behavior change (the
leading-empty-event quirk is asserted, not altered).

Closes #12
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@cjimti
cjimti merged commit da531c4 into master Jun 16, 2026
9 checks passed
@cjimti
cjimti deleted the 12-add-tests-and-coverage branch June 16, 2026 23:39
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.

Add unit tests and code coverage (currently 0%)

1 participant