Repository navigation
Add updateAttributes() comparison tests for all native APIs - #3288
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
🔇 Additional comments (5)
📝 WalkthroughWalkthroughRemoves a global Linux skip for disk store tests, moves partition equality behind a non-Linux guard, and adds updateAttributes() refresh tests for process, thread, disk, network, file store, and power source with monotonicity and consistency assertions. ChangesupdateAttributes() Test Coverage Expansion
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3288 +/- ##
============================================
+ Coverage 74.83% 75.59% +0.75%
Complexity 76 76
============================================
Files 410 410
Lines 15901 15826 -75
Branches 2534 2518 -16
============================================
+ Hits 11899 11963 +64
+ Misses 2544 2393 -151
- Partials 1458 1470 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java`:
- Around line 465-467: The new tests in NativeComparisonTest silently skip cases
when a matching entity isn't found; add a matched counter in each test loop that
checks paired JNA/FFM updateAttributes() calls (increment matched each time you
actually call updateAttributes() on a matching pair in the loop inside methods
that reference updateAttributes()), and after the loop assert matched > 0 so the
test fails if nothing was exercised; for the power-source test that currently
returns early when JNA power sources are empty, change that branch to assert
that both JNA and FFM power source lists are empty before returning (i.e., only
return early if both sides are empty), otherwise proceed to find matches and
increment/assert matched > 0.
- Around line 447-448: The test unconditionally asserts
jnaThread.updateAttributes() and ffmThread.updateAttributes() are true, but
OSThread#updateAttributes() is not implemented on macOS (MacOSThread uses the
default false), so modify NativeComparisonTest to detect macOS (e.g., via a
Platform/OS check or System.getProperty("os.name") contains "Mac") and guard
these assertions: on macOS assertFalse for jnaThread.updateAttributes() and
ffmThread.updateAttributes(), otherwise keep the existing assertTrue;
alternatively skip the checks on macOS—locate the assertions in
NativeComparisonTest and adjust the logic around jnaThread and ffmThread
accordingly, referencing OSThread#updateAttributes(), MacOSThread, and
MacOperatingSystem#getCurrentThread.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 2981d7ec-0958-4d4c-8637-1f7a13fd9b5d
📒 Files selected for processing (1)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 11, windows-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: Analyze (java)
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🔇 Additional comments (2)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java (2)
265-270: LGTM!
405-431: LGTM!
2deee08 to
bb81991
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java (1)
569-577: ⚡ Quick winConsider matching power sources by name for robustness.
The test iterates by index, assuming JNA and FFM return power sources in the same order. For consistency with the disk/network/file store update tests and to guard against implementation-specific ordering variations, consider matching power sources by name (e.g., building a
Map<String, PowerSource>keyed bygetName()).That said, the static
powerSources()test at lines 225-241 also uses index-based iteration, which suggests ordering is consistent in practice.♻️ Optional refactor to match by name
`@Test` void powerSourceUpdateAttributes() throws InterruptedException { List<PowerSource> jna = jnaHal.getPowerSources(); List<PowerSource> ffm = ffmHal.getPowerSources(); if (jna.isEmpty()) { assertThat(ffm).as("both JNA and FFM should be empty").isEmpty(); return; } assertThat(ffm).hasSameSizeAs(jna); + Map<String, PowerSource> ffmByName = ffm.stream() + .collect(Collectors.toMap(PowerSource::getName, Function.identity())); // Delay before refresh Thread.sleep(100); - for (int i = 0; i < jna.size(); i++) { - PowerSource j = jna.get(i); - PowerSource f = ffm.get(i); + int matched = 0; + for (PowerSource j : jna) { + PowerSource f = ffmByName.get(j.getName()); + if (f == null) { + continue; + } + matched++; assertThat(j.updateAttributes()).as("JNA powerSource %s updateAttributes", j.getName()).isTrue(); assertThat(f.updateAttributes()).as("FFM powerSource %s updateAttributes", f.getName()).isTrue(); // After refresh, names should still match and capacity should be valid assertThat(f.getName()).isEqualTo(j.getName()); assertThat(f.getMaxCapacity()).as("FFM maxCapacity(%s)", f.getName()).isEqualTo(j.getMaxCapacity()); } + assertThat(matched).as("matched power sources").isGreaterThan(0); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java` around lines 569 - 577, The test currently iterates jna and ffm lists by index which assumes identical ordering; instead, build a Map<String, PowerSource> for one of the providers keyed by getName() (e.g., mapJna by j.getName()), then iterate the other list (ffm) and for each f use mapJna.get(f.getName()) to find the matching PowerSource, assert the match exists, call updateAttributes() on both PowerSource instances, and compare names and getMaxCapacity() as before (refer to methods PowerSource.getName(), PowerSource.updateAttributes(), PowerSource.getMaxCapacity() and variables jna/ffm/powerSources()).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java`:
- Around line 569-577: The test currently iterates jna and ffm lists by index
which assumes identical ordering; instead, build a Map<String, PowerSource> for
one of the providers keyed by getName() (e.g., mapJna by j.getName()), then
iterate the other list (ffm) and for each f use mapJna.get(f.getName()) to find
the matching PowerSource, assert the match exists, call updateAttributes() on
both PowerSource instances, and compare names and getMaxCapacity() as before
(refer to methods PowerSource.getName(), PowerSource.updateAttributes(),
PowerSource.getMaxCapacity() and variables jna/ffm/powerSources()).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: e29898e5-2d1a-43d0-925c-94303c46e943
📒 Files selected for processing (1)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: Test JDK 11, windows-latest
- GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Analyze (java)
🔇 Additional comments (6)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java (6)
265-270: LGTM!
405-431: LGTM!
433-458: LGTM!
460-491: LGTM!
493-528: LGTM!
530-556: LGTM!
8e66642 to
145fdea
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java`:
- Around line 551-553: The assertion in NativeComparisonTest that
j.getTotalSpace() is strictly greater than 0 can fail for pseudo/virtual file
stores; relax the lower bound by changing the assertion on getTotalSpace() (used
in the test methods around NativeComparisonTest where j and f represent file
stores) to allow zero (e.g., assertGreaterThanOrEqualTo(0L) or compare with
f.getTotalSpace() only), and ensure the subsequent assertion comparing
f.getTotalSpace() to j.getTotalSpace() still verifies equality using
getTotalSpace() so real mismatches are caught.
In `@oshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.java`:
- Around line 193-201: Capture pre-update counters from os.getCurrentThread()
into local variables (e.g., preKernel = currentThread.getKernelTime(), preUser =
currentThread.getUserTime()), then call currentThread.updateAttributes(); if it
returns false assert the runtime OS is macOS by checking
System.getProperty("os.name") contains "mac" to allow the unimplemented case;
otherwise assert updateAttributes() returned true and that post-update
currentThread.getKernelTime() and getUserTime() are >= their respective
pre-update values and >= 0. Use the symbols OperatingSystem os, OSThread
currentThread, getCurrentThread(), updateAttributes(), getKernelTime(), and
getUserTime() to locate and modify the test.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 649e765f-78a4-4505-8a48-414ea7e0ecff
📒 Files selected for processing (4)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.javaoshi-core/src/test/java/oshi/hardware/platform/shared/PowerSourceTest.javaoshi-core/src/test/java/oshi/software/os/shared/FileSystemTest.javaoshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🔇 Additional comments (5)
oshi-benchmark/src/test/java/oshi/comparison/NativeComparisonTest.java (2)
48-48: LGTM!Also applies to: 266-271, 412-459, 461-529, 559-578
531-550: LGTM!Also applies to: 554-557
oshi-core/src/test/java/oshi/software/os/shared/FileSystemTest.java (1)
66-72: LGTM!oshi-core/src/test/java/oshi/hardware/platform/shared/PowerSourceTest.java (1)
40-46: LGTM!oshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.java (1)
52-52: LGTM!Also applies to: 176-184
| // Test updateAttributes on current thread (not implemented on macOS) | ||
| OSThread currentThread = os.getCurrentThread(); | ||
| assertThat("Current thread shouldn't be null", currentThread, is(notNullValue())); | ||
| if (currentThread.updateAttributes()) { | ||
| assertThat("Thread kernel time should be 0 or higher after update", currentThread.getKernelTime(), | ||
| is(greaterThanOrEqualTo(0L))); | ||
| assertThat("Thread user time should be 0 or higher after update", currentThread.getUserTime(), | ||
| is(greaterThanOrEqualTo(0L))); | ||
| } |
There was a problem hiding this comment.
Strengthen the current-thread update test to avoid silent pass and validate refresh behavior.
If updateAttributes() returns false, this branch currently skips all checks and the test still passes. Also, >= 0 checks don’t verify that refreshed counters are nondecreasing after update. Capture pre-update times, assert expected success outside macOS, and compare post-update values.
Suggested test tightening
// Test updateAttributes on current thread (not implemented on macOS)
OSThread currentThread = os.getCurrentThread();
assertThat("Current thread shouldn't be null", currentThread, is(notNullValue()));
- if (currentThread.updateAttributes()) {
- assertThat("Thread kernel time should be 0 or higher after update", currentThread.getKernelTime(),
- is(greaterThanOrEqualTo(0L)));
- assertThat("Thread user time should be 0 or higher after update", currentThread.getUserTime(),
- is(greaterThanOrEqualTo(0L)));
+ long kernelBefore = currentThread.getKernelTime();
+ long userBefore = currentThread.getUserTime();
+ boolean updated = currentThread.updateAttributes();
+ assertThat("Current thread updateAttributes should succeed outside macOS", updated || Platform.isMac(), is(true));
+ if (updated) {
+ assertThat("Thread kernel time should be nondecreasing after update", currentThread.getKernelTime(),
+ is(greaterThanOrEqualTo(kernelBefore)));
+ assertThat("Thread user time should be nondecreasing after update", currentThread.getUserTime(),
+ is(greaterThanOrEqualTo(userBefore)));
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Test updateAttributes on current thread (not implemented on macOS) | |
| OSThread currentThread = os.getCurrentThread(); | |
| assertThat("Current thread shouldn't be null", currentThread, is(notNullValue())); | |
| if (currentThread.updateAttributes()) { | |
| assertThat("Thread kernel time should be 0 or higher after update", currentThread.getKernelTime(), | |
| is(greaterThanOrEqualTo(0L))); | |
| assertThat("Thread user time should be 0 or higher after update", currentThread.getUserTime(), | |
| is(greaterThanOrEqualTo(0L))); | |
| } | |
| // Test updateAttributes on current thread (not implemented on macOS) | |
| OSThread currentThread = os.getCurrentThread(); | |
| assertThat("Current thread shouldn't be null", currentThread, is(notNullValue())); | |
| long kernelBefore = currentThread.getKernelTime(); | |
| long userBefore = currentThread.getUserTime(); | |
| boolean updated = currentThread.updateAttributes(); | |
| assertThat("Current thread updateAttributes should succeed outside macOS", updated || Platform.isMac(), is(true)); | |
| if (updated) { | |
| assertThat("Thread kernel time should be nondecreasing after update", currentThread.getKernelTime(), | |
| is(greaterThanOrEqualTo(kernelBefore))); | |
| assertThat("Thread user time should be nondecreasing after update", currentThread.getUserTime(), | |
| is(greaterThanOrEqualTo(userBefore))); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@oshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.java`
around lines 193 - 201, Capture pre-update counters from os.getCurrentThread()
into local variables (e.g., preKernel = currentThread.getKernelTime(), preUser =
currentThread.getUserTime()), then call currentThread.updateAttributes(); if it
returns false assert the runtime OS is macOS by checking
System.getProperty("os.name") contains "mac" to allow the unimplemented case;
otherwise assert updateAttributes() returned true and that post-update
currentThread.getKernelTime() and getUserTime() are >= their respective
pre-update values and >= 0. Use the symbols OperatingSystem os, OSThread
currentThread, getCurrentThread(), updateAttributes(), getKernelTime(), and
getUserTime() to locate and modify the test.
Add JNA vs FFM comparison tests exercising updateAttributes() on all interfaces that define it: OSProcess, OSThread, HWDiskStore, NetworkIF, OSFileStore, and PowerSource. Each test snapshots values, sleeps briefly, calls updateAttributes() on both JNA and FFM instances, then verifies: - updateAttributes() returns true - Monotonically nondecreasing counters have not decreased (kernel/user time, disk reads/writes, network bytes sent/received) - Static values remain consistent between JNA and FFM Also narrows the Linux disk test skip: instead of disabling the entire diskStores() test on Linux, only skip the partition name comparison which is affected by the JNA UdevDevice.getSysname() bug fixed in java-native-access/jna#1715 (not yet released past 5.18.1).
145fdea to
7bdf5d9
Compare
Add JNA vs FFM comparison tests exercising
updateAttributes()on all interfaces that define it:OSProcess,OSThread,HWDiskStore,NetworkIF,OSFileStore, andPowerSource.Each test snapshots values, sleeps briefly, calls
updateAttributes()on both JNA and FFM instances, then verifies:updateAttributes()returns trueAlso narrows the Linux disk test skip: instead of disabling the entire
diskStores()test on Linux, only skip the partition name comparison which is affected by the JNAUdevDevice.getSysname()bug fixed in java-native-access/jna#1715 (not yet released past 5.18.1).Motivated by #3287 where patch coverage showed
updateAttributes()was not being exercised.Summary by CodeRabbit