From ea3078cd8af152fce7906283d6e54f796e3c9496 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 24 Aug 2017 21:21:11 +0900 Subject: [PATCH 1/2] Add option to enable/disable _runMultipleHandlers --- bin/node-lambda | 3 +++ lib/main.js | 12 +++++++++--- test/node-lambda.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/bin/node-lambda b/bin/node-lambda index df554fde..731c0bcd 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -55,6 +55,7 @@ const AWS_DLQ_TARGET_ARN = (() => { return undefined })() const PROXY = process.env.PROXY || process.env.http_proxy || '' +const ENABLE_RUN_MULTIPLE_EVENTS = true program .command('deploy') @@ -126,6 +127,8 @@ program .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE) + .option('-M, --enableRunMultipleEvents [' + ENABLE_RUN_MULTIPLE_EVENTS + ']', 'Enable run multiple events', + ENABLE_RUN_MULTIPLE_EVENTS) .action((prg) => lambda.run(prg)) program diff --git a/lib/main.js b/lib/main.js index b413b046..0c1b7415 100644 --- a/lib/main.js +++ b/lib/main.js @@ -67,11 +67,17 @@ class Lambda { const handler = require(path.join(process.cwd(), filename))[handlername] const event = require(path.join(process.cwd(), program.eventFile)) const context = require(path.join(process.cwd(), program.contextFile)) + const enableRunMultipleEvents = (() => { + if (typeof program.enableRunMultipleEvents === 'boolean') { + return program.enableRunMultipleEvents + } + return program.enableRunMultipleEvents === 'true' + })() - if (!Array.isArray(event)) { - return this._runHandler(handler, event, program, context) + if (Array.isArray(event) && enableRunMultipleEvents === true) { + return this._runMultipleHandlers(event) } - this._runMultipleHandlers(event) + this._runHandler(handler, event, program, context) } _runHandler (handler, event, program, context) { diff --git a/test/node-lambda.js b/test/node-lambda.js index dcabaf30..a2f97871 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -197,6 +197,53 @@ describe('bin/node-lambda', () => { }, done) }) }) + + describe('node-lambda run (disable Multiple events))', () => { + const eventObj = [{ + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 1 + }, { + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 2 + }, { + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 3 + }] + _generateEventFile(eventObj) + + it('`node-lambda run` exitCode is `0`', function (done) { + this.timeout(10000) // give it time to multiple executions + + const run = spawn('node', [ + nodeLambdaPath, 'run', + '--handler', 'index.handler', + '--eventFile', 'event.json', + '-M', 'false' + ]) + let stdoutString = '' + run.stdout.on('data', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, '') + }) + + run.on('exit', (code) => { + const expected = 'Running index.handler==================================event ' + + '[ { asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 1 }, ' + + '{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 2 }, ' + + '{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 3 } ]' + + '==================================Stopping index.handlerSuccess:' + + assert.equal(stdoutString, expected) + assert.equal(code, 0) + done() + }) + }) + }) }) describe('node-lambda --version', () => { From 4e16d80f01eef48ab2bb4f1bd6f1df2532ee8ab1 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 24 Aug 2017 21:46:59 +0900 Subject: [PATCH 2/2] Modify the timing to generate event.json --- 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 a2f97871..857b3030 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -215,11 +215,11 @@ describe('bin/node-lambda', () => { callbackCode: 'callback(null);', no: 3 }] - _generateEventFile(eventObj) it('`node-lambda run` exitCode is `0`', function (done) { this.timeout(10000) // give it time to multiple executions + _generateEventFile(eventObj) const run = spawn('node', [ nodeLambdaPath, 'run', '--handler', 'index.handler',