Skip to content
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

Merged
merged 2 commits into from
Sep 19, 2016
Merged

Conversation

targos
Copy link
Member

@targos targos commented Sep 15, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

buffer

Description of change

cc @nodejs/testing

@targos targos added buffer Issues and PRs related to the buffer subsystem. test Issues and PRs related to the tests. labels Sep 15, 2016
@nodejs-github-bot nodejs-github-bot added the buffer Issues and PRs related to the buffer subsystem. label Sep 15, 2016
@Trott
Copy link
Member

Trott commented Sep 15, 2016

LGTM if CI passes

@targos
Copy link
Member Author

targos commented Sep 15, 2016

@@ -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);
Copy link
Member

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?

Copy link
Member Author

@targos targos Sep 15, 2016

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.

Copy link
Member

@ChALkeR ChALkeR Sep 15, 2016

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.

Copy link
Member

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.

Copy link
Member Author

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

@ChALkeR
Copy link
Member

ChALkeR commented Sep 15, 2016

First commit (buffer: remove unnecessary argument check) LGTM.

The second one — not sure what happened to the .offset/.byteOffset of unsafe buffers. Is this intentional?

@targos
Copy link
Member Author

targos commented Sep 16, 2016

Copy link
Member

@ChALkeR ChALkeR left a comment

Choose a reason for hiding this comment

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

LGTM

@YurySolovyov
Copy link

test/parallel/test-buffer-new.js sounds like it is testing constructor, while does different thing.

@targos
Copy link
Member Author

targos commented Sep 17, 2016

test/parallel/test-buffer-new.js sounds like it is testing constructor, while does different thing.

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>
@targos targos merged commit 8699ecd into nodejs:master Sep 19, 2016
@targos targos deleted the buffer-test-coverage branch September 19, 2016 07:17
@targos
Copy link
Member Author

targos commented Sep 19, 2016

Thanks, landed in 4c76881...8699ecd

@MylesBorins
Copy link
Contributor

@targos should this be backported?

@targos
Copy link
Member Author

targos commented Oct 7, 2016

@thealphanerd If #8256 is backported, the test commit can be. Otherwise I think it's not worth the trouble.
The lib/buffer.js change isn't relevant in v4.x

Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
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>
Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
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>
@ChALkeR
Copy link
Member

ChALkeR commented Oct 23, 2016

The alloc tests probably should be backported to v4.x.
If they were present there earlier, it would have saved us from #9226, for example.

@MylesBorins
Copy link
Contributor

@ChALkeR can you take a lead on that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants