Skip to content

Add unit test coverage for vsetMemUsage(), vsetClear() and vsetIsValid() - #4341

Merged
ranshid merged 3 commits into
valkey-io:unstablefrom
AmariahAK:unstable
Aug 6, 2026
Merged

ranshid merged 3 commits into
valkey-io:unstablefrom
AmariahAK:unstable

Conversation

@AmariahAK

Copy link
Copy Markdown
Contributor

Description

### i. What was the issue

The three public vset APIs — `vsetMemUsage()`, `vsetClear()`, and `vsetIsValid()` — had
no direct unit test coverage in `test_vset.cpp`. The existing tests exercised add, remove,
iterate, expire, defrag, and fuzzer paths, but never directly asserted on memory usage
reporting, clear behaviour, or validity state transitions. The gap was flagged during
review of #3956.

### ii. Where was the issue

`src/unit/test_vset.cpp` — the VsetTest fixture had 10 tests but none covering these three
entry points, despite their being called indirectly through `vsetRelease` and other paths.

### iii. How it was fixed

Added three `TEST_F(VsetTest, ...)` cases:

- **TestVsetMemUsage** — exercises every reachable bucket encoding: NONE (0), SINGLE (0),
  VECTOR (>0), RAX with one bucket (>0), and RAX with multiple buckets (larger than single).
  A top-level HT bucket is unreachable by construction, so it is deliberately not covered
  (same rationale as #3956).

- **TestVsetClear** — verifies clearing an already-empty set is safe, clearing a populated
  set makes it empty (`vsetIsEmpty` returns true), entries survive the clear (caller owns
  them — freed manually afterwards to prove they are still valid), and `vsetRelease`
  correctly makes `vsetIsValid` return false.

- **TestVsetIsValid** — verifies an uninitialized (stack, zeroed) set returns false,
  `vsetInit` produces a valid set, a populated set is still valid, `vsetRelease` makes it
  invalid, and re-initialization after release restores validity.

Also wrapped the GCC-only `-fno-var-tracking-assignments` flag in `CMakeLists.txt` with a
compiler check to fix Clang builds.

### iv. Why this method was chosen

Followed the exact pattern established by #3956 (which added coverage for
`vsetEstimatedEarliestExpiry`): one test per API, exercising every reachable bucket
encoding using the existing `insert_mock_entry_with_expiry()` and `mockGetExpiry` helpers.
This approach keeps the tests readable by C developers (no STL, no C++-only features),
matches the project's existing code style, and avoids over-engineering.

### v. How to test locally

```bash
# Build the server library (libc malloc avoids the jemalloc build step)
cd src && make libvalkey.a MALLOC=libc -j$(nproc)

# Generate wrappers and compile the test
cd unit
python3 generate-wrappers.py
g++ -std=c++17 -c test_vset.cpp main.cpp generated_wrappers.cpp \
  -I.. $(pkg-config --cflags gtest gmock) \
  -Wno-deprecated-declarations -Wno-write-strings -fpermissive

# Link
g++ -std=c++17 -o valkey-unit-gtests *.o ../libvalkey.a \
  $(pkg-config --libs gtest gmock) \
  ../../deps/libvalkey/lib/libvalkey.a \
  ../../deps/hdr_histogram/libhdrhistogram.a \
  ../../deps/fpconv/libfpconv.a -lm -lpthread -ldl

# Run
./valkey-unit-gtests --gtest_filter='VsetTest.*'

All 13 tests pass (10 existing + 3 new).

[----------] 13 tests from VsetTest
[  PASSED  ] VsetTest.TestVsetAddAndIterate
[  PASSED  ] VsetTest.TestVsetLargeBatchSameExpiry
[  PASSED  ] VsetTest.TestVsetLargeBatchUpdateEntrySameExpiry
[  PASSED  ] VsetTest.TestVsetLargeBatchUpdateEntryMultipleExpiries
[  PASSED  ] VsetTest.TestVsetIterateMultipleExpiries
[  PASSED  ] VsetTest.TestVsetAddAndRemoveAll
[  PASSED  ] VsetTest.TestVsetRemoveExpireShrink
[  PASSED  ] VsetTest.TestVsetRemoveExpiredLeavesHtBucketSizeOne
[  PASSED  ] VsetTest.TestVsetDefrag
[  PASSED  ] VsetTest.TestVsetFuzzer
[  PASSED  ] VsetTest.TestVsetMemUsage
[  PASSED  ] VsetTest.TestVsetClear
[  PASSED  ] VsetTest.TestVsetIsValid
[  PASSED  ] 13 tests.

Fixes #4329

…id()

Add three TEST_F(VsetTest, ...) cases covering the public vset APIs that
previously lacked direct unit test coverage:

- TestVsetMemUsage: verifies memory reporting across all reachable bucket
  encodings (NONE, SINGLE, VECTOR, RAX with one bucket, RAX with multiple
  buckets). No top-level HT test (unreachable by construction).

- TestVsetClear: verifies clearing an empty set is safe, clearing a
  populated set makes it empty, entries survive clear (caller owns them),
  and vsetRelease makes vsetIsValid return false.

- TestVsetIsValid: verifies an uninitialized zeroed set returns false,
  initialized returns true, populated returns true, released returns false,
  and re-initialized returns true.

Also fix a GCC-only -fno-var-tracking-assignments flag in CMakeLists.txt
that breaks Clang builds.

Fixes valkey-io#4329

Co-authored-by: atlarix-agent <agent@atlarix.dev>
Signed-off-by: Amariah Kamau <110414493+AmariahAK@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 68506d15-1b97-4050-b5f1-dcb3a980bfc5

📥 Commits

Reviewing files that changed from the base of the PR and between d84a2e8 and 1c3bd5f.

📒 Files selected for processing (2)
  • src/unit/CMakeLists.txt
  • src/unit/test_vset.cpp

📝 Walkthrough

Walkthrough

Changes

vset validation and build configuration

Layer / File(s) Summary
vset API behavior coverage
src/unit/test_vset.cpp
Adds tests for vsetMemUsage(), vsetClear(), and vsetIsValid() across multiple representations and lifecycle states.
GCC-specific unit-test compiler option
src/unit/CMakeLists.txt
Applies -fno-var-tracking-assignments only for GNU C++ compilers.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: eifrah-aws

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The CMake compiler flag change addresses Clang compatibility but is unrelated to the unit test objective in issue #4329. Move the GCC-only compiler flag change to a separate pull request or link an issue that covers the Clang build compatibility requirement.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the added unit test coverage for all three APIs named in the changeset.
Description check ✅ Passed The description explains the missing coverage, test cases, compiler flag change, and reported test results.
Linked Issues check ✅ Passed The PR adds direct coverage for vsetMemUsage(), vsetClear(), and vsetIsValid() as required by issue #4329.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ranshid

ranshid commented Aug 5, 2026

Copy link
Copy Markdown
Member

@valkey-review-bot please review this PR and also the makefile change which I am not sure why it was required

@valkey-review-bot

valkey-review-bot Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

The vset tests match the public API contracts. I also checked the CMake change: it mirrors the existing compiler gate in src/unit/Makefile; Clang rejects -fno-var-tracking-assignments, and the top-level CMake configuration selects Clang on macOS, so the guard is required for CMake unit-test builds there.

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.92%. Comparing base (21c0d49) to head (224836a).
⚠️ Report is 1 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #4341      +/-   ##
============================================
+ Coverage     76.85%   76.92%   +0.06%     
============================================
  Files           162      162              
  Lines         81566    81628      +62     
============================================
+ Hits          62689    62791     +102     
+ Misses        18877    18837      -40     
Files with missing lines Coverage Δ
src/unit/test_vset.cpp 99.43% <100.00%> (+0.11%) ⬆️

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@madolson
madolson requested a review from ranshid August 5, 2026 22:43

@ranshid ranshid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ranshid
ranshid merged commit 1050daf into valkey-io:unstable Aug 6, 2026
64 checks passed
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 UT coverage for vsetMemUsage(), vsetClear() and vsetIsValid()

2 participants