-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tsfn: add wrappers for Ref and Unref
Ref: #556 (comment) PR-URL: #561 Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
Showing
8 changed files
with
125 additions
and
0 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
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,41 @@ | ||
#include "napi.h" | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
static Value TestUnref(const CallbackInfo& info) { | ||
Napi::Env env = info.Env(); | ||
Object global = env.Global(); | ||
Object resource = info[0].As<Object>(); | ||
Function cb = info[1].As<Function>(); | ||
Function setTimeout = global.Get("setTimeout").As<Function>(); | ||
ThreadSafeFunction* tsfn = new ThreadSafeFunction; | ||
|
||
*tsfn = ThreadSafeFunction::New(info.Env(), cb, resource, "Test", 1, 1, [tsfn](Napi::Env /* env */) { | ||
delete tsfn; | ||
}); | ||
|
||
tsfn->BlockingCall(); | ||
|
||
setTimeout.Call( global, { | ||
Function::New(env, [tsfn](const CallbackInfo& info) { | ||
tsfn->Unref(info.Env()); | ||
}), | ||
Number::New(env, 100) | ||
}); | ||
|
||
return info.Env().Undefined(); | ||
} | ||
|
||
} | ||
|
||
Object InitThreadSafeFunctionUnref(Env env) { | ||
Object exports = Object::New(env); | ||
exports["testUnref"] = Function::New(env, TestUnref); | ||
return exports; | ||
} | ||
|
||
#endif |
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,53 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const buildType = process.config.target_defaults.default_configuration; | ||
|
||
const isMainProcess = process.argv[1] != __filename; | ||
|
||
/** | ||
* In order to test that the event loop exits even with an active TSFN, we need | ||
* to spawn a new process for the test. | ||
* - Main process: spawns new node instance, executing this script | ||
* - Child process: creates TSFN. Native module Unref's via setTimeout after some time but does NOT call Release. | ||
* | ||
* Main process should expect child process to exit. | ||
*/ | ||
|
||
if (isMainProcess) { | ||
test(`../build/${buildType}/binding.node`); | ||
test(`../build/${buildType}/binding_noexcept.node`); | ||
} else { | ||
test(process.argv[2]); | ||
} | ||
|
||
function test(bindingFile) { | ||
if (isMainProcess) { | ||
// Main process | ||
const child = require('../napi_child').spawn(process.argv[0], [ '--expose-gc', __filename, bindingFile ], { | ||
stdio: 'inherit', | ||
}); | ||
|
||
let timeout = setTimeout( function() { | ||
child.kill(); | ||
timeout = 0; | ||
throw new Error("Expected child to die"); | ||
}, 5000); | ||
|
||
child.on("error", (err) => { | ||
clearTimeout(timeout); | ||
timeout = 0; | ||
throw new Error(err); | ||
}) | ||
|
||
child.on("close", (code) => { | ||
if (timeout) clearTimeout(timeout); | ||
assert(!code, "Expected return value 0"); | ||
}); | ||
|
||
} else { | ||
// Child process | ||
const binding = require(bindingFile); | ||
binding.threadsafe_function_unref.testUnref({}, () => { }); | ||
} | ||
} |