Skip to content

Commit

Permalink
Add option to disable run multiple (#372)
Browse files Browse the repository at this point in the history
* Add option to enable/disable _runMultipleHandlers

* Modify the timing to generate event.json
  • Loading branch information
abetomo authored and DeviaVir committed Aug 27, 2017
1 parent b70555e commit f838c98
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions test/node-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]

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',
'--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', () => {
Expand Down

0 comments on commit f838c98

Please sign in to comment.