-
-
Notifications
You must be signed in to change notification settings - Fork 498
Test and fix AsyncWorker #30
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Test and fix AsyncWorker
- Remove MakeCallback overload that defaulted to undefined receiver, because node::MakeCallback requires an object. - Allow the AsyncWorker constructor to optionally specify a receiver object, that is persisted and used as of an async callback. - Persist async errors as strings, because an Error object cannot be created outside of a JS context. - Remove overridable AsyncWorker::WorkComplete() because it wasn't useful and caused confusion. OnOK() and/or OnError() should be (optionally) overridden instead. - Add tests to validate basic success and error scenarios for AsyncWorker. - Also add necessary cast to Object when calling Unwrap.
- Loading branch information
commit 4236269ae9a99c44bd301426ed875fda6d4caa31
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #include "napi.h" | ||
|
|
||
| using namespace Napi; | ||
|
|
||
| class TestWorker : public AsyncWorker { | ||
| public: | ||
| static void DoWork(const CallbackInfo& info) { | ||
| bool succeed = info[0].As<Boolean>(); | ||
| Function cb = info[1].As<Function>(); | ||
| Value data = info[2]; | ||
|
|
||
| TestWorker* worker = new TestWorker(cb); | ||
| worker->Receiver().Set("data", data); | ||
| worker->_succeed = succeed; | ||
| worker->Queue(); | ||
| } | ||
|
|
||
| protected: | ||
| void Execute() override { | ||
| if (!_succeed) { | ||
| SetError("test error"); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| TestWorker(Function cb) : AsyncWorker(cb) {} | ||
| bool _succeed; | ||
| }; | ||
|
|
||
| Object InitAsyncWorker(Env env) { | ||
| Object exports = Object::New(env); | ||
| exports["doWork"] = Function::New(env, TestWorker::DoWork); | ||
| return exports; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
| const buildType = process.config.target_defaults.default_configuration; | ||
| const binding = require(`./build/${buildType}/binding.node`); | ||
| const assert = require('assert'); | ||
|
|
||
| // Use setTimeout() when asserting after async callbacks because | ||
| // unhandled JS exceptions in async callbacks are currently ignored. | ||
| // See the TODO comment in AsyncWorker::OnWorkComplete(). | ||
|
|
||
| binding.asyncworker.doWork(true, function (e) { | ||
| setTimeout(() => { | ||
| assert.strictEqual(typeof e, 'undefined'); | ||
| assert.strictEqual(typeof this, 'object'); | ||
| assert.strictEqual(this.data, 'test data'); | ||
| }); | ||
| }, 'test data'); | ||
|
|
||
| binding.asyncworker.doWork(false, function (e) { | ||
| setTimeout(() => { | ||
| assert.ok(e instanceof Error); | ||
| assert.strictEqual(e.message, 'test error'); | ||
| assert.strictEqual(typeof this, 'object'); | ||
| assert.strictEqual(this.data, 'test data'); | ||
| }); | ||
| }, 'test data'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Were you still planning on having an overload that allowed the user to pass in the this pointer for use with the callbacks?
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 had been thinking that another object reference would be wasteful, but now I realize it could make sense to use the receiver object as the
_persistentobject reference, if one was supplied, instead of creating a new object. I'll update this PR.