-
Notifications
You must be signed in to change notification settings - Fork 459
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
- Loading branch information
Gabriel Schulhof
committed
Feb 13, 2019
1 parent
d8e9c22
commit 3b3a482
Showing
8 changed files
with
124 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,65 @@ | ||
#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 WorkerGone(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["workerGone"] = Function::New(env, PersistentTestWorker::WorkerGone); | ||
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(this), false); | ||
binding.deleteWorker(this); | ||
assert.strictEqual(binding.workerGone(this), true); | ||
resolve(); | ||
}); | ||
})); | ||
} | ||
|
||
test(binding.persistentasyncworker, false, 'binding') | ||
.then(() => test(binding.persistentasyncworker, true, 'binding')) | ||
.then(() => test(noexceptBinding.persistentasyncworker, false, 'noxbinding')) | ||
.then(() => test(noexceptBinding.persistentasyncworker, true, 'noxbinding')); |
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