Skip to content

Commit

Permalink
lib: check min value for sampleInterval and duration
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Aug 23, 2024
1 parent 3913f0e commit 879033c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/nsolid.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ const defaultHeapSamplingOptions = { sampleInterval: 512 * 1024, stackDepth: 16,
*/
function heapSamplingStream(threadId, duration, options) {
validateNumber(threadId, 'threadId');
validateNumber(duration, 'duration');
validateNumber(duration, 'duration', 1);
options = options || {};
validateObject(options, 'options');
options = { ...defaultHeapSamplingOptions, ...options };
validateNumber(options.sampleInterval, 'options.sampleInterval');
validateNumber(options.sampleInterval, 'options.sampleInterval', 1);
validateNumber(options.stackDepth, 'options.stackDepth');
validateNumber(options.flags, 'options.flags');
const readable = new Readable({
Expand Down
20 changes: 15 additions & 5 deletions test/parallel/test-nsolid-heap-sampling-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const nsolid = require('nsolid');
const { internalBinding } = require('internal/test/binding');

const {
UV_EINVAL,
UV_ESRCH,
} = internalBinding('uv');

Expand Down Expand Up @@ -104,10 +103,21 @@ const {
// Using a sampleInterval of 0 should result in an error as it causes a crash
// on v8.
const opts = { sampleInterval: 0 };
const stream = nsolid.heapSamplingStream(0, 12000, opts);
stream.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'Heap sampling could not be started');
assert.strictEqual(err.code, UV_EINVAL);
assert.throws(() => {
nsolid.heapSamplingStream(0, 12000, opts);
}, common.expectsError({
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "options.sampleInterval" is out of range. It must be >= 1. Received 0',
}));
}

{
// Using a duration of 0 should result in an error
assert.throws(() => {
nsolid.heapSamplingStream(10, 0);
}, common.expectsError({
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "duration" is out of range. It must be >= 1. Received 0',
}));
}

Expand Down

0 comments on commit 879033c

Please sign in to comment.