-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve buffer test coverage #8552
Conversation
LGTM if CI passes |
@@ -8,6 +8,8 @@ const SlowBuffer = require('buffer').SlowBuffer; | |||
|
|||
const b = Buffer.allocUnsafe(1024); | |||
assert.strictEqual(1024, b.length); | |||
assert.strictEqual(0, b.byteOffset); | |||
assert.strictEqual(0, b.offset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Um. Since when are unsafe buffers not sliced from a pool? Did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran this test locally 1000 times and it never failed. It works because it is the first allocation in this test and so will use the start of the pool. The goal here is to cover Buffer.prototype.offset
so I can change it to assert.strictEqual(b.byteOffset, b.offset)
if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you should test .offset
and .byteOffset
to be equal to 0
on buffers created using Buffer.alloc(number)
and/or Buffer.allocUnsafeSlow(number)
— those are not pooled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw,
It works because it is the first allocation in this test and so will use the start of the pool.
is most likely not valid, as many buffers are allocated internally, so this is not the first allocation. Try removing const common = require('../common');
, moving it below this check, or adding some other requires, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to use Buffer.alloc
, PTAL
First commit ( The second one — not sure what happened to the |
0b9a8cb
to
95a9e3e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
It is actually testing something specific to the constructor. I couldn't find any relevant place in the existing tests so I created this file and gave it a not too specific name so that we could add tests to it in the future. |
In Buffer.prototype.compare, the first check makes sure that target is an instance of Buffer. The value cannot be falsy after that so we can safely get its length. PR-URL: nodejs#8552 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Add tests for untested branches and statements. Also convert some lines to const. PR-URL: nodejs#8552 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
95a9e3e
to
8699ecd
Compare
Thanks, landed in 4c76881...8699ecd |
@targos should this be backported? |
@thealphanerd If #8256 is backported, the test commit can be. Otherwise I think it's not worth the trouble. |
In Buffer.prototype.compare, the first check makes sure that target is an instance of Buffer. The value cannot be falsy after that so we can safely get its length. PR-URL: #8552 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Add tests for untested branches and statements. Also convert some lines to const. PR-URL: #8552 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
The alloc tests probably should be backported to v4.x. |
@ChALkeR can you take a lead on that? |
Checklist
make -j4 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
buffer
Description of change
cc @nodejs/testing