From 7a95be8aed07ddf38b91c145eebf6e0e0624c8f6 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 18 May 2017 13:16:59 +0900 Subject: [PATCH 1/3] Fix to support `callbackWaitsForEmptyEventLoop` It is not a complete reproduction of AWS Lambda as it is a simple one. --- lib/main.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/main.js b/lib/main.js index 51cfd992..47f63ad3 100644 --- a/lib/main.js +++ b/lib/main.js @@ -68,14 +68,18 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { var callback = function (err, result) { if (err) { + process.exitCode = 255; console.log('Error: ' + err); - process.exit(255); + } else { + process.exitCode = 0; + console.log('Success:'); + if (result) { + console.log(JSON.stringify(result)); + } } - console.log('Success:'); - if (!result) { - process.exit(0); + if (context.callbackWaitsForEmptyEventLoop === false) { + process.exit(); } - console.log(JSON.stringify(result)); }; context.getRemainingTimeInMillis = function () { From 43c995ea8feb49bb08528bda2f886a291025ea5d Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 18 May 2017 13:19:25 +0900 Subject: [PATCH 2/3] Fix test as it supported `callbackWaitsForEmptyEventLoop` --- test/node-lambda.js | 100 ++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 32 deletions(-) diff --git a/test/node-lambda.js b/test/node-lambda.js index b0b39558..4a1c4501 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -10,6 +10,20 @@ const nodeLambdaPath = path.join(__dirname, '..', 'bin', 'node-lambda'); // The reason for specifying the node command in this test is to support Windows. describe('bin/node-lambda', () => { describe('node-lambda run', () => { + const _testMain = (expectedValues, done) => { + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', (code) => { + assert.match(stdoutString, expectedValues.stdoutRegExp); + assert.equal(code, expectedValues.exitCode); + done(); + }); + }; + const _generateHandlerFile = (callbackString) => { fs.writeFileSync( '__test.js', @@ -27,53 +41,75 @@ describe('bin/node-lambda', () => { 'event.json', 'deploy.env', 'event_sources.json', - '__test.js' +// '__test.js' ].forEach((file) => fs.unlinkSync(file)); }); it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { - const run = spawn('node', [nodeLambdaPath, 'run']); - var stdoutString = ''; - run.stdout.on('data', (data) => { - stdoutString += data.toString().replace(/\r|\n/g, ''); - }); - - run.on('exit', (code) => { - assert.match(stdoutString, /Success:$/); - assert.equal(code, 0); - done(); - }); + _generateHandlerFile('callback(null);'); + _testMain({ stdoutRegExp: /Success:$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { _generateHandlerFile('callback(null, "text");'); - - const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); - var stdoutString = ''; - run.stdout.on('data', (data) => { - stdoutString += data.toString().replace(/\r|\n/g, ''); - }); - - run.on('exit', (code) => { - assert.match(stdoutString, /Success:"text"$/); - assert.equal(code, 0); - done(); - }); + _testMain({ stdoutRegExp: /Success:"text"$/, exitCode: 0 }, done); }); it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { _generateHandlerFile('callback(new Error("e"));'); + _testMain({ stdoutRegExp: /Error: Error: e$/, exitCode: 255 }, done); + }); - const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); - var stdoutString = ''; - run.stdout.on('data', (data) => { - stdoutString += data.toString().replace(/\r|\n/g, ''); + describe('node-lambda run (async)', function () { + this.timeout(5000); // give it time to setTimeout + + const _generateHandlerFile = (callbackString, callbackWaitsForEmptyEventLoop) => { + const asyncCodeAndCallbackWaitsForEmptyEventLoopSettig = ` + setTimeout(() => console.log('sleep 3500 msec'), 3500); + context.callbackWaitsForEmptyEventLoop = ${callbackWaitsForEmptyEventLoop}; + `; + const testJsText = fs + .readFileSync('index.js').toString() + .replace( + /console.log\('Running index.handler'\);/, + asyncCodeAndCallbackWaitsForEmptyEventLoopSettig + ) + .replace(/callback\(null\);/, callbackString); + fs.writeFileSync('__test.js', testJsText); + }; + + describe('callbackWaitsForEmptyEventLoop = true', () => { + it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { + _generateHandlerFile('callback(null);', true); + _testMain({ stdoutRegExp: /Success:sleep 3500 msec$/, exitCode: 0 }, done); + }); + + it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { + _generateHandlerFile('callback(null, "text");', true); + _testMain({ stdoutRegExp: /Success:"text"sleep 3500 msec$/, exitCode: 0 }, done); + }); + + it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { + _generateHandlerFile('callback(new Error("e"));', true); + _testMain({ stdoutRegExp: /Error: Error: esleep 3500 msec$/, exitCode: 255 }, done); + }); }); - run.on('exit', (code) => { - assert.match(stdoutString, /Error: Error: e$/); - assert.equal(code, 255); - done(); + describe('callbackWaitsForEmptyEventLoop = false', () => { + it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { + _generateHandlerFile('callback(null);', false); + _testMain({ stdoutRegExp: /Success:$/, exitCode: 0 }, done); + }); + + it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { + _generateHandlerFile('callback(null, "text");', false); + _testMain({ stdoutRegExp: /Success:"text"$/, exitCode: 0 }, done); + }); + + it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { + _generateHandlerFile('callback(new Error("e"));', false); + _testMain({ stdoutRegExp: /Error: Error: e$/, exitCode: 255 }, done); + }); }); }); }); From 4450858ba852830232e57e89dbfba9b20d9207e5 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 18 May 2017 13:22:03 +0900 Subject: [PATCH 3/3] Remove debugging code --- test/node-lambda.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/node-lambda.js b/test/node-lambda.js index 4a1c4501..d4aa84df 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -41,7 +41,7 @@ describe('bin/node-lambda', () => { 'event.json', 'deploy.env', 'event_sources.json', -// '__test.js' + '__test.js' ].forEach((file) => fs.unlinkSync(file)); });