-
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.
Add resource parameters to AsyncWorker constructor
This change is initiated from #140 (comment). PR-URL: #253 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
- Loading branch information
Showing
4 changed files
with
134 additions
and
22 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,88 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const common = require('./common'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function installAsyncHooksForTest() { | ||
return new Promise((resolve, reject) => { | ||
let id; | ||
const events = []; | ||
const hook = async_hooks.createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
if (id === undefined && 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(); | ||
}); | ||
} | ||
|
||
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'); | ||
{ | ||
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'); | ||
|
||
hooks.then(actual => { | ||
assert.deepStrictEqual(actual, [ | ||
{ eventName: 'init', | ||
type: 'TestResource', | ||
triggerAsyncId: triggerAsyncId, | ||
resource: { foo: 'foo' } }, | ||
{ eventName: 'before' }, | ||
{ eventName: 'after' }, | ||
{ eventName: 'destroy' } | ||
]); | ||
}).catch(common.mustNotCall()); | ||
} | ||
|
||
{ | ||
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'); | ||
hooks.then(actual => { | ||
assert.deepStrictEqual(actual, [ | ||
{ eventName: 'init', | ||
type: 'TestResource', | ||
triggerAsyncId: triggerAsyncId, | ||
resource: { foo: 'foo' } }, | ||
{ eventName: 'before' }, | ||
{ eventName: 'after' }, | ||
{ eventName: 'destroy' } | ||
]); | ||
}).catch(common.mustNotCall()); | ||
} | ||
} |