-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add AsyncWorker destruction suppression
Add method `SuppressDestruct()` to `AsyncWorker`, which will cause an instance of the class to remain allocated even after the `OnOK` callback fires. Such an instance must be explicitly `delete`-ed from user code. Re: #231 Re: nodejs/abi-stable-node#353 PR-URL: #407 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: NickNaso <nicoladelgobbo@gmail.com>
- Loading branch information
Showing
8 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,67 @@ | ||
#include "napi.h" | ||
|
||
// A variant of TestWorker wherein destruction is suppressed. That is, instances | ||
// are not destroyed during the `OnOK` callback. They must be explicitly | ||
// destroyed. | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
class PersistentTestWorker : public AsyncWorker { | ||
public: | ||
static PersistentTestWorker* current_worker; | ||
static void DoWork(const CallbackInfo& info) { | ||
bool succeed = info[0].As<Boolean>(); | ||
Function cb = info[1].As<Function>(); | ||
|
||
PersistentTestWorker* worker = new PersistentTestWorker(cb, "TestResource"); | ||
current_worker = worker; | ||
|
||
worker->SuppressDestruct(); | ||
worker->_succeed = succeed; | ||
worker->Queue(); | ||
} | ||
|
||
static Value GetWorkerGone(const CallbackInfo& info) { | ||
return Boolean::New(info.Env(), current_worker == nullptr); | ||
} | ||
|
||
static void DeleteWorker(const CallbackInfo& info) { | ||
(void) info; | ||
delete current_worker; | ||
} | ||
|
||
~PersistentTestWorker() { | ||
current_worker = nullptr; | ||
} | ||
|
||
protected: | ||
void Execute() override { | ||
if (!_succeed) { | ||
SetError("test error"); | ||
} | ||
} | ||
|
||
private: | ||
PersistentTestWorker(Function cb, | ||
const char* resource_name) | ||
: AsyncWorker(cb, resource_name) {} | ||
|
||
bool _succeed; | ||
}; | ||
|
||
PersistentTestWorker* PersistentTestWorker::current_worker = nullptr; | ||
|
||
} // end of anonymous namespace | ||
|
||
Object InitPersistentAsyncWorker(Env env) { | ||
Object exports = Object::New(env); | ||
exports["doWork"] = Function::New(env, PersistentTestWorker::DoWork); | ||
exports.DefineProperty( | ||
PropertyDescriptor::Accessor(env, exports, "workerGone", | ||
PersistentTestWorker::GetWorkerGone)); | ||
exports["deleteWorker"] = | ||
Function::New(env, PersistentTestWorker::DeleteWorker); | ||
return exports; | ||
} |
This file contains 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,27 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const common = require('./common'); | ||
const binding = require(`./build/${buildType}/binding.node`); | ||
const noexceptBinding = require(`./build/${buildType}/binding_noexcept.node`); | ||
|
||
function test(binding, succeed) { | ||
return new Promise((resolve) => | ||
// Can't pass an arrow function to doWork because that results in an | ||
// undefined context inside its body when the function gets called. | ||
binding.doWork(succeed, function(e) { | ||
setImmediate(() => { | ||
// If the work is supposed to fail, make sure there's an error. | ||
assert.strictEqual(succeed || e.message === 'test error', true); | ||
assert.strictEqual(binding.workerGone, false); | ||
binding.deleteWorker(); | ||
assert.strictEqual(binding.workerGone, true); | ||
resolve(); | ||
}); | ||
})); | ||
} | ||
|
||
test(binding.persistentasyncworker, false) | ||
.then(() => test(binding.persistentasyncworker, true)) | ||
.then(() => test(noexceptBinding.persistentasyncworker, false)) | ||
.then(() => test(noexceptBinding.persistentasyncworker, true)); |
This file contains 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 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 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