From 53bd313739df4fd8ae22ec9840b4cc28b3de12b5 Mon Sep 17 00:00:00 2001 From: Kinnan Kwok Date: Fri, 6 Oct 2017 15:23:58 -0400 Subject: [PATCH] test: fix race condition in addon test Backport-PR-URL: https://github.com/nodejs/node/pull/19447 PR-URL: https://github.com/nodejs/node/pull/16037 Reviewed-By: Joyee Cheung Reviewed-By: Evan Lucas Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann --- test/addons-napi/test_promise/test.js | 71 +++++++++++---------------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/test/addons-napi/test_promise/test.js b/test/addons-napi/test_promise/test.js index 4c2a2e5e76c9fb..e0c8a1a7575bdd 100644 --- a/test/addons-napi/test_promise/test.js +++ b/test/addons-napi/test_promise/test.js @@ -4,32 +4,34 @@ const common = require('../../common'); const test_promise = require(`./build/${common.buildType}/test_promise`); const assert = require('assert'); -let expected_result, promise; - // A resolution -expected_result = 42; -promise = test_promise.createPromise(); -promise.then( - common.mustCall(function(result) { - assert.strictEqual(result, expected_result, - 'promise resolved as expected'); - }), - common.mustNotCall()); -test_promise.concludeCurrentPromise(expected_result, true); +{ + const expected_result = 42; + const promise = test_promise.createPromise(); + promise.then( + common.mustCall(function(result) { + assert.strictEqual(result, expected_result, + `promise resolved as expected, received ${result}`); + }), + common.mustNotCall()); + test_promise.concludeCurrentPromise(expected_result, true); +} // A rejection -expected_result = 'It\'s not you, it\'s me.'; -promise = test_promise.createPromise(); -promise.then( - common.mustNotCall(), - common.mustCall(function(result) { - assert.strictEqual(result, expected_result, - 'promise rejected as expected'); - })); -test_promise.concludeCurrentPromise(expected_result, false); +{ + const expected_result = 'It\'s not you, it\'s me.'; + const promise = test_promise.createPromise(); + promise.then( + common.mustNotCall(), + common.mustCall(function(result) { + assert.strictEqual(result, expected_result, + `promise rejected as expected, received ${result}`); + })); + test_promise.concludeCurrentPromise(expected_result, false); +} // Chaining -promise = test_promise.createPromise(); +const promise = test_promise.createPromise(); promise.then( common.mustCall(function(result) { assert.strictEqual(result, 'chained answer', @@ -38,23 +40,10 @@ promise.then( common.mustNotCall()); test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true); -assert.strictEqual(test_promise.isPromise(promise), true, - 'natively created promise is recognized as a promise'); - -assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true, - 'Promise created with JS is recognized as a promise'); - -assert.strictEqual(test_promise.isPromise(2.4), false, - 'Number is recognized as not a promise'); - -assert.strictEqual(test_promise.isPromise('I promise!'), false, - 'String is recognized as not a promise'); - -assert.strictEqual(test_promise.isPromise(undefined), false, - 'undefined is recognized as not a promise'); - -assert.strictEqual(test_promise.isPromise(null), false, - 'null is recognized as not a promise'); - -assert.strictEqual(test_promise.isPromise({}), false, - 'an object is recognized as not a promise'); +assert.strictEqual(test_promise.isPromise(promise), true); +assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true); +assert.strictEqual(test_promise.isPromise(2.4), false); +assert.strictEqual(test_promise.isPromise('I promise!'), false); +assert.strictEqual(test_promise.isPromise(undefined), false); +assert.strictEqual(test_promise.isPromise(null), false); +assert.strictEqual(test_promise.isPromise({}), false);