-
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.
Add resource_name and resource parameters to AsyncWorker constructor
This change is initiated from #140 (comment).
- Loading branch information
Showing
4 changed files
with
134 additions
and
25 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 |
---|---|---|
@@ -1,21 +1,85 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
(async() => { | ||
await test(require(`./build/${buildType}/binding.node`)); | ||
await test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
})(); | ||
|
||
function test(binding) { | ||
binding.asyncworker.doWork(true, function (e) { | ||
assert.strictEqual(typeof e, 'undefined'); | ||
assert.strictEqual(typeof this, 'object'); | ||
assert.strictEqual(this.data, 'test data'); | ||
}, 'test data'); | ||
function installAsyncHooksForTest() { | ||
return new Promise((resolve, reject) => { | ||
let id; | ||
const events = []; | ||
const hook = async_hooks.createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
if (type === 'TestResource'){ | ||
id = asyncId; | ||
events.push({ eventName: 'init', type, triggerAsyncId, resource }); | ||
} | ||
}, | ||
before(asyncId) { | ||
if (asyncId === id) { | ||
events.push({ eventName: 'before' }); | ||
} | ||
}, | ||
after(asyncId) { | ||
if (asyncId === id) { | ||
events.push({ eventName: 'after' }); | ||
} | ||
}, | ||
destroy(asyncId) { | ||
if (asyncId === id) { | ||
events.push({ eventName: 'destroy' }); | ||
hook.disable(); | ||
resolve(events); | ||
} | ||
} | ||
}).enable(); | ||
}); | ||
} | ||
|
||
async function test(binding) { | ||
{ | ||
const hooks = installAsyncHooksForTest(); | ||
const triggerAsyncId = async_hooks.executionAsyncId(); | ||
binding.asyncworker.doWork(true, { foo: 'foo' }, function (e) { | ||
assert.strictEqual(typeof e, 'undefined'); | ||
assert.strictEqual(typeof this, 'object'); | ||
assert.strictEqual(this.data, 'test data'); | ||
}, 'test data'); | ||
|
||
assert.deepStrictEqual(await hooks, [ | ||
{ eventName: 'init', | ||
type: 'TestResource', | ||
triggerAsyncId, | ||
resource: { foo: 'foo' } }, | ||
{ eventName: 'before' }, | ||
{ eventName: 'after' }, | ||
{ eventName: 'destroy' } | ||
]); | ||
} | ||
|
||
{ | ||
const hooks = installAsyncHooksForTest(); | ||
const triggerAsyncId = async_hooks.executionAsyncId(); | ||
|
||
binding.asyncworker.doWork(false, { foo: 'foo' }, function (e) { | ||
assert.ok(e instanceof Error); | ||
assert.strictEqual(e.message, 'test error'); | ||
assert.strictEqual(typeof this, 'object'); | ||
assert.strictEqual(this.data, 'test data'); | ||
}, 'test data'); | ||
|
||
binding.asyncworker.doWork(false, function (e) { | ||
assert.ok(e instanceof Error); | ||
assert.strictEqual(e.message, 'test error'); | ||
assert.strictEqual(typeof this, 'object'); | ||
assert.strictEqual(this.data, 'test data'); | ||
}, 'test data'); | ||
assert.deepStrictEqual(await hooks, [ | ||
{ eventName: 'init', | ||
type: 'TestResource', | ||
triggerAsyncId, | ||
resource: { foo: 'foo' } }, | ||
{ eventName: 'before' }, | ||
{ eventName: 'after' }, | ||
{ eventName: 'destroy' } | ||
]); | ||
} | ||
} |