Skip to content

Fix off-by-one page count in PageUtil for divisible totals - #15446

Merged
KomachiSion merged 3 commits into
alibaba:developfrom
vasiliy-mikhailov:fix/pageutil-ceil-division
Jul 3, 2026
Merged

KomachiSion merged 3 commits into
alibaba:developfrom
vasiliy-mikhailov:fix/pageutil-ceil-division

Conversation

@vasiliy-mikhailov

@vasiliy-mikhailov vasiliy-mikhailov commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

PageUtil computes the page count as (totalCount / pageSize) + 1, which
reports one page too many whenever totalCount is an exact multiple of
pageSize (e.g. 9 items at 3 per page yields 4 instead of 3), and reports 1
page for an empty result. Callers that iterate 1..pagesAvailable then visit a
trailing empty page.

Use ceiling division (totalCount + pageSize - 1) / pageSize. One existing
assertion in PageUtilTest encoded the old off-by-one value (2 pages for 2
items at 2 per page) and is corrected to 1; a test for the exactly-divisible
case is added.

Verifying this change

PageUtilTest (which includes the added cases) fails on the current develop and passes with this change:

Before the fix (on develop):

$ mvn test -Dtest=PageUtilTest -pl core
[INFO] Running com.alibaba.nacos.core.utils.PageUtilTest
[ERROR] Tests run: 13, Failures: 2, Errors: 0, Skipped: 0 <<< FAILURE! -- in com.alibaba.nacos.core.utils.PageUtilTest
[ERROR]   PageUtilTest.subPageExactDivisiblePages:134 expected: <3> but was: <4>
[ERROR]   PageUtilTest.subPageExactlyOneFullPage:125 expected: <1> but was: <2>
[INFO] BUILD FAILURE

After the fix:

$ mvn test -Dtest=PageUtilTest -pl core
[INFO] Running com.alibaba.nacos.core.utils.PageUtilTest
[INFO] Tests run: 13, Failures: 0, Errors: 0, Skipped: 0 -- in com.alibaba.nacos.core.utils.PageUtilTest
[INFO] BUILD SUCCESS

AI assistance disclosure

This contribution was produced with the help of an AI pipeline. The pipeline processed a large amount of source code to surface suspected bugs, reproduced a subset of them with failing unit tests and generated candidate fixes, and prepared pull requests from the ones that held up. Each PR was then reviewed and verified by a human before being opened: the fix and test were checked by hand and the test was confirmed to fail before the change and pass after.

@KomachiSion

Copy link
Copy Markdown
Collaborator

@vasiliy-mikhailov Please fix the ci problem.

Signed-off-by: vmihaylov <vasiliy.mikhailov@gmail.com>
@vasiliy-mikhailov

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up. The CI failure was a Spotless format issue (a blank line in the added test needed the project indent). Ran spotless:apply and pushed, the check is green locally now.

…ivision fix

Signed-off-by: Vasiliy Mikhailov <vasiliy.mikhailov@gmail.com>
@vasiliy-mikhailov

Copy link
Copy Markdown
Contributor Author

Fixed. The remaining CI failure was a pre-existing test, CatalogServiceV2ImplTest.testListService, that hardcoded the old off-by-one page count (it asserted 4 pages for 3 services at pageSize 1, with the message describing the buggy (totalCount / pageSize) + 1 formula). The PageUtil fix corrects that to 3 (a divisible total has no extra page), so I updated the assertion and its message to match. Pushed.

@KomachiSion

Copy link
Copy Markdown
Collaborator

reopen for cla

@KomachiSion KomachiSion closed this Jul 1, 2026
@KomachiSion KomachiSion reopened this Jul 1, 2026
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API、新增配置参数、修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@KomachiSion
KomachiSion merged commit 77827af into alibaba:develop Jul 3, 2026
8 of 9 checks passed
@KomachiSion KomachiSion added this to the 3.3.0 milestone Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants