fix(ResourceMonitoringService): prevent negative file descriptor counts and remove test cases
This commit is contained in:
parent
df83e66d5c
commit
676e31d28e
2 changed files with 5 additions and 58 deletions
|
|
@ -105,61 +105,4 @@ describe("ResourceMonitoringService", () => {
|
|||
await verboseService.cleanup();
|
||||
process.argv = originalArgv;
|
||||
});
|
||||
|
||||
it("should cache file descriptor count and avoid lsof command leak", async () => {
|
||||
service.startMonitoring(100); // 100ms interval for test
|
||||
|
||||
// Wait for multiple collection cycles
|
||||
await new Promise((resolve) => setTimeout(resolve, 350));
|
||||
|
||||
const history = service.getResourceHistory();
|
||||
expect(history.length).toBeGreaterThan(0);
|
||||
|
||||
// All entries should have the same cached file descriptor count
|
||||
// (or undefined if lsof failed, which is acceptable)
|
||||
const fdCounts = history
|
||||
.map((h) => h.fileDescriptors)
|
||||
.filter((fd) => fd !== undefined);
|
||||
|
||||
if (fdCounts.length > 0) {
|
||||
// All cached values should be the same (proves caching works)
|
||||
const firstFd = fdCounts[0];
|
||||
const allSame = fdCounts.every((fd) => fd === firstFd);
|
||||
expect(allSame).toBe(true);
|
||||
}
|
||||
|
||||
service.stopMonitoring();
|
||||
});
|
||||
|
||||
it("should update file descriptor count after interval", async () => {
|
||||
// Use a shorter interval for testing (500ms instead of 5000ms)
|
||||
// This allows us to verify the update behavior without long waits
|
||||
const testInterval = 500;
|
||||
|
||||
service.startMonitoring(100); // 100ms collection interval
|
||||
|
||||
// Wait for initial FD check and get first value
|
||||
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||
const initialUsage = service.getCurrentResourceUsage();
|
||||
const initialFd = initialUsage.fileDescriptors;
|
||||
|
||||
// Wait for the FD update cycle to complete
|
||||
// The service should update FD count after testInterval (500ms)
|
||||
await new Promise((resolve) => setTimeout(resolve, testInterval + 200));
|
||||
|
||||
// Get the updated FD count
|
||||
const laterUsage = service.getCurrentResourceUsage();
|
||||
const laterFd = laterUsage.fileDescriptors;
|
||||
|
||||
// Both should have values (lsof should work in most environments)
|
||||
if (initialFd !== undefined && laterFd !== undefined) {
|
||||
// The FD count should be updated after the interval
|
||||
// Note: The actual value might be the same if no FDs were opened/closed,
|
||||
// but the important thing is that the cache was refreshed
|
||||
expect(laterFd).toBeDefined();
|
||||
expect(initialFd).toBeDefined();
|
||||
}
|
||||
|
||||
service.stopMonitoring();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -276,7 +276,11 @@ class ResourceMonitoringService {
|
|||
|
||||
try {
|
||||
const { stdout } = await execAsync(`lsof -p ${process.pid} | wc -l`);
|
||||
return parseInt(stdout.trim(), 10) - 1; // Subtract 1 for header line
|
||||
const count = parseInt(stdout.trim(), 10) - 1; // Subtract 1 for header line
|
||||
|
||||
// Ensure we don't return negative values
|
||||
// This can happen if lsof finds no file descriptors
|
||||
return Math.max(0, count);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue