dengxiaofu opened a new issue, #11604:
URL: https://github.com/apache/rocketmq/issues/11604
### Before Creating the Enhancement Request
- [x] I have confirmed that this should be classified as an enhancement
rather than a bug/feature.
### Summary
`org.apache.rocketmq.common.statistics.StatisticsItem` has no dedicated unit
tests.
No test class in any module references it, although it is the core
accumulator
behind the broker statistics framework. This issue proposes adding
`StatisticsItemTest` to cover its constructor and public methods.
### Motivation
`StatisticsItem` is used in production by:
- `StatisticsManager`, which attaches a `StatisticsBriefInterceptor` via
`setInterceptor` and evicts idle items based on `getLastTimeStamp()`;
- `StatisticsItemScheduledIncrementPrinter`, which periodically calls
`snapshot()`, then `subtract(lastSnapshot)` (with `lastSnapshot == null`
on the
first run), uses `allZeros()` to decide whether to print a line, and reads
values through `getItemAccumulate(name)`.
Because none of these methods are tested directly, a regression in snapshot
independence, delta computation, or the `allZeros()` check would only show up
as wrong statistics output at runtime. Dedicated tests make this behavior
explicit and protect future refactoring.
### Describe the Solution You'd Like
Add a single new test class
`common/src/test/java/org/apache/rocketmq/common/statistics/StatisticsItemTest.java`
(JUnit 4, no production code changes, no new dependencies) covering:
- constructor validation (null / empty `itemNames`) and initial state;
- `incItems` with fewer or more values than items, invoke-count and
last-timestamp updates, and delegation of all values to the `Interceptor`;
- `allZeros` for untouched, zero, non-zero and net-zero accumulations;
- `getItemAccumulate` for known and unknown item names;
- `snapshot` producing an independent copy without the interceptor;
- `subtract` with `null`, with incompatible items (kind / object / item
names),
and the normal per-item delta computation.
### Describe Alternatives You've Considered
Testing `StatisticsItem` indirectly through `StatisticsManager` or
`StatisticsItemScheduledIncrementPrinter` was considered, but those classes
involve scheduled executors and logging, which makes the tests slower and
harder to keep deterministic. Testing `StatisticsItem` directly is simpler
and
pinpoints failures.
### Additional Context
Two tests intentionally document current behavior rather than a
specification:
`allZeros()` returns true whenever `invokeTimes` is 0, and `subtract()` does
not
clamp negative deltas.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]